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
using System;
using System.Drawing;
using System.Linq;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
class Demolish : LegacyEnter
class Demolish : Enter
{
readonly Actor target;
readonly IDemolishable[] demolishables;
readonly int delay;
readonly int flashes;
readonly int flashesDelay;
readonly int flashInterval;
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)
: 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();
this.delay = delay;
this.flashes = flashes;
this.flashesDelay = flashesDelay;
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 =>
{
if (target.IsDead)
// Make sure the target hasn't changed while entering
// OnEnterComplete is only called if targetActor is alive
if (targetActor != enterActor)
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)
ind.Demolishing(self);
foreach (var d in demolishables)
d.Demolish(target, self, delay);
foreach (var d in enterDemolishables)
d.Demolish(enterActor, self, delay);
if (enterBehaviour == EnterBehaviour.Dispose)
self.Dispose();
else if (enterBehaviour == EnterBehaviour.Suicide)
self.Kill(self);
});
}
}