Cache some trait lookups that occur frequently.

This commit is contained in:
RoosterDragon
2015-04-20 21:21:09 +01:00
parent 6125d7c117
commit 500a33b590
4 changed files with 17 additions and 11 deletions

View File

@@ -315,13 +315,12 @@ namespace OpenRA.Mods.Common.Activities
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
var mobile = self.Trait<Mobile>();
var ret = InnerTick(self, Move.mobile); var ret = InnerTick(self, Move.mobile);
mobile.IsMoving = ret is MovePart; Move.mobile.IsMoving = ret is MovePart;
if (moveFraction > MoveFractionTotal) if (moveFraction > MoveFractionTotal)
moveFraction = MoveFractionTotal; moveFraction = MoveFractionTotal;
UpdateCenterLocation(self, mobile); UpdateCenterLocation(self, Move.mobile);
return ret; return ret;
} }

View File

@@ -312,6 +312,7 @@ namespace OpenRA.Mods.Common.Traits
internal int TicksBeforePathing = 0; internal int TicksBeforePathing = 0;
readonly Actor self; readonly Actor self;
readonly ISpeedModifier[] speedModifiers;
public readonly MobileInfo Info; public readonly MobileInfo Info;
public bool IsMoving { get; set; } public bool IsMoving { get; set; }
@@ -351,6 +352,8 @@ namespace OpenRA.Mods.Common.Traits
self = init.Self; self = init.Self;
Info = info; Info = info;
speedModifiers = self.TraitsImplementing<ISpeedModifier>().ToArray();
ToSubCell = FromSubCell = info.SharesCell ? init.World.Map.DefaultSubCell : SubCell.FullCell; ToSubCell = FromSubCell = info.SharesCell ? init.World.Map.DefaultSubCell : SubCell.FullCell;
if (init.Contains<SubCellInit>()) if (init.Contains<SubCellInit>())
FromSubCell = ToSubCell = init.Get<SubCellInit, SubCell>(); FromSubCell = ToSubCell = init.Get<SubCellInit, SubCell>();
@@ -597,7 +600,7 @@ namespace OpenRA.Mods.Common.Traits
return 0; return 0;
speed *= Info.Speed; speed *= Info.Speed;
foreach (var t in self.TraitsImplementing<ISpeedModifier>()) foreach (var t in speedModifiers)
speed *= t.GetSpeedModifier() / 100m; speed *= t.GetSpeedModifier() / 100m;
return (int)(speed / 100); return (int)(speed / 100);

View File

@@ -27,9 +27,10 @@ namespace OpenRA.Mods.Common.Traits
class WithMuzzleFlash : UpgradableTrait<WithMuzzleFlashInfo>, INotifyAttack, IRender, ITick class WithMuzzleFlash : UpgradableTrait<WithMuzzleFlashInfo>, INotifyAttack, IRender, ITick
{ {
Dictionary<Barrel, bool> visible = new Dictionary<Barrel, bool>(); readonly Dictionary<Barrel, bool> visible = new Dictionary<Barrel, bool>();
Dictionary<Barrel, AnimationWithOffset> anims = new Dictionary<Barrel, AnimationWithOffset>(); readonly Dictionary<Barrel, AnimationWithOffset> anims = new Dictionary<Barrel, AnimationWithOffset>();
Func<int> getFacing; readonly Func<int> getFacing;
readonly Armament[] armaments;
public WithMuzzleFlash(Actor self, WithMuzzleFlashInfo info) public WithMuzzleFlash(Actor self, WithMuzzleFlashInfo info)
: base(info) : base(info)
@@ -37,7 +38,9 @@ namespace OpenRA.Mods.Common.Traits
var render = self.Trait<RenderSprites>(); var render = self.Trait<RenderSprites>();
var facing = self.TraitOrDefault<IFacing>(); var facing = self.TraitOrDefault<IFacing>();
foreach (var arm in self.TraitsImplementing<Armament>()) armaments = self.TraitsImplementing<Armament>().ToArray();
foreach (var arm in armaments)
{ {
var armClosure = arm; // closure hazard in AnimationWithOffset var armClosure = arm; // closure hazard in AnimationWithOffset
@@ -87,7 +90,7 @@ namespace OpenRA.Mods.Common.Traits
public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr) public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
{ {
foreach (var arm in self.TraitsImplementing<Armament>()) foreach (var arm in armaments)
{ {
var palette = wr.Palette(arm.Info.MuzzlePalette); var palette = wr.Palette(arm.Info.MuzzlePalette);
foreach (var kv in anims) foreach (var kv in anims)

View File

@@ -13,7 +13,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
public class TurretedInfo : ITraitInfo, UsesInit<TurretFacingInit> public class TurretedInfo : ITraitInfo, UsesInit<TurretFacingInit>, Requires<IBodyOrientationInfo>
{ {
public readonly string Turret = "primary"; public readonly string Turret = "primary";
[Desc("Rate of Turning")] [Desc("Rate of Turning")]
@@ -34,6 +34,7 @@ namespace OpenRA.Mods.Common.Traits
readonly TurretedInfo info; readonly TurretedInfo info;
AttackTurreted attack; AttackTurreted attack;
IFacing facing; IFacing facing;
IBodyOrientation body;
[Sync] public int QuantizedFacings = 0; [Sync] public int QuantizedFacings = 0;
[Sync] public int TurretFacing = 0; [Sync] public int TurretFacing = 0;
@@ -67,6 +68,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
attack = self.TraitOrDefault<AttackTurreted>(); attack = self.TraitOrDefault<AttackTurreted>();
facing = self.TraitOrDefault<IFacing>(); facing = self.TraitOrDefault<IFacing>();
body = self.Trait<IBodyOrientation>();
} }
public virtual void Tick(Actor self) public virtual void Tick(Actor self)
@@ -94,7 +96,6 @@ namespace OpenRA.Mods.Common.Traits
// Turret offset in world-space // Turret offset in world-space
public WVec Position(Actor self) public WVec Position(Actor self)
{ {
var body = self.Trait<IBodyOrientation>();
var bodyOrientation = body.QuantizeOrientation(self, self.Orientation); var bodyOrientation = body.QuantizeOrientation(self, self.Orientation);
return body.LocalToWorld(Offset.Rotate(bodyOrientation)); return body.LocalToWorld(Offset.Rotate(bodyOrientation));
} }