Misc constructor caching

Cache trait look-ups in constructor for various other traits and
activities.
This commit is contained in:
reaperrr
2015-03-14 04:00:39 +01:00
parent 1e9d1a6cb7
commit b52d055eec
11 changed files with 70 additions and 48 deletions

View File

@@ -25,8 +25,8 @@ namespace OpenRA.Mods.Common.Traits
class Burns : ITick, ISync
{
readonly BurnsInfo info;
[Sync] int ticks;
BurnsInfo info;
public Burns(Actor self, BurnsInfo info)
{

View File

@@ -51,21 +51,20 @@ namespace OpenRA.Mods.Common.Traits
public class ExternalCapturable : ITick
{
readonly Building building;
[Sync] public int CaptureProgressTime = 0;
[Sync] public Actor Captor;
private Actor self;
public ExternalCapturableInfo Info;
public bool CaptureInProgress { get { return Captor != null; } }
public ExternalCapturable(Actor self, ExternalCapturableInfo info)
{
this.self = self;
Info = info;
building = self.TraitOrDefault<Building>();
}
public void BeginCapture(Actor captor)
{
var building = self.TraitOrDefault<Building>();
if (building != null)
building.Lock();
@@ -74,7 +73,6 @@ namespace OpenRA.Mods.Common.Traits
public void EndCapture()
{
var building = self.TraitOrDefault<Building>();
if (building != null)
building.Unlock();

View File

@@ -99,7 +99,7 @@ namespace OpenRA.Mods.Common.Traits
self.CancelActivity();
self.SetTargetLine(target, Color.Red);
self.QueueActivity(new ExternalCaptureActor(target));
self.QueueActivity(new ExternalCaptureActor(self, target));
}
}

View File

@@ -30,6 +30,7 @@ namespace OpenRA.Mods.Common.Traits
class ScaredyCat : ITick, INotifyIdle, INotifyDamage, INotifyAttack, ISpeedModifier, ISync, IRenderInfantrySequenceModifier
{
readonly ScaredyCatInfo info;
readonly Mobile mobile;
[Sync] readonly Actor self;
[Sync] int panicStartedTick;
bool Panicking { get { return panicStartedTick > 0; } }
@@ -41,6 +42,7 @@ namespace OpenRA.Mods.Common.Traits
{
this.self = self;
this.info = info;
mobile = self.Trait<Mobile>();
}
public void Panic()
@@ -68,7 +70,7 @@ namespace OpenRA.Mods.Common.Traits
if (!Panicking)
return;
self.Trait<Mobile>().Nudge(self, self, true);
mobile.Nudge(self, self, true);
}
public void Damaged(Actor self, AttackInfo e)

View File

@@ -27,21 +27,22 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Custom palette name")]
public readonly string Palette = "effect";
public object Create(ActorInitializer init) { return new WithBuildingExplosion(this); }
public object Create(ActorInitializer init) { return new WithBuildingExplosion(init.Self, this); }
}
class WithBuildingExplosion : INotifyKilled
{
WithBuildingExplosionInfo info;
BuildingInfo buildingInfo;
public WithBuildingExplosion(WithBuildingExplosionInfo info)
public WithBuildingExplosion(Actor self, WithBuildingExplosionInfo info)
{
this.info = info;
buildingInfo = self.Info.Traits.Get<BuildingInfo>();
}
public void Killed(Actor self, AttackInfo e)
{
var buildingInfo = self.Info.Traits.Get<BuildingInfo>();
var cells = FootprintUtils.UnpathableTiles(self.Info.Name, buildingInfo, self.Location);
if (info.Delay > 0)

View File

@@ -29,8 +29,14 @@ namespace OpenRA.Mods.Common.Traits
{
readonly Actor self;
readonly RepairableNearInfo info;
readonly IMove movement;
public RepairableNear(Actor self, RepairableNearInfo info) { this.self = self; this.info = info; }
public RepairableNear(Actor self, RepairableNearInfo info)
{
this.self = self;
this.info = info;
movement = self.Trait<IMove>();
}
public IEnumerable<IOrderTargeter> Orders
{
@@ -63,7 +69,6 @@ namespace OpenRA.Mods.Common.Traits
{
if (order.OrderString == "RepairNear" && CanRepairAt(order.TargetActor) && ShouldRepair())
{
var movement = self.Trait<IMove>();
var target = Target.FromOrder(self.World, order);
self.CancelActivity();

View File

@@ -29,12 +29,18 @@ namespace OpenRA.Mods.Common.Traits
{
readonly Actor self;
readonly Lazy<Health> health;
readonly SellableInfo info;
readonly Building building;
readonly WithMakeAnimation makeAnimation;
public Sellable(Actor self, SellableInfo info)
: base(info)
{
this.self = self;
this.info = info;
health = Exts.Lazy(() => self.TraitOrDefault<Health>());
building = self.TraitOrDefault<Building>();
makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
}
public void ResolveOrder(Actor self, Order order)
@@ -48,23 +54,21 @@ namespace OpenRA.Mods.Common.Traits
if (IsTraitDisabled)
return;
var building = self.TraitOrDefault<Building>();
if (building != null && !building.Lock())
return;
self.CancelActivity();
foreach (var s in Info.SellSounds)
foreach (var s in info.SellSounds)
Sound.PlayToPlayer(self.Owner, s, self.CenterPosition);
foreach (var ns in self.TraitsImplementing<INotifySold>())
ns.Selling(self);
var makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
if (makeAnimation != null)
makeAnimation.Reverse(self, new Sell(), false);
makeAnimation.Reverse(self, new Sell(self), false);
else
self.QueueActivity(false, new Sell());
self.QueueActivity(false, new Sell(self));
}
public bool IsTooltipVisible(Player forPlayer)
@@ -78,7 +82,7 @@ namespace OpenRA.Mods.Common.Traits
{
get
{
var sellValue = self.GetSellValue() * Info.RefundPercent / 100;
var sellValue = self.GetSellValue() * info.RefundPercent / 100;
if (health.Value != null)
{
sellValue *= health.Value.HP;

View File

@@ -46,14 +46,18 @@ namespace OpenRA.Mods.Common.Traits
{
readonly Actor self;
readonly TransformsInfo info;
readonly BuildingInfo bi;
readonly Building building;
readonly BuildingInfo buildingInfo;
readonly string race;
readonly WithMakeAnimation makeAnimation;
public Transforms(ActorInitializer init, TransformsInfo info)
{
self = init.Self;
this.info = info;
bi = self.World.Map.Rules.Actors[info.IntoActor].Traits.GetOrDefault<BuildingInfo>();
buildingInfo = self.World.Map.Rules.Actors[info.IntoActor].Traits.GetOrDefault<BuildingInfo>();
building = self.TraitOrDefault<Building>();
makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
race = init.Contains<RaceInit>() ? init.Get<RaceInit, string>() : self.Owner.Country.Race;
}
@@ -64,11 +68,10 @@ namespace OpenRA.Mods.Common.Traits
bool CanDeploy()
{
var b = self.TraitOrDefault<Building>();
if (b != null && b.Locked)
if (building != null && building.Locked)
return false;
return bi == null || self.World.CanPlaceBuilding(info.IntoActor, bi, self.Location + info.Offset, self);
return buildingInfo == null || self.World.CanPlaceBuilding(info.IntoActor, buildingInfo, self.Location + info.Offset, self);
}
public IEnumerable<IOrderTargeter> Orders
@@ -86,9 +89,7 @@ namespace OpenRA.Mods.Common.Traits
public void DeployTransform(bool queued)
{
var b = self.TraitOrDefault<Building>();
if (!CanDeploy() || (b != null && !b.Lock()))
if (!CanDeploy() || (building != null && !building.Lock()))
{
foreach (var s in info.NoTransformSounds)
Sound.PlayToPlayer(self.Owner, s);
@@ -116,7 +117,6 @@ namespace OpenRA.Mods.Common.Traits
Race = race
};
var makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
if (makeAnimation != null)
makeAnimation.Reverse(self, transform);
else