Overhaul AmbientSound trait.

This commit is contained in:
Paul Chote
2017-02-03 21:59:32 +00:00
parent a880562b4b
commit 647ee841b4

View File

@@ -19,56 +19,90 @@ namespace OpenRA.Mods.Common.Traits.Sound
[FieldLoader.Require]
public readonly string SoundFile = null;
[Desc("Interval between playing the sound (in ticks).")]
public readonly int Interval = 0;
[Desc("Initial delay (in ticks) before playing the sound for the first time.",
"Two values indicate a random delay range.")]
public readonly int[] Delay = { 0 };
[Desc("Interval between playing the sound (in ticks).",
"Two values indicate a random delay range.")]
public readonly int[] Interval = { 0 };
public override object Create(ActorInitializer init) { return new AmbientSound(init.Self, this); }
}
class AmbientSound : ConditionalTrait<AmbientSoundInfo>, ITick
class AmbientSound : ConditionalTrait<AmbientSoundInfo>, ITick, INotifyRemovedFromWorld
{
readonly bool loop;
ISound currentSound;
bool wasDisabled = true;
int interval;
WPos cachedPosition;
int delay;
public AmbientSound(Actor self, AmbientSoundInfo info)
: base(info)
{
interval = info.Interval;
delay = RandomDelay(self.World, info.Delay);
loop = Info.Interval.Length == 0 || (Info.Interval.Length == 1 && Info.Interval[0] == 0);
}
public void Tick(Actor self)
void ITick.Tick(Actor self)
{
if (IsTraitDisabled)
{
Game.Sound.StopSound(currentSound);
currentSound = null;
wasDisabled = true;
return;
var pos = self.CenterPosition;
if (currentSound != null && pos != cachedPosition)
{
currentSound.SetPosition(pos);
cachedPosition = pos;
}
if (wasDisabled && Info.Interval <= 0)
if (delay < 0)
return;
if (--delay < 0)
{
if (self.OccupiesSpace != null)
currentSound = Game.Sound.PlayLooped(SoundType.World, Info.SoundFile, self.CenterPosition);
else
currentSound = Game.Sound.PlayLooped(SoundType.World, Info.SoundFile);
StartSound(self);
if (!loop)
delay = RandomDelay(self.World, Info.Interval);
}
wasDisabled = false;
if (Info.Interval <= 0)
return;
if (interval-- > 0)
return;
interval = Info.Interval;
if (self.OccupiesSpace != null)
Game.Sound.Play(SoundType.World, Info.SoundFile, self.CenterPosition);
else
Game.Sound.Play(SoundType.World, Info.SoundFile);
}
void StartSound(Actor self)
{
if (self.OccupiesSpace != null)
{
cachedPosition = self.CenterPosition;
currentSound = loop ? Game.Sound.PlayLooped(SoundType.World, Info.SoundFile, cachedPosition) :
Game.Sound.Play(SoundType.World, Info.SoundFile, self.CenterPosition);
}
else
currentSound = loop ? Game.Sound.PlayLooped(SoundType.World, Info.SoundFile) :
Game.Sound.Play(SoundType.World, Info.SoundFile);
}
void StopSound()
{
if (currentSound == null)
return;
Game.Sound.StopSound(currentSound);
currentSound = null;
}
static int RandomDelay(World world, int[] range)
{
if (range.Length == 0)
return 0;
if (range.Length == 1)
return range[0];
return world.SharedRandom.Next(range[0], range[1]);
}
protected override void TraitEnabled(Actor self) { delay = RandomDelay(self.World, Info.Delay); }
protected override void TraitDisabled(Actor self) { StopSound(); }
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self) { StopSound(); }
}
}