Merge pull request #12919 from obrakmann/transform-refactor

Refactor Transform to make use of child activities
This commit is contained in:
reaperrr
2017-05-04 18:55:41 +02:00
committed by GitHub
14 changed files with 86 additions and 64 deletions

View File

@@ -201,7 +201,7 @@ namespace OpenRA.Activities
/// </summary> /// </summary>
protected virtual void OnLastRun(Actor self) { } protected virtual void OnLastRun(Actor self) { }
public virtual bool Cancel(Actor self) public virtual bool Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsInterruptible) if (!IsInterruptible)
return false; return false;
@@ -209,9 +209,11 @@ namespace OpenRA.Activities
if (ChildActivity != null && !ChildActivity.Cancel(self)) if (ChildActivity != null && !ChildActivity.Cancel(self))
return false; return false;
State = ActivityState.Canceled; if (!keepQueue)
NextActivity = null; NextActivity = null;
ChildActivity = null; ChildActivity = null;
State = ActivityState.Canceled;
return true; return true;
} }

View File

@@ -207,7 +207,7 @@ namespace OpenRA.Mods.Common.Activities
}); });
} }
public override bool Cancel(Actor self) public override bool Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsCanceled && innerActivity != null && !innerActivity.Cancel(self)) if (!IsCanceled && innerActivity != null && !innerActivity.Cancel(self))
return false; return false;

View File

@@ -112,7 +112,7 @@ namespace OpenRA.Mods.Common.Activities
inner.Cancel(self); inner.Cancel(self);
} }
public override bool Cancel(Actor self) public override bool Cancel(Actor self, bool keepQueue = false)
{ {
AbortOrExit(self); AbortOrExit(self);
if (nextState < EnterState.Exiting) if (nextState < EnterState.Exiting)

View File

@@ -81,7 +81,7 @@ namespace OpenRA.Mods.Common.Activities
throw new InvalidOperationException("Invalid harvester dock state"); throw new InvalidOperationException("Invalid harvester dock state");
} }
public override bool Cancel(Actor self) public override bool Cancel(Actor self, bool keepQueue = false)
{ {
dockingState = DockingState.Undock; dockingState = DockingState.Undock;
return base.Cancel(self); return base.Cancel(self);

View File

@@ -46,12 +46,12 @@ namespace OpenRA.Mods.Common.Activities
return this; return this;
} }
public override bool Cancel(Actor self) public override bool Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsCanceled && inner != null && !inner.Cancel(self)) if (!IsCanceled && inner != null && !inner.Cancel(self))
return false; return false;
return base.Cancel(self); return base.Cancel(self, keepQueue);
} }
public override IEnumerable<Target> GetTargets(Actor self) public override IEnumerable<Target> GetTargets(Actor self)

View File

@@ -334,9 +334,9 @@ namespace OpenRA.Mods.Common.Activities
} }
} }
public override bool Cancel(Actor self) public override bool Cancel(Actor self, bool keepQueue = false)
{ {
Move.Cancel(self); Move.Cancel(self, keepQueue);
return base.Cancel(self); return base.Cancel(self);
} }

View File

@@ -161,7 +161,7 @@ namespace OpenRA.Mods.Common.Activities
return Target.None; return Target.None;
} }
public override bool Cancel(Actor self) public override bool Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsCanceled && inner != null && !inner.Cancel(self)) if (!IsCanceled && inner != null && !inner.Cancel(self))
return false; return false;

View File

@@ -157,12 +157,12 @@ namespace OpenRA.Mods.Common.Activities
}); });
} }
public override bool Cancel(Actor self) public override bool Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsCanceled && innerActivity != null && !innerActivity.Cancel(self)) if (!IsCanceled && innerActivity != null && !innerActivity.Cancel(self))
return false; return false;
return base.Cancel(self); return base.Cancel(self, keepQueue);
} }
} }
} }

View File

@@ -12,7 +12,9 @@
using System.Linq; using System.Linq;
using OpenRA.Activities; using OpenRA.Activities;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities namespace OpenRA.Mods.Common.Activities
{ {
@@ -32,11 +34,61 @@ namespace OpenRA.Mods.Common.Activities
ToActor = toActor; ToActor = toActor;
} }
protected override void OnFirstRun(Actor self)
{
if (self.Info.HasTraitInfo<IFacingInfo>())
QueueChild(new Turn(self, Facing));
if (self.Info.HasTraitInfo<AircraftInfo>())
QueueChild(new HeliLand(self, true));
}
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (IsCanceled) if (IsCanceled)
return NextActivity; return NextActivity;
if (ChildActivity != null)
{
ActivityUtils.RunActivity(self, ChildActivity);
return this;
}
// Prevent deployment in bogus locations
var transforms = self.TraitOrDefault<Transforms>();
var building = self.TraitOrDefault<Building>();
if ((transforms != null && !transforms.CanDeploy()) || (building != null && !building.Lock()))
{
Cancel(self, true);
return NextActivity;
}
foreach (var nt in self.TraitsImplementing<INotifyTransform>())
nt.BeforeTransform(self);
var makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
if (makeAnimation != null)
{
// Once the make animation starts the activity must not be stopped anymore.
IsInterruptible = false;
// Wait forever
QueueChild(new WaitFor(() => false));
makeAnimation.Reverse(self, () => DoTransform(self));
return this;
}
return NextActivity;
}
protected override void OnLastRun(Actor self)
{
if (!IsCanceled)
DoTransform(self);
}
void DoTransform(Actor self)
{
self.World.AddFrameEndTask(w => self.World.AddFrameEndTask(w =>
{ {
if (self.IsDead) if (self.IsDead)
@@ -84,11 +136,10 @@ namespace OpenRA.Mods.Common.Activities
if (selected) if (selected)
w.Selection.Add(w, a); w.Selection.Add(w, a);
if (controlgroup.HasValue) if (controlgroup.HasValue)
w.Selection.AddToControlGroup(a, controlgroup.Value); w.Selection.AddToControlGroup(a, controlgroup.Value);
}); });
return this;
} }
} }
} }

View File

@@ -30,9 +30,9 @@ namespace OpenRA.Mods.Common.Activities
return (remainingTicks-- == 0) ? NextActivity : this; return (remainingTicks-- == 0) ? NextActivity : this;
} }
public override bool Cancel(Actor self) public override bool Cancel(Actor self, bool keepQueue = false)
{ {
if (!base.Cancel(self)) if (!base.Cancel(self, keepQueue))
return false; return false;
remainingTicks = 0; remainingTicks = 0;
@@ -56,9 +56,9 @@ namespace OpenRA.Mods.Common.Activities
return (f == null || f()) ? NextActivity : this; return (f == null || f()) ? NextActivity : this;
} }
public override bool Cancel(Actor self) public override bool Cancel(Actor self, bool keepQueue = false)
{ {
if (!base.Cancel(self)) if (!base.Cancel(self, keepQueue))
return false; return false;
f = null; f = null;

View File

@@ -41,12 +41,12 @@ namespace OpenRA.Mods.Common.Activities
return this; return this;
} }
public override bool Cancel(Actor self) public override bool Cancel(Actor self, bool keepQueue = false)
{ {
if (!IsCanceled && inner != null && !inner.Cancel(self)) if (!IsCanceled && inner != null && !inner.Cancel(self))
return false; return false;
return base.Cancel(self); return base.Cancel(self, keepQueue);
} }
} }
} }

View File

@@ -43,9 +43,9 @@ namespace OpenRA.Mods.Common.Activities
return NextActivity; return NextActivity;
} }
public override bool Cancel(Actor self) public override bool Cancel(Actor self, bool keepQueue = false)
{ {
if (!base.Cancel(self)) if (!base.Cancel(self, keepQueue))
return false; return false;
Dispose(); Dispose();

View File

@@ -51,7 +51,7 @@ namespace OpenRA.Mods.Common.Traits.Render
Forward(self, () => building.NotifyBuildingComplete(self)); Forward(self, () => building.NotifyBuildingComplete(self));
} }
void Forward(Actor self, Action onComplete) public void Forward(Actor self, Action onComplete)
{ {
if (conditionManager != null && !string.IsNullOrEmpty(info.Condition) && token == ConditionManager.InvalidConditionToken) if (conditionManager != null && !string.IsNullOrEmpty(info.Condition) && token == ConditionManager.InvalidConditionToken)
token = conditionManager.GrantCondition(self, info.Condition); token = conditionManager.GrantCondition(self, info.Condition);
@@ -66,7 +66,7 @@ namespace OpenRA.Mods.Common.Traits.Render
}); });
} }
void Reverse(Actor self, Action onComplete) public void Reverse(Actor self, Action onComplete)
{ {
if (conditionManager != null && !string.IsNullOrEmpty(info.Condition) && token == ConditionManager.InvalidConditionToken) if (conditionManager != null && !string.IsNullOrEmpty(info.Condition) && token == ConditionManager.InvalidConditionToken)
token = conditionManager.GrantCondition(self, info.Condition); token = conditionManager.GrantCondition(self, info.Condition);

View File

@@ -10,10 +10,8 @@
#endregion #endregion
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.Activities;
using OpenRA.Mods.Common.Activities; using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Orders; using OpenRA.Mods.Common.Orders;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
@@ -73,7 +71,7 @@ namespace OpenRA.Mods.Common.Traits
return (order.OrderString == "DeployTransform") ? info.Voice : null; return (order.OrderString == "DeployTransform") ? info.Voice : null;
} }
bool CanDeploy() public bool CanDeploy()
{ {
var building = self.TraitOrDefault<Building>(); var building = self.TraitOrDefault<Building>();
if (building != null && building.Locked) if (building != null && building.Locked)
@@ -113,43 +111,14 @@ namespace OpenRA.Mods.Common.Traits
if (!queued) if (!queued)
self.CancelActivity(); self.CancelActivity();
if (self.Info.HasTraitInfo<IFacingInfo>()) self.QueueActivity(new Transform(self, info.IntoActor)
self.QueueActivity(new Turn(self, info.Facing));
if (self.Info.HasTraitInfo<AircraftInfo>())
self.QueueActivity(new HeliLand(self, true));
self.QueueActivity(new CallFunc(() =>
{ {
// Prevent deployment in bogus locations Offset = info.Offset,
var building = self.TraitOrDefault<Building>(); Facing = info.Facing,
if (!CanDeploy() || (building != null && !building.Lock())) Sounds = info.TransformSounds,
return; Notification = info.TransformNotification,
Faction = faction
foreach (var nt in self.TraitsImplementing<INotifyTransform>()) });
nt.BeforeTransform(self);
var transform = new Transform(self, info.IntoActor)
{
Offset = info.Offset,
Facing = info.Facing,
Sounds = info.TransformSounds,
Notification = info.TransformNotification,
Faction = faction
};
// Try and stop the actor from doing anything between the sanity checks above
// and the actual transform, which we're about to queue.
// TODO: The proper way to do this is to write all the transform code as a nested activity.
if (self.CurrentActivity.NextInQueue != null)
self.CurrentActivity.NextInQueue.Cancel(self);
var makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
if (makeAnimation != null)
makeAnimation.Reverse(self, transform);
else
self.QueueActivity(transform);
}));
} }
public void ResolveOrder(Actor self, Order order) public void ResolveOrder(Actor self, Order order)