Overhaul AmbientSound trait.
This commit is contained in:
@@ -19,56 +19,90 @@ namespace OpenRA.Mods.Common.Traits.Sound
|
|||||||
[FieldLoader.Require]
|
[FieldLoader.Require]
|
||||||
public readonly string SoundFile = null;
|
public readonly string SoundFile = null;
|
||||||
|
|
||||||
[Desc("Interval between playing the sound (in ticks).")]
|
[Desc("Initial delay (in ticks) before playing the sound for the first time.",
|
||||||
public readonly int Interval = 0;
|
"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); }
|
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;
|
ISound currentSound;
|
||||||
bool wasDisabled = true;
|
WPos cachedPosition;
|
||||||
int interval;
|
int delay;
|
||||||
|
|
||||||
public AmbientSound(Actor self, AmbientSoundInfo info)
|
public AmbientSound(Actor self, AmbientSoundInfo info)
|
||||||
: base(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)
|
if (IsTraitDisabled)
|
||||||
{
|
|
||||||
Game.Sound.StopSound(currentSound);
|
|
||||||
currentSound = null;
|
|
||||||
wasDisabled = true;
|
|
||||||
return;
|
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)
|
StartSound(self);
|
||||||
currentSound = Game.Sound.PlayLooped(SoundType.World, Info.SoundFile, self.CenterPosition);
|
if (!loop)
|
||||||
else
|
delay = RandomDelay(self.World, Info.Interval);
|
||||||
currentSound = Game.Sound.PlayLooped(SoundType.World, Info.SoundFile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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(); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user