Add delay support to SpriteEffect

This commit is contained in:
reaperrr
2019-08-11 19:02:03 +02:00
committed by Paul Chote
parent 037ce9ebf3
commit c55c65f6d7

View File

@@ -23,31 +23,46 @@ namespace OpenRA.Mods.Common.Effects
readonly Animation anim;
readonly Func<WPos> posFunc;
readonly bool visibleThroughFog;
readonly string sequence;
WPos pos;
int delay;
bool initialized;
// Facing is last on these overloads partially for backwards compatibility with previous main ctor revision
// and partially because most effects don't need it.
public SpriteEffect(WPos pos, World world, string image, string sequence, string palette, bool visibleThroughFog = false, int facing = 0)
: this(() => pos, () => facing, world, image, sequence, palette, visibleThroughFog) { }
// and partially because most effects don't need it. The latter is also the reason for placement of 'delay'.
public SpriteEffect(WPos pos, World world, string image, string sequence, string palette,
bool visibleThroughFog = false, int facing = 0, int delay = 0)
: this(() => pos, () => facing, world, image, sequence, palette, visibleThroughFog, delay) { }
public SpriteEffect(Actor actor, World world, string image, string sequence, string palette, bool visibleThroughFog = false, int facing = 0)
: this(() => actor.CenterPosition, () => facing, world, image, sequence, palette, visibleThroughFog) { }
public SpriteEffect(Actor actor, World world, string image, string sequence, string palette,
bool visibleThroughFog = false, int facing = 0, int delay = 0)
: this(() => actor.CenterPosition, () => facing, world, image, sequence, palette, visibleThroughFog, delay) { }
public SpriteEffect(Func<WPos> posFunc, Func<int> facingFunc, World world, string image, string sequence, string palette,
bool visibleThroughFog = false)
bool visibleThroughFog = false, int delay = 0)
{
this.world = world;
this.posFunc = posFunc;
this.palette = palette;
this.sequence = sequence;
this.visibleThroughFog = visibleThroughFog;
this.delay = delay;
pos = posFunc();
anim = new Animation(world, image, facingFunc);
anim.PlayThen(sequence, () => world.AddFrameEndTask(w => { w.Remove(this); w.ScreenMap.Remove(this); }));
world.ScreenMap.Add(this, pos, anim.Image);
}
public void Tick(World world)
{
if (delay-- > 0)
return;
if (!initialized)
{
anim.PlayThen(sequence, () => world.AddFrameEndTask(w => { w.Remove(this); w.ScreenMap.Remove(this); }));
world.ScreenMap.Add(this, pos, anim.Image);
initialized = true;
}
anim.Tick();
pos = posFunc();