-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudio.cs
84 lines (71 loc) · 2.39 KB
/
Audio.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Godot;
namespace LD56;
public partial class Audio : Node
{
public static Audio Instance { get; private set; }
private AudioStreamPlayer backgroundMusic = new();
private AudioStream splat = GD.Load<AudioStream>("res://audio/splat.wav");
private AudioStream plop = GD.Load<AudioStream>("res://audio/plop.wav");
private AudioStream ding = GD.Load<AudioStream>("res://audio/ding.wav");
public float BackgroundVolumeLinear
{
get => Mathf.DbToLinear(backgroundMusic.VolumeDb);
set => backgroundMusic.VolumeDb = Mathf.LinearToDb(value);
}
public Audio()
{
Instance = this;
}
public override void _Ready()
{
StartBackgroundMusic();
}
public void StartBackgroundMusic()
{
BackgroundVolumeLinear = 0.1f;
backgroundMusic.Stream = GD.Load<AudioStream>("res://audio/background-music.mp3");
backgroundMusic.Autoplay = true;
backgroundMusic.Finished += () => backgroundMusic.Play();
AddChild(backgroundMusic);
}
public void StopBackgroundMusic()
{
backgroundMusic.Stop();
RemoveChild(backgroundMusic);
}
public void PlayOneShot(AudioStream audioStream, float volumeLinear = 0.1f, Node node = null)
{
var player = new AudioStreamPlayer();
player.Stream = audioStream;
player.Autoplay = true;
player.VolumeDb = Mathf.LinearToDb(volumeLinear);
player.Finished += () => RemoveChild(player);
(node ?? this).AddChild(player);
}
public void PlayOneShotAt(Vector2 position, AudioStream audioStream, float volumeLinear = 0.1f, Node node = null)
{
var player = new AudioStreamPlayer2D();
player.GlobalPosition = position;
player.Stream = audioStream;
player.Autoplay = true;
player.VolumeDb = Mathf.LinearToDb(volumeLinear);
player.Finished += () => RemoveChild(player);
(node ?? this).AddChild(player);
}
public void Plop(Node node)
{
PlayOneShot(plop, 0.5f, node);
}
public void Splat(float volumeLinear = 0.1f, Node node = null)
{
PlayOneShot(splat, volumeLinear, node);
}
public void Ding()
{
PlayOneShot(ding, 0.4f);
}
public void SplatAt(Vector2 position, float volumeLinear = 0.1f, Node node = null)
{
PlayOneShotAt(position, splat, volumeLinear, node);
}
}