Port Infiltrate to the new Enter activity.

This commit is contained in:
Paul Chote
2019-01-31 22:18:01 +00:00
committed by Oliver Brakmann
parent e9c3216048
commit a17608a24e
3 changed files with 54 additions and 27 deletions

View File

@@ -9,9 +9,9 @@
*/ */
#endregion #endregion
using System;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Cnc.Traits; using OpenRA.Mods.Cnc.Traits;
using OpenRA.Mods.Common.Activities; using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
@@ -19,34 +19,51 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Activities namespace OpenRA.Mods.Cnc.Activities
{ {
class Infiltrate : LegacyEnter class Infiltrate : Enter
{ {
readonly Actor target;
readonly Infiltrates infiltrates; readonly Infiltrates infiltrates;
readonly INotifyInfiltration[] notifiers; readonly INotifyInfiltration[] notifiers;
Actor enterActor;
public Infiltrate(Actor self, Actor target, Infiltrates infiltrate) public Infiltrate(Actor self, Target target, Infiltrates infiltrates)
: base(self, target, infiltrate.Info.EnterBehaviour, targetLineColor: Color.Red) : base(self, target, Color.Red)
{ {
this.target = target; this.infiltrates = infiltrates;
infiltrates = infiltrate;
notifiers = self.TraitsImplementing<INotifyInfiltration>().ToArray(); notifiers = self.TraitsImplementing<INotifyInfiltration>().ToArray();
} }
protected override void OnInside(Actor self) protected override void TickInner(Actor self, Target target, bool targetIsDeadOrHiddenActor)
{ {
if (target.IsDead) if (infiltrates.IsTraitDisabled)
return; Cancel(self, true);
}
var stance = self.Owner.Stances[target.Owner]; protected override bool TryStartEnter(Actor self, Actor targetActor)
if (!infiltrates.Info.ValidStances.HasStance(stance)) {
// 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 (!infiltrates.CanInfiltrateTarget(self, Target.FromActor(targetActor)))
{
Cancel(self, true);
return false;
}
enterActor = targetActor;
return true;
}
protected override void OnEnterComplete(Actor self, Actor targetActor)
{
// Make sure the target hasn't changed while entering
// OnEnterComplete is only called if targetActor is alive
if (targetActor != enterActor || !infiltrates.CanInfiltrateTarget(self, Target.FromActor(targetActor)))
return; return;
foreach (var ini in notifiers) foreach (var ini in notifiers)
ini.Infiltrating(self); ini.Infiltrating(self);
foreach (var t in target.TraitsImplementing<INotifyInfiltrated>()) foreach (var t in targetActor.TraitsImplementing<INotifyInfiltrated>())
t.Infiltrated(target, self, infiltrates.Info.Types); t.Infiltrated(targetActor, self, infiltrates.Info.Types);
var exp = self.Owner.PlayerActor.TraitOrDefault<PlayerExperience>(); var exp = self.Owner.PlayerActor.TraitOrDefault<PlayerExperience>();
if (exp != null) if (exp != null)
@@ -55,14 +72,11 @@ namespace OpenRA.Mods.Cnc.Activities
if (!string.IsNullOrEmpty(infiltrates.Info.Notification)) if (!string.IsNullOrEmpty(infiltrates.Info.Notification))
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech",
infiltrates.Info.Notification, self.Owner.Faction.InternalName); infiltrates.Info.Notification, self.Owner.Faction.InternalName);
}
public override Activity Tick(Actor self) if (infiltrates.Info.EnterBehaviour == EnterBehaviour.Dispose)
{ self.Dispose();
if (infiltrates.IsTraitDisabled) else if (infiltrates.Info.EnterBehaviour == EnterBehaviour.Suicide)
Cancel(self); self.Kill(self);
return base.Tick(self);
} }
} }
} }

View File

@@ -37,7 +37,7 @@ namespace OpenRA.Mods.Cnc.Scripting
if (infiltrates == null) if (infiltrates == null)
throw new LuaException("{0} tried to infiltrate invalid target {1}!".F(Self, target)); throw new LuaException("{0} tried to infiltrate invalid target {1}!".F(Self, target));
Self.QueueActivity(new Infiltrate(Self, target, infiltrates)); Self.QueueActivity(new Infiltrate(Self, Target.FromActor(target), infiltrates));
} }
} }
} }

View File

@@ -92,21 +92,34 @@ namespace OpenRA.Mods.Cnc.Traits
? Info.Voice : null; ? Info.Voice : null;
} }
public bool CanInfiltrateTarget(Actor self, Target target)
{
switch (target.Type)
{
case TargetType.Actor:
return Info.Types.Overlaps(target.Actor.GetEnabledTargetTypes()) &&
Info.ValidStances.HasStance(self.Owner.Stances[target.Actor.Owner]);
case TargetType.FrozenActor:
return target.FrozenActor.IsValid && Info.Types.Overlaps(target.FrozenActor.TargetTypes) &&
Info.ValidStances.HasStance(self.Owner.Stances[target.FrozenActor.Owner]);
default:
return false;
}
}
public void ResolveOrder(Actor self, Order order) public void ResolveOrder(Actor self, Order order)
{ {
if (order.OrderString != "Infiltrate" || !IsValidOrder(self, order) || IsTraitDisabled) if (order.OrderString != "Infiltrate" || !IsValidOrder(self, order) || IsTraitDisabled)
return; return;
var target = self.ResolveFrozenActorOrder(order, Color.Red); if (!CanInfiltrateTarget(self, order.Target))
if (target.Type != TargetType.Actor
|| !Info.Types.Overlaps(target.Actor.GetAllTargetTypes()))
return; 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 Infiltrate(self, target.Actor, this)); self.QueueActivity(new Infiltrate(self, order.Target, this));
} }
} }