Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ActiveSound.cs
Go to the documentation of this file.
2using Microsoft.Xna.Framework.Audio;
3
4namespace Terraria.Audio;
5
6public class ActiveSound
7{
8 public delegate bool LoopedPlayCondition();
9
10 public readonly bool IsGlobal;
11
13
14 public float Volume;
15
16 public float Pitch;
17
18 public LoopedPlayCondition Condition;
19
20 public SoundEffectInstance Sound { get; private set; }
21
22 public SoundStyle Style { get; private set; }
23
24 public bool IsPlaying => Sound.State == SoundState.Playing;
25
26 public ActiveSound(SoundStyle style, Vector2 position)
27 {
28 Position = position;
29 Volume = 1f;
30 Pitch = style.PitchVariance;
31 IsGlobal = false;
32 Style = style;
33 Play();
34 }
35
36 public ActiveSound(SoundStyle style)
37 {
39 Volume = 1f;
40 Pitch = style.PitchVariance;
41 IsGlobal = true;
42 Style = style;
43 Play();
44 }
45
46 public ActiveSound(SoundStyle style, Vector2 position, LoopedPlayCondition condition)
47 {
48 Position = position;
49 Volume = 1f;
50 Pitch = style.PitchVariance;
51 IsGlobal = false;
52 Style = style;
53 PlayLooped(condition);
54 }
55
56 private void Play()
57 {
58 SoundEffectInstance soundEffectInstance = Style.GetRandomSound().CreateInstance();
59 soundEffectInstance.Pitch += Style.GetRandomPitch();
60 Pitch = soundEffectInstance.Pitch;
61 soundEffectInstance.Play();
62 SoundInstanceGarbageCollector.Track(soundEffectInstance);
63 Sound = soundEffectInstance;
64 Update();
65 }
66
67 private void PlayLooped(LoopedPlayCondition condition)
68 {
69 SoundEffectInstance soundEffectInstance = Style.GetRandomSound().CreateInstance();
70 soundEffectInstance.Pitch += Style.GetRandomPitch();
71 Pitch = soundEffectInstance.Pitch;
72 soundEffectInstance.IsLooped = true;
73 Condition = condition;
74 soundEffectInstance.Play();
75 SoundInstanceGarbageCollector.Track(soundEffectInstance);
76 Sound = soundEffectInstance;
77 Update();
78 }
79
80 public void Stop()
81 {
82 if (Sound != null)
83 {
84 Sound.Stop();
85 }
86 }
87
88 public void Pause()
89 {
90 if (Sound != null && Sound.State == SoundState.Playing)
91 {
92 Sound.Pause();
93 }
94 }
95
96 public void Resume()
97 {
98 if (Sound != null && Sound.State == SoundState.Paused)
99 {
100 Sound.Resume();
101 }
102 }
103
104 public void Update()
105 {
106 if (Sound == null)
107 {
108 return;
109 }
110 if (Condition != null && !Condition())
111 {
112 Sound.Stop(immediate: true);
113 return;
114 }
115 Vector2 value = Main.screenPosition + new Vector2(Main.screenWidth / 2, Main.screenHeight / 2);
116 float num = 1f;
117 if (!IsGlobal)
118 {
119 float value2 = (Position.X - value.X) / ((float)Main.screenWidth * 0.5f);
120 value2 = MathHelper.Clamp(value2, -1f, 1f);
121 Sound.Pan = value2;
122 float num2 = Vector2.Distance(Position, value);
123 num = 1f - num2 / ((float)Main.screenWidth * 1.5f);
124 }
125 num *= Style.Volume * Volume;
126 switch (Style.Type)
127 {
128 case SoundType.Sound:
129 num *= Main.soundVolume;
130 break;
131 case SoundType.Ambient:
132 num *= Main.ambientVolume;
133 break;
134 case SoundType.Music:
135 num *= Main.musicVolume;
136 break;
137 }
138 num = MathHelper.Clamp(num, 0f, 1f);
139 Sound.Volume = num;
140 Sound.Pitch = Pitch;
141 }
142}
static float Clamp(float value, float min, float max)
Definition MathHelper.cs:46
void PlayLooped(LoopedPlayCondition condition)
SoundEffectInstance Sound
ActiveSound(SoundStyle style, Vector2 position)
ActiveSound(SoundStyle style, Vector2 position, LoopedPlayCondition condition)
ActiveSound(SoundStyle style)
LoopedPlayCondition Condition
delegate bool LoopedPlayCondition()
SoundEffect GetRandomSound()
static float ambientVolume
Definition Main.cs:1423
static int screenHeight
Definition Main.cs:1721
static int screenWidth
Definition Main.cs:1719
static float musicVolume
Definition Main.cs:1421
static float soundVolume
Definition Main.cs:1425
static float Distance(Vector2 value1, Vector2 value2)
Definition Vector2.cs:91