diff --git a/OpenRA.Mods.Common/Activities/DeployForGrantedCondition.cs b/OpenRA.Mods.Common/Activities/DeployForGrantedCondition.cs new file mode 100644 index 0000000000..5a1899995f --- /dev/null +++ b/OpenRA.Mods.Common/Activities/DeployForGrantedCondition.cs @@ -0,0 +1,69 @@ +#region Copyright & License Information +/* + * Copyright 2007-2017 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 OpenRA.Activities; +using OpenRA.Mods.Common.Traits; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Activities +{ + public class DeployForGrantedCondition : Activity + { + readonly GrantConditionOnDeploy deploy; + readonly int facing; + readonly bool canTurn; + + public DeployForGrantedCondition(Actor self) : base() + { + canTurn = self.Info.HasTraitInfo(); + facing = self.Info.TraitInfo().Facing; + deploy = self.Trait(); + } + + protected override void OnFirstRun(Actor self) + { + // Turn to the required facing. + if (facing != -1 && canTurn) + QueueChild(new Turn(self, facing)); + } + + public override Activity Tick(Actor self) + { + // Do turn first, if needed. + if (ChildActivity != null) + { + ChildActivity = ActivityUtils.RunActivity(self, ChildActivity); + return this; + } + + // Without this, turn for facing deploy angle will be canceled and immediately deploy! + if (IsCanceled) + return NextActivity; + + if (IsInterruptible) + { + IsInterruptible = false; // must DEPLOY from now. + deploy.Deploy(); + return this; + } + + // Wait for deployment + if (deploy.DeployState == DeployState.Deploying) + return this; + + // Failed or success, we are going to NextActivity. + // Deploy() at the first run would have put DeployState == Deploying so + // if we are back to DeployState.Undeployed, it means deploy failure. + // Parent activity will see the status and will take appropriate action. + return NextActivity; + } + } +} diff --git a/OpenRA.Mods.Common/Activities/UndeployForGrantedCondition.cs b/OpenRA.Mods.Common/Activities/UndeployForGrantedCondition.cs new file mode 100644 index 0000000000..29ffeb186f --- /dev/null +++ b/OpenRA.Mods.Common/Activities/UndeployForGrantedCondition.cs @@ -0,0 +1,39 @@ +#region Copyright & License Information +/* + * Copyright 2007-2017 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 OpenRA.Activities; +using OpenRA.Mods.Common.Traits; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Activities +{ + public class UndeployForGrantedCondition : Activity + { + readonly GrantConditionOnDeploy deploy; + + public UndeployForGrantedCondition(Actor self) : base() + { + deploy = self.Trait(); + } + + public override Activity Tick(Actor self) + { + IsInterruptible = false; // must DEPLOY from now. + deploy.Undeploy(); + + // Wait for deployment + if (deploy.DeployState == DeployState.Undeploying) + return this; + + return NextActivity; + } + } +} diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 91715fca31..06b487b7e5 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -92,6 +92,7 @@ + @@ -117,6 +118,7 @@ + diff --git a/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnDeploy.cs b/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnDeploy.cs index a95b3898d2..010c4d83fc 100644 --- a/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnDeploy.cs +++ b/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnDeploy.cs @@ -73,6 +73,8 @@ namespace OpenRA.Mods.Common.Traits int deployedToken = ConditionManager.InvalidConditionToken; int undeployedToken = ConditionManager.InvalidConditionToken; + public DeployState DeployState { get { return deployState; } } + public GrantConditionOnDeploy(ActorInitializer init, GrantConditionOnDeployInfo info) { self = init.Self; @@ -142,17 +144,9 @@ namespace OpenRA.Mods.Common.Traits self.CancelActivity(); if (deployState == DeployState.Deployed && info.CanUndeploy) - { - self.QueueActivity(new CallFunc(Undeploy)); - } + self.QueueActivity(new UndeployForGrantedCondition(self)); else if (deployState == DeployState.Undeployed) - { - // Turn to the required facing. - if (info.Facing != -1 && canTurn) - self.QueueActivity(new Turn(self, info.Facing)); - - self.QueueActivity(new CallFunc(Deploy)); - } + self.QueueActivity(new DeployForGrantedCondition(self)); } bool IsCursorBlocked() @@ -206,7 +200,7 @@ namespace OpenRA.Mods.Common.Traits } /// Play deploy sound and animation. - void Deploy() { Deploy(false); } + public void Deploy() { Deploy(false); } void Deploy(bool init) { // Something went wrong, most likely due to deploy order spam and the fact that this is a delayed action. @@ -233,7 +227,7 @@ namespace OpenRA.Mods.Common.Traits } /// Play undeploy sound and animation and after that revoke the condition. - void Undeploy() { Undeploy(false); } + public void Undeploy() { Undeploy(false); } void Undeploy(bool init) { // Something went wrong, most likely due to deploy order spam and the fact that this is a delayed action.