Add support for non-overlapping sound notifications

This commit is contained in:
Ivaylo Draganov
2021-06-24 15:43:04 +03:00
committed by abcdefg30
parent 137d384304
commit fa6ff32f65
6 changed files with 47 additions and 10 deletions

View File

@@ -33,11 +33,11 @@ namespace OpenRA.GameRules
{
FieldLoader.Load(this, y);
VoicePools = Exts.Lazy(() => Voices.ToDictionary(a => a.Key, a => new SoundPool(1f, a.Value)));
VoicePools = Exts.Lazy(() => Voices.ToDictionary(a => a.Key, a => new SoundPool(1f, false, a.Value)));
NotificationsPools = Exts.Lazy(() => ParseSoundPool(y, "Notifications"));
}
Dictionary<string, SoundPool> ParseSoundPool(MiniYaml y, string key)
static Dictionary<string, SoundPool> ParseSoundPool(MiniYaml y, string key)
{
var ret = new Dictionary<string, SoundPool>();
var classifiction = y.Nodes.First(x => x.Key == key);
@@ -48,8 +48,13 @@ namespace OpenRA.GameRules
if (volumeModifierNode != null)
volumeModifier = FieldLoader.GetValue<float>(volumeModifierNode.Key, volumeModifierNode.Value.Value);
var allowInterrupt = false;
var allowInterruptNode = t.Value.Nodes.FirstOrDefault(x => x.Key == "AllowInterrupt");
if (allowInterruptNode != null)
allowInterrupt = FieldLoader.GetValue<bool>(allowInterruptNode.Key, allowInterruptNode.Value.Value);
var names = FieldLoader.GetValue<string[]>(t.Key, t.Value.Value);
var sp = new SoundPool(volumeModifier, names);
var sp = new SoundPool(volumeModifier, allowInterrupt, names);
ret.Add(t.Key, sp);
}
@@ -60,12 +65,14 @@ namespace OpenRA.GameRules
public class SoundPool
{
public readonly float VolumeModifier;
public readonly bool AllowInterrupt;
readonly string[] clips;
readonly List<string> liveclips = new List<string>();
public SoundPool(float volumeModifier, params string[] clips)
public SoundPool(float volumeModifier, bool allowInterrupt, params string[] clips)
{
VolumeModifier = volumeModifier;
AllowInterrupt = allowInterrupt;
this.clips = clips;
}

View File

@@ -45,6 +45,7 @@ namespace OpenRA
ISound video;
MusicInfo currentMusic;
Dictionary<uint, ISound> currentSounds = new Dictionary<uint, ISound>();
Dictionary<string, ISound> currentNotifications = new Dictionary<string, ISound>();
public bool DummyEngine { get; private set; }
public Sound(IPlatform platform, SoundSettings soundSettings)
@@ -390,16 +391,29 @@ namespace OpenRA
if (!string.IsNullOrEmpty(name) && (p == null || p == p.World.LocalPlayer))
{
if (currentNotifications.ContainsKey(name) && !currentNotifications[name].Complete)
{
if (pool.AllowInterrupt)
soundEngine.StopSound(currentNotifications[name]);
else
return false;
}
else if (currentSounds.ContainsKey(id) && !currentSounds[id].Complete)
{
if (pool.AllowInterrupt)
soundEngine.StopSound(currentSounds[id]);
else
return false;
}
var sound = soundEngine.Play2D(sounds[name],
false, relative, pos,
InternalSoundVolume * volumeModifier * pool.VolumeModifier, attenuateVolume);
if (id != 0)
{
if (currentSounds.ContainsKey(id))
soundEngine.StopSound(currentSounds[id]);
if (id != 0)
currentSounds[id] = sound;
}
else
currentNotifications[name] = sound;
}
return true;