Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SubsystemAudio.cs
浏览该文件的文档.
1using Engine;
2using Engine.Audio;
6
7namespace Game {
9 public class Congestion {
10 public double LastUpdateTime;
11
12 public double LastPlayedTime;
13
14 public float LastPlayedVolume;
15
16 public float Value;
17 }
18
19 public struct SoundInfo {
20 public double Time;
21
22 public string Name;
23
24 public float Volume;
25
26 public float Pitch;
27
28 public float Pan;
29
31
32 public SoundInfo() { }
33 }
34
36
38
39 public Random m_random = new();
40
41 public List<Vector3> m_listenerPositions = [];
42
43 public Dictionary<string, Congestion> m_congestions = [];
44
45 public double m_nextSoundTime;
46
47 public List<SoundInfo> m_queuedSounds = [];
48
49 public List<Sound> m_sounds = [];
50
51 public Dictionary<Sound, bool> m_mutedSounds = [];
52
53 public ReadOnlyList<Vector3> ListenerPositions => new(m_listenerPositions);
54
56
58 float num = float.MaxValue;
59 for (int i = 0; i < m_listenerPositions.Count; i++) {
61 if (num2 < num) {
62 num = num2;
63 }
64 }
65 return num;
66 }
67
69
70 public void Mute() {
71 foreach (Sound sound in m_sounds) {
72 if (sound.State == SoundState.Playing) {
73 m_mutedSounds[sound] = true;
74 sound.Pause();
75 }
76 }
77 }
78
79 public void Unmute() {
80 foreach (Sound key in m_mutedSounds.Keys) {
81 key.Play();
82 }
83 m_mutedSounds.Clear();
84 }
85
86 public void PlaySound(string name, float volume, float pitch, float pan, float delay) {
87 double num = m_subsystemTime.GameTime + delay;
88 m_nextSoundTime = Math.Min(m_nextSoundTime, num);
89 m_queuedSounds.Add(new SoundInfo { Time = num, Name = name, Volume = volume, Pitch = pitch, Pan = pan });
90 }
91
92 public void PlaySound(string name, float volume, float pitch, float pan, float delay, Vector3 direction) {
93 double num = m_subsystemTime.GameTime + delay;
94 m_nextSoundTime = Math.Min(m_nextSoundTime, num);
95 m_queuedSounds.Add(new SoundInfo { Time = num, Name = name, Volume = volume, Pitch = pitch, Pan = pan, direction = direction });
96 }
97
98 public virtual void PlaySound(string name, float volume, float pitch, Vector3 position, float minDistance, float delay) {
99 float num = CalculateVolume(CalculateListenerDistance(position), minDistance);
100 PlaySound(name, volume * num, pitch, 0f, delay);
101 }
102
103 public virtual void PlaySound(string name, float volume, float pitch, Vector3 position, float minDistance, bool autoDelay) {
104 float num = CalculateVolume(CalculateListenerDistance(position), minDistance);
105 PlaySound(name, volume * num, pitch, 0f, autoDelay ? CalculateDelay(position) : 0f);
106 }
107
108 public void PlayRandomSound(string directory, float volume, float pitch, float pan, float delay) {
109 ReadOnlyList<ContentInfo> readOnlyList = ContentManager.List(directory);
110 if (readOnlyList.Count > 0) {
111 int index = m_random.Int(0, readOnlyList.Count - 1);
112 PlaySound(readOnlyList[index].ContentPath, volume, pitch, pan, delay);
113 }
114 else {
115 Log.Warning("Sounds directory \"{0}\" not found or empty.", directory);
116 }
117 }
118
119 public virtual void PlayRandomSound(string directory, float volume, float pitch, Vector3 position, float minDistance, float delay) {
120 float num = CalculateVolume(CalculateListenerDistance(position), minDistance);
121 PlayRandomSound(directory, volume * num, pitch, 0f, delay);
122 }
123
124 public virtual void PlayRandomSound(string directory, float volume, float pitch, Vector3 position, float minDistance, bool autoDelay) {
125 float num = CalculateVolume(CalculateListenerDistance(position), minDistance);
126 PlayRandomSound(directory, volume * num, pitch, 0f, autoDelay ? CalculateDelay(position) : 0f);
127 }
128
129 public Sound CreateSound(string name) {
130 Sound sound = new(ContentManager.Get<SoundBuffer>(name));
131 m_sounds.Add(sound);
132 return sound;
133 }
134
135 public float CalculateVolume(float distance, float minDistance, float rolloffFactor = 2f) => distance > minDistance
136 ? minDistance / (minDistance + Math.Max(rolloffFactor * (distance - minDistance), 0f))
137 : 1f;
138
140
141 public float CalculateDelay(float distance) => Math.Min(distance / 120f, 3f);
142
143 public virtual void Update(float dt) {
144 m_listenerPositions.Clear();
145 foreach (GameWidget gameWidget in m_subsystemViews.GameWidgets) {
147 }
148 if (!(m_subsystemTime.GameTime >= m_nextSoundTime)) {
149 return;
150 }
151 m_nextSoundTime = double.MaxValue;
152 int num = 0;
153 while (num < m_queuedSounds.Count) {
154 SoundInfo soundInfo = m_queuedSounds[num];
155 if (m_subsystemTime.GameTime >= soundInfo.Time) {
156 if (m_subsystemTime.GameTimeFactor == 1f
157 && !m_subsystemTime.FixedTimeStep.HasValue
159 && UpdateCongestion(soundInfo.Name, soundInfo.Volume)) {
160 AudioManager.PlaySound(soundInfo.Name, soundInfo.Volume, soundInfo.Pitch, soundInfo.Pan, soundInfo.direction);
161 }
162 m_queuedSounds.RemoveAt(num);
163 }
164 else {
165 m_nextSoundTime = Math.Min(m_nextSoundTime, soundInfo.Time);
166 num++;
167 }
168 }
169 }
170
171 public override void Load(ValuesDictionary valuesDictionary) {
172 m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
173 m_subsystemViews = Project.FindSubsystem<SubsystemGameWidgets>(true);
174 }
175
176 public override void Dispose() {
177 foreach (Sound sound in m_sounds) {
178 sound.Dispose();
179 }
180 }
181
182 public bool UpdateCongestion(string name, float volume) {
183 if (!m_congestions.TryGetValue(name, out Congestion value)) {
184 value = new Congestion();
185 m_congestions.Add(name, value);
186 }
187 double realTime = Time.RealTime;
188 double lastUpdateTime = value.LastUpdateTime;
189 double lastPlayedTime = value.LastPlayedTime;
190 float num = lastUpdateTime > 0.0 ? (float)(realTime - lastUpdateTime) : 0f;
191 value.Value = Math.Max(value.Value - 10f * num, 0f);
192 value.LastUpdateTime = realTime;
193 if (value.Value <= 6f
194 && (lastPlayedTime == 0.0 || volume > value.LastPlayedVolume || realTime - lastPlayedTime >= 0.0)) {
195 value.LastPlayedTime = realTime;
196 value.LastPlayedVolume = volume;
197 value.Value += 1f;
198 return true;
199 }
200 return false;
201 }
202 }
203}
override void Dispose()
static void Warning(object message)
定义 Log.cs:68
static double RealTime
定义 Time.cs:38
static float MinAudibleVolume
static void PlaySound(string name, float volume, float pitch, float pan)
Vector3 ViewPosition
static object Get(Type type, string name)
static ReadOnlyList< ContentInfo > List()
float CalculateDelay(Vector3 position)
virtual void PlaySound(string name, float volume, float pitch, Vector3 position, float minDistance, bool autoDelay)
Sound CreateSound(string name)
void PlayRandomSound(string directory, float volume, float pitch, float pan, float delay)
List< Vector3 > m_listenerPositions
void PlaySound(string name, float volume, float pitch, float pan, float delay, Vector3 direction)
float CalculateListenerDistanceSquared(Vector3 p)
virtual void Update(float dt)
SubsystemGameWidgets m_subsystemViews
float CalculateListenerDistance(Vector3 p)
ReadOnlyList< Vector3 > ListenerPositions
float CalculateVolume(float distance, float minDistance, float rolloffFactor=2f)
Dictionary< string, Congestion > m_congestions
virtual void PlayRandomSound(string directory, float volume, float pitch, Vector3 position, float minDistance, bool autoDelay)
virtual void PlaySound(string name, float volume, float pitch, Vector3 position, float minDistance, float delay)
void PlaySound(string name, float volume, float pitch, float pan, float delay)
bool UpdateCongestion(string name, float volume)
override void Load(ValuesDictionary valuesDictionary)
virtual void PlayRandomSound(string directory, float volume, float pitch, Vector3 position, float minDistance, float delay)
float CalculateDelay(float distance)
Dictionary< Sound, bool > m_mutedSounds
List< SoundInfo > m_queuedSounds
ValuesDictionary ValuesDictionary
Camera ActiveCamera
static float DistanceSquared(Vector3 v1, Vector3 v2)
static readonly Vector3 Zero