From c2d6b78b185022659071c0fabd6896f9fe9a5b31 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Sun, 7 Oct 2018 17:23:07 +0200 Subject: [PATCH] Add dynamic position support to SpriteEffect This was the last missing 'puzzle piece' to replace some remaining spcial-case effects. --- OpenRA.Mods.Common/Effects/SpriteEffect.cs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/OpenRA.Mods.Common/Effects/SpriteEffect.cs b/OpenRA.Mods.Common/Effects/SpriteEffect.cs index 0f28ba89f6..c7df32152f 100644 --- a/OpenRA.Mods.Common/Effects/SpriteEffect.cs +++ b/OpenRA.Mods.Common/Effects/SpriteEffect.cs @@ -9,6 +9,7 @@ */ #endregion +using System; using System.Collections.Generic; using OpenRA.Effects; using OpenRA.Graphics; @@ -20,18 +21,29 @@ namespace OpenRA.Mods.Common.Effects readonly World world; readonly string palette; readonly Animation anim; - readonly WPos pos; + readonly Func posFunc; readonly bool visibleThroughFog; readonly bool scaleSizeWithZoom; + WPos pos; + // 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, bool scaleSizeWithZoom = false, int facing = 0) + : this(() => pos, () => facing, world, image, sequence, palette, visibleThroughFog, scaleSizeWithZoom) { } + + public SpriteEffect(Actor actor, World world, string image, string sequence, string palette, bool visibleThroughFog = false, bool scaleSizeWithZoom = false, int facing = 0) + : this(() => actor.CenterPosition, () => facing, world, image, sequence, palette, visibleThroughFog, scaleSizeWithZoom) { } + + public SpriteEffect(Func posFunc, Func facingFunc, World world, string image, string sequence, string palette, + bool visibleThroughFog = false, bool scaleSizeWithZoom = false) { this.world = world; - this.pos = pos; + this.posFunc = posFunc; this.palette = palette; this.scaleSizeWithZoom = scaleSizeWithZoom; this.visibleThroughFog = visibleThroughFog; - anim = new Animation(world, image, () => facing); + 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); } @@ -39,6 +51,8 @@ namespace OpenRA.Mods.Common.Effects public void Tick(World world) { anim.Tick(); + + pos = posFunc(); world.ScreenMap.Update(this, pos, anim.Image); }