From ea2e452075fb2436385711272b4c7eba55595043 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Sun, 6 Jan 2019 07:40:41 +0100 Subject: [PATCH] Add SpawnActorEffect And use it to drop DelayedAction from ReinforcementsGlobal. --- .../Effects/SpawnActorEffect.cs | 64 +++++++++++++++++++ OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 + .../Scripting/Global/ReinforcementsGlobal.cs | 28 ++++---- 3 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 OpenRA.Mods.Common/Effects/SpawnActorEffect.cs diff --git a/OpenRA.Mods.Common/Effects/SpawnActorEffect.cs b/OpenRA.Mods.Common/Effects/SpawnActorEffect.cs new file mode 100644 index 0000000000..14ca88afda --- /dev/null +++ b/OpenRA.Mods.Common/Effects/SpawnActorEffect.cs @@ -0,0 +1,64 @@ +#region Copyright & License Information +/* + * Copyright 2007-2019 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; +using System.Collections.Generic; +using System.Linq; +using OpenRA.Activities; +using OpenRA.Effects; +using OpenRA.Graphics; +using OpenRA.Mods.Common.Traits; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Effects +{ + public class SpawnActorEffect : IEffect + { + readonly Actor actor; + readonly CPos[] pathAfterSpawn; + readonly Activity activityAtDestination; + readonly IMove move; + int remainingDelay; + + public SpawnActorEffect(Actor actor) + : this(actor, 0, new CPos[0], null) { } + + public SpawnActorEffect(Actor actor, int delay) + : this(actor, delay, new CPos[0], null) { } + + public SpawnActorEffect(Actor actor, int delay, CPos[] pathAfterSpawn, Activity activityAtDestination) + { + this.actor = actor; + remainingDelay = delay; + this.pathAfterSpawn = pathAfterSpawn; + this.activityAtDestination = activityAtDestination; + move = actor.TraitOrDefault(); + } + + public void Tick(World world) + { + if (remainingDelay-- > 0) + return; + + world.Add(actor); + if (move != null) + for (var j = 0; j < pathAfterSpawn.Length; j++) + actor.QueueActivity(move.MoveTo(pathAfterSpawn[j], 2)); + + if (activityAtDestination != null) + actor.QueueActivity(activityAtDestination); + + world.AddFrameEndTask(w => w.Remove(this)); + } + + public IEnumerable Render(WorldRenderer wr) { return SpriteRenderable.None; } + } +} diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 5097dab1a7..fe9111745e 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -149,6 +149,7 @@ + diff --git a/OpenRA.Mods.Common/Scripting/Global/ReinforcementsGlobal.cs b/OpenRA.Mods.Common/Scripting/Global/ReinforcementsGlobal.cs index bd78908b89..89e0b68a79 100644 --- a/OpenRA.Mods.Common/Scripting/Global/ReinforcementsGlobal.cs +++ b/OpenRA.Mods.Common/Scripting/Global/ReinforcementsGlobal.cs @@ -16,6 +16,7 @@ using Eluant; using OpenRA.Activities; using OpenRA.Effects; using OpenRA.Mods.Common.Activities; +using OpenRA.Mods.Common.Effects; using OpenRA.Mods.Common.Traits; using OpenRA.Primitives; using OpenRA.Scripting; @@ -84,24 +85,21 @@ namespace OpenRA.Mods.Common.Scripting actors.Add(actor); var actionDelay = i * interval; - Action actorAction = () => + Activity queuedActivity = null; + if (af != null) { - Context.World.Add(actor); - for (var j = 1; j < entryPath.Length; j++) - Move(actor, entryPath[j]); - - if (af != null) + queuedActivity = new CallFunc(() => { - actor.QueueActivity(new CallFunc(() => - { - using (af) - using (var a = actor.ToLuaValue(Context)) - af.Call(a); - })); - } - }; + using (af) + using (var a = actor.ToLuaValue(Context)) + af.Call(a); + }); + } - Context.World.AddFrameEndTask(w => w.Add(new DelayedAction(actionDelay, actorAction))); + // We need to exclude the spawn location from the movement path + var path = entryPath.Skip(1).ToArray(); + + Context.World.AddFrameEndTask(w => w.Add(new SpawnActorEffect(actor, actionDelay, path, queuedActivity))); } return actors.ToArray();