Replace Lazy trait lookups with INotifyCreated.
This commit is contained in:
@@ -62,7 +62,7 @@ namespace OpenRA.Mods.Cnc.Traits
|
|||||||
|
|
||||||
protected override bool CanAttack(Actor self, Target target)
|
protected override bool CanAttack(Actor self, Target target)
|
||||||
{
|
{
|
||||||
if (state == PopupState.Transitioning || !building.Value.BuildComplete)
|
if (state == PopupState.Transitioning || !building.BuildComplete)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!base.CanAttack(self, target))
|
if (!base.CanAttack(self, target))
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
|
|
||||||
inAttackRange = false;
|
inAttackRange = false;
|
||||||
|
|
||||||
var f = facing.Value.Facing;
|
var f = facing.Facing;
|
||||||
var delta = target.CenterPosition - self.CenterPosition;
|
var delta = target.CenterPosition - self.CenterPosition;
|
||||||
var facingToTarget = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : f;
|
var facingToTarget = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : f;
|
||||||
facingTarget = Math.Abs(facingToTarget - f) % 256 <= info.FacingTolerance;
|
facingTarget = Math.Abs(facingToTarget - f) % 256 <= info.FacingTolerance;
|
||||||
@@ -66,7 +66,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
inAttackRange = true;
|
inAttackRange = true;
|
||||||
a.CheckFire(self, facing.Value, bombTarget);
|
a.CheckFire(self, facing, bombTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Guns only fire when approaching the target
|
// Guns only fire when approaching the target
|
||||||
@@ -81,7 +81,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
|
|
||||||
var gunPos = self.CenterPosition - new WVec(0, a.MaxRange().Length / 2, 0).Rotate(WRot.FromFacing(f));
|
var gunPos = self.CenterPosition - new WVec(0, a.MaxRange().Length / 2, 0).Rotate(WRot.FromFacing(f));
|
||||||
var gunHeight = self.World.Map.DistanceAboveTerrain(gunPos);
|
var gunHeight = self.World.Map.DistanceAboveTerrain(gunPos);
|
||||||
a.CheckFire(self, facing.Value, Target.FromPos(gunPos - new WVec(WDist.Zero, WDist.Zero, gunHeight)));
|
a.CheckFire(self, facing, Target.FromPos(gunPos - new WVec(WDist.Zero, WDist.Zero, gunHeight)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
public override abstract object Create(ActorInitializer init);
|
public override abstract object Create(ActorInitializer init);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class AttackBase : ConditionalTrait<AttackBaseInfo>, IIssueOrder, IResolveOrder, IOrderVoice, ISync
|
public abstract class AttackBase : ConditionalTrait<AttackBaseInfo>, INotifyCreated, IIssueOrder, IResolveOrder, IOrderVoice, ISync
|
||||||
{
|
{
|
||||||
readonly string attackOrderName = "Attack";
|
readonly string attackOrderName = "Attack";
|
||||||
readonly string forceAttackOrderName = "ForceAttack";
|
readonly string forceAttackOrderName = "ForceAttack";
|
||||||
@@ -47,9 +47,9 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
[Sync] public bool IsAttacking { get; internal set; }
|
[Sync] public bool IsAttacking { get; internal set; }
|
||||||
public IEnumerable<Armament> Armaments { get { return getArmaments(); } }
|
public IEnumerable<Armament> Armaments { get { return getArmaments(); } }
|
||||||
|
|
||||||
protected Lazy<IFacing> facing;
|
protected IFacing facing;
|
||||||
protected Lazy<Building> building;
|
protected Building building;
|
||||||
protected Lazy<IPositionable> positionable;
|
protected IPositionable positionable;
|
||||||
protected Func<IEnumerable<Armament>> getArmaments;
|
protected Func<IEnumerable<Armament>> getArmaments;
|
||||||
|
|
||||||
readonly Actor self;
|
readonly Actor self;
|
||||||
@@ -58,15 +58,23 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
: base(info)
|
: base(info)
|
||||||
{
|
{
|
||||||
this.self = self;
|
this.self = self;
|
||||||
|
}
|
||||||
|
|
||||||
var armaments = Exts.Lazy(() => self.TraitsImplementing<Armament>()
|
void INotifyCreated.Created(Actor self)
|
||||||
.Where(a => info.Armaments.Contains(a.Info.Name)).ToArray());
|
{
|
||||||
|
facing = self.TraitOrDefault<IFacing>();
|
||||||
|
building = self.TraitOrDefault<Building>();
|
||||||
|
positionable = self.TraitOrDefault<IPositionable>();
|
||||||
|
|
||||||
getArmaments = () => armaments.Value;
|
getArmaments = InitializeGetArmaments(self);
|
||||||
|
}
|
||||||
|
|
||||||
facing = Exts.Lazy(() => self.TraitOrDefault<IFacing>());
|
protected virtual Func<IEnumerable<Armament>> InitializeGetArmaments(Actor self)
|
||||||
building = Exts.Lazy(() => self.TraitOrDefault<Building>());
|
{
|
||||||
positionable = Exts.Lazy(() => self.Trait<IPositionable>());
|
var armaments = self.TraitsImplementing<Armament>()
|
||||||
|
.Where(a => Info.Armaments.Contains(a.Info.Name)).ToArray();
|
||||||
|
|
||||||
|
return () => armaments;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual bool CanAttack(Actor self, Target target)
|
protected virtual bool CanAttack(Actor self, Target target)
|
||||||
@@ -85,7 +93,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Building is under construction or is being sold
|
// Building is under construction or is being sold
|
||||||
if (building.Value != null && !building.Value.BuildComplete)
|
if (building != null && !building.BuildComplete)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (Armaments.All(a => a.IsReloading))
|
if (Armaments.All(a => a.IsReloading))
|
||||||
@@ -100,7 +108,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
foreach (var a in armaments ?? Armaments)
|
foreach (var a in armaments ?? Armaments)
|
||||||
a.CheckFire(self, facing.Value, target);
|
a.CheckFire(self, facing, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<IOrderTargeter> Orders
|
public IEnumerable<IOrderTargeter> Orders
|
||||||
@@ -188,7 +196,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
if (IsTraitDisabled)
|
if (IsTraitDisabled)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (Info.AttackRequiresEnteringCell && !positionable.Value.CanEnterCell(t.Actor.Location, null, false))
|
if (Info.AttackRequiresEnteringCell && (positionable == null || !positionable.CanEnterCell(t.Actor.Location, null, false)))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// PERF: Avoid LINQ.
|
// PERF: Avoid LINQ.
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
if (!base.CanAttack(self, target))
|
if (!base.CanAttack(self, target))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var f = facing.Value.Facing;
|
var f = facing.Facing;
|
||||||
var delta = target.CenterPosition - self.CenterPosition;
|
var delta = target.CenterPosition - self.CenterPosition;
|
||||||
var facingToTarget = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : f;
|
var facingToTarget = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : f;
|
||||||
|
|
||||||
|
|||||||
@@ -92,8 +92,11 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
paxFacing = new Dictionary<Actor, IFacing>();
|
paxFacing = new Dictionary<Actor, IFacing>();
|
||||||
paxPos = new Dictionary<Actor, IPositionable>();
|
paxPos = new Dictionary<Actor, IPositionable>();
|
||||||
paxRender = new Dictionary<Actor, RenderSprites>();
|
paxRender = new Dictionary<Actor, RenderSprites>();
|
||||||
|
}
|
||||||
|
|
||||||
getArmaments = () => armaments;
|
protected override Func<IEnumerable<Armament>> InitializeGetArmaments(Actor self)
|
||||||
|
{
|
||||||
|
return () => armaments;
|
||||||
}
|
}
|
||||||
|
|
||||||
void INotifyPassengerEntered.OnPassengerEntered(Actor self, Actor passenger)
|
void INotifyPassengerEntered.OnPassengerEntered(Actor self, Actor passenger)
|
||||||
@@ -117,7 +120,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
FirePort SelectFirePort(Actor self, WAngle targetYaw)
|
FirePort SelectFirePort(Actor self, WAngle targetYaw)
|
||||||
{
|
{
|
||||||
// Pick a random port that faces the target
|
// Pick a random port that faces the target
|
||||||
var bodyYaw = facing.Value != null ? WAngle.FromFacing(facing.Value.Facing) : WAngle.Zero;
|
var bodyYaw = facing != null ? WAngle.FromFacing(facing.Facing) : WAngle.Zero;
|
||||||
var indices = Enumerable.Range(0, Info.Ports.Length).Shuffle(self.World.SharedRandom);
|
var indices = Enumerable.Range(0, Info.Ports.Length).Shuffle(self.World.SharedRandom);
|
||||||
foreach (var i in indices)
|
foreach (var i in indices)
|
||||||
{
|
{
|
||||||
@@ -155,7 +158,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
paxFacing[a.Actor].Facing = muzzleFacing;
|
paxFacing[a.Actor].Facing = muzzleFacing;
|
||||||
paxPos[a.Actor].SetVisualPosition(a.Actor, pos + PortOffset(self, port));
|
paxPos[a.Actor].SetVisualPosition(a.Actor, pos + PortOffset(self, port));
|
||||||
|
|
||||||
var barrel = a.CheckFire(a.Actor, facing.Value, target);
|
var barrel = a.CheckFire(a.Actor, facing, target);
|
||||||
if (barrel == null)
|
if (barrel == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ namespace OpenRA.Mods.D2k.Traits
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
self.CancelActivity();
|
self.CancelActivity();
|
||||||
self.QueueActivity(new SwallowActor(self, target, a, facing.Value));
|
self.QueueActivity(new SwallowActor(self, target, a, facing));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user