From c55c65f6d763cfe212fb35d699aa1af1ec39e434 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Sun, 11 Aug 2019 19:02:03 +0200 Subject: [PATCH] Add delay support to SpriteEffect --- OpenRA.Mods.Common/Effects/SpriteEffect.cs | 31 ++++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/OpenRA.Mods.Common/Effects/SpriteEffect.cs b/OpenRA.Mods.Common/Effects/SpriteEffect.cs index aa212b2a0c..958f1bac3f 100644 --- a/OpenRA.Mods.Common/Effects/SpriteEffect.cs +++ b/OpenRA.Mods.Common/Effects/SpriteEffect.cs @@ -23,31 +23,46 @@ namespace OpenRA.Mods.Common.Effects readonly Animation anim; readonly Func 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 posFunc, Func 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();