Cache traits directly in FrozenUnderFog.

Avoid the performance impact of accessing Lazy.Value by caching the traits on the first tick and accessing the references directly when needed.
This commit is contained in:
RoosterDragon
2015-11-27 15:20:03 +00:00
parent 23a38a08f7
commit ab6ef9be40

View File

@@ -35,11 +35,10 @@ namespace OpenRA.Mods.Common.Traits
readonly bool startsRevealed; readonly bool startsRevealed;
readonly PPos[] footprint; readonly PPos[] footprint;
readonly Lazy<ITooltip> tooltip;
readonly Lazy<Health> health;
readonly Dictionary<Player, FrozenState> stateByPlayer = new Dictionary<Player, FrozenState>(); readonly Dictionary<Player, FrozenState> stateByPlayer = new Dictionary<Player, FrozenState>();
ITooltip tooltip;
Health health;
bool initialized; bool initialized;
class FrozenState class FrozenState
@@ -62,8 +61,6 @@ namespace OpenRA.Mods.Common.Traits
startsRevealed = info.StartsRevealed && !init.Contains<ParentActorInit>(); startsRevealed = info.StartsRevealed && !init.Contains<ParentActorInit>();
var footprintCells = FootprintUtils.Tiles(init.Self).ToList(); var footprintCells = FootprintUtils.Tiles(init.Self).ToList();
footprint = footprintCells.SelectMany(c => map.ProjectedCellsCovering(c.ToMPos(map))).ToArray(); footprint = footprintCells.SelectMany(c => map.ProjectedCellsCovering(c.ToMPos(map))).ToArray();
tooltip = Exts.Lazy(() => init.Self.TraitsImplementing<ITooltip>().FirstOrDefault());
health = Exts.Lazy(() => init.Self.TraitOrDefault<Health>());
} }
bool IsVisibleInner(Actor self, Player byPlayer) bool IsVisibleInner(Actor self, Player byPlayer)
@@ -90,6 +87,13 @@ namespace OpenRA.Mods.Common.Traits
return; return;
VisibilityHash = 0; VisibilityHash = 0;
if (!initialized)
{
tooltip = self.TraitsImplementing<ITooltip>().FirstOrDefault();
health = self.TraitOrDefault<Health>();
}
foreach (var player in self.World.Players) foreach (var player in self.World.Players)
{ {
FrozenActor frozenActor; FrozenActor frozenActor;
@@ -116,16 +120,16 @@ namespace OpenRA.Mods.Common.Traits
frozenActor.Owner = self.Owner; frozenActor.Owner = self.Owner;
if (health.Value != null) if (health != null)
{ {
frozenActor.HP = health.Value.HP; frozenActor.HP = health.HP;
frozenActor.DamageState = health.Value.DamageState; frozenActor.DamageState = health.DamageState;
} }
if (tooltip.Value != null) if (tooltip != null)
{ {
frozenActor.TooltipInfo = tooltip.Value.TooltipInfo; frozenActor.TooltipInfo = tooltip.TooltipInfo;
frozenActor.TooltipOwner = tooltip.Value.Owner; frozenActor.TooltipOwner = tooltip.Owner;
} }
} }