Port Demolish to the new Enter activity.

This commit is contained in:
Paul Chote
2019-02-01 09:48:25 +00:00
committed by Oliver Brakmann
parent a17608a24e
commit 167371d540
3 changed files with 47 additions and 24 deletions

View File

@@ -9,55 +9,79 @@
*/ */
#endregion #endregion
using System;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.Mods.Common.Effects; using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities namespace OpenRA.Mods.Common.Activities
{ {
class Demolish : LegacyEnter class Demolish : Enter
{ {
readonly Actor target;
readonly IDemolishable[] demolishables;
readonly int delay; readonly int delay;
readonly int flashes; readonly int flashes;
readonly int flashesDelay; readonly int flashesDelay;
readonly int flashInterval; readonly int flashInterval;
readonly INotifyDemolition[] notifiers; readonly INotifyDemolition[] notifiers;
readonly EnterBehaviour enterBehaviour;
public Demolish(Actor self, Actor target, EnterBehaviour enterBehaviour, int delay, Actor enterActor;
IDemolishable[] enterDemolishables;
public Demolish(Actor self, Target target, EnterBehaviour enterBehaviour, int delay,
int flashes, int flashesDelay, int flashInterval) int flashes, int flashesDelay, int flashInterval)
: base(self, target, enterBehaviour, targetLineColor: Color.Red) : base(self, target, Color.Red)
{ {
this.target = target;
demolishables = target.TraitsImplementing<IDemolishable>().ToArray();
notifiers = self.TraitsImplementing<INotifyDemolition>().ToArray(); notifiers = self.TraitsImplementing<INotifyDemolition>().ToArray();
this.delay = delay; this.delay = delay;
this.flashes = flashes; this.flashes = flashes;
this.flashesDelay = flashesDelay; this.flashesDelay = flashesDelay;
this.flashInterval = flashInterval; this.flashInterval = flashInterval;
this.enterBehaviour = enterBehaviour;
} }
protected override bool CanReserve(Actor self) protected override bool TryStartEnter(Actor self, Actor targetActor)
{ {
return demolishables.Any(i => i.IsValidTarget(target, self)); enterActor = targetActor;
enterDemolishables = targetActor.TraitsImplementing<IDemolishable>().ToArray();
// Make sure we can still demolish the target before entering
// (but not before, because this may stop the actor in the middle of nowhere)
if (!enterDemolishables.Any(i => i.IsValidTarget(enterActor, self)))
{
Cancel(self, true);
return false;
}
return true;
} }
protected override void OnInside(Actor self) protected override void OnEnterComplete(Actor self, Actor targetActor)
{ {
self.World.AddFrameEndTask(w => self.World.AddFrameEndTask(w =>
{ {
if (target.IsDead) // Make sure the target hasn't changed while entering
// OnEnterComplete is only called if targetActor is alive
if (targetActor != enterActor)
return; return;
w.Add(new FlashTarget(target, count: flashes, delay: flashesDelay, interval: flashInterval)); if (!enterDemolishables.Any(i => i.IsValidTarget(enterActor, self)))
return;
w.Add(new FlashTarget(enterActor, count: flashes, delay: flashesDelay, interval: flashInterval));
foreach (var ind in notifiers) foreach (var ind in notifiers)
ind.Demolishing(self); ind.Demolishing(self);
foreach (var d in demolishables) foreach (var d in enterDemolishables)
d.Demolish(target, self, delay); d.Demolish(enterActor, self, delay);
if (enterBehaviour == EnterBehaviour.Dispose)
self.Dispose();
else if (enterBehaviour == EnterBehaviour.Suicide)
self.Kill(self);
}); });
} }
} }

View File

@@ -31,7 +31,7 @@ namespace OpenRA.Mods.Common.Scripting
[Desc("Demolish the target actor.")] [Desc("Demolish the target actor.")]
public void Demolish(Actor target) public void Demolish(Actor target)
{ {
Self.QueueActivity(new Demolish(Self, target, info.EnterBehaviour, info.DetonationDelay, Self.QueueActivity(new Demolish(Self, Target.FromActor(target), info.EnterBehaviour, info.DetonationDelay,
info.Flashes, info.FlashesDelay, info.FlashInterval)); info.Flashes, info.FlashesDelay, info.FlashInterval));
} }
} }

View File

@@ -75,19 +75,18 @@ namespace OpenRA.Mods.Common.Traits
if (order.OrderString != "C4") if (order.OrderString != "C4")
return; return;
var target = self.ResolveFrozenActorOrder(order, Color.Red); if (order.Target.Type == TargetType.Actor)
if (target.Type != TargetType.Actor) {
return; var demolishables = order.Target.Actor.TraitsImplementing<IDemolishable>();
if (!demolishables.Any(i => i.IsValidTarget(order.Target.Actor, self)))
var demolishable = target.Actor.TraitOrDefault<IDemolishable>(); return;
if (demolishable == null || !demolishable.IsValidTarget(target.Actor, self)) }
return;
if (!order.Queued) if (!order.Queued)
self.CancelActivity(); self.CancelActivity();
self.SetTargetLine(target, Color.Red); self.SetTargetLine(order.Target, Color.Red);
self.QueueActivity(new Demolish(self, target.Actor, info.EnterBehaviour, info.DetonationDelay, self.QueueActivity(new Demolish(self, order.Target, info.EnterBehaviour, info.DetonationDelay,
info.Flashes, info.FlashesDelay, info.FlashInterval)); info.Flashes, info.FlashesDelay, info.FlashInterval));
} }