Make IsDisabled a method on Actor.

This allows us to cache the disabled traits, which simplifies life for some callers since we relieve them of having to cache it, as well as improving perf for all IsDisabled calls.
This commit is contained in:
RoosterDragon
2015-04-20 22:12:30 +01:00
parent 500a33b590
commit 2937a31463
9 changed files with 18 additions and 25 deletions

View File

@@ -85,6 +85,7 @@ namespace OpenRA
readonly IRenderModifier[] renderModifiers;
readonly IRender[] renders;
readonly IDisable[] disables;
internal Actor(World world, string name, TypeDictionary initDict)
{
@@ -128,6 +129,7 @@ namespace OpenRA
renderModifiers = TraitsImplementing<IRenderModifier>().ToArray();
renders = TraitsImplementing<IRender>().ToArray();
disables = TraitsImplementing<IDisable>().ToArray();
}
public void Tick()
@@ -277,6 +279,14 @@ namespace OpenRA
health.Value.InflictDamage(this, attacker, health.Value.MaxHP, null, true);
}
public bool IsDisabled()
{
foreach (var disable in disables)
if (disable.Disabled)
return true;
return false;
}
#region Scripting interface
Lazy<ScriptActorInterface> luaInterface;

View File

@@ -37,7 +37,7 @@ namespace OpenRA.Traits
if (lobbyShroudFogDisabled)
return;
var disabled = self.TraitsImplementing<IDisable>().Any(d => d.Disabled);
var disabled = self.IsDisabled();
if (cachedLocation != self.Location || cachedDisabled != disabled)
{
cachedLocation = self.Location;

View File

@@ -329,14 +329,6 @@ namespace OpenRA.Traits
void OnObjectiveFailed(Player player, int objectiveID);
}
public static class DisableExts
{
public static bool IsDisabled(this Actor a)
{
return a.TraitsImplementing<IDisable>().Any(d => d.Disabled);
}
}
public interface ILegacyEditorRenderInfo
{
string EditorPalette { get; }

View File

@@ -54,11 +54,8 @@ namespace OpenRA.Mods.Common.Traits
DefaultAnimation.PlayRepeating(NormalizeSequence(self, info.Sequence));
if (info.PauseOnLowPower)
{
var disabled = self.TraitsImplementing<IDisable>();
DefaultAnimation.Paused = () => disabled.Any(d => d.Disabled)
&& DefaultAnimation.CurrentSequence.Name == NormalizeSequence(self, info.Sequence);
}
DefaultAnimation.Paused = () =>
self.IsDisabled() && DefaultAnimation.CurrentSequence.Name == NormalizeSequence(self, info.Sequence);
}
public void PlayCustomAnimThen(Actor self, string name, Action a)

View File

@@ -30,13 +30,11 @@ namespace OpenRA.Mods.Common.Traits
public class WithActiveAnimation : ITick, INotifyBuildComplete, INotifySold
{
readonly IEnumerable<IDisable> disabled;
readonly WithActiveAnimationInfo info;
readonly RenderBuilding renderBuilding;
public WithActiveAnimation(Actor self, WithActiveAnimationInfo info)
{
disabled = self.TraitsImplementing<IDisable>();
renderBuilding = self.Trait<RenderBuilding>();
this.info = info;
}
@@ -49,7 +47,7 @@ namespace OpenRA.Mods.Common.Traits
if (--ticks <= 0)
{
if (!(info.PauseOnLowPower && disabled.Any(d => d.Disabled)))
if (!(info.PauseOnLowPower && self.IsDisabled()))
renderBuilding.PlayCustomAnim(self, info.Sequence);
ticks = info.Interval;
}

View File

@@ -61,7 +61,6 @@ namespace OpenRA.Mods.Common.Traits
{
var rs = self.Trait<RenderSprites>();
var body = self.Trait<IBodyOrientation>();
var disabled = self.TraitsImplementing<IDisable>();
buildComplete = !self.HasTrait<Building>(); // always render instantly for units
overlay = new Animation(self.World, rs.GetImage(self));
@@ -70,7 +69,7 @@ namespace OpenRA.Mods.Common.Traits
new AnimationWithOffset(overlay,
() => body.LocalToWorld(info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation))),
() => IsTraitDisabled || !buildComplete,
() => info.PauseOnLowPower && disabled.Any(d => d.Disabled),
() => info.PauseOnLowPower && self.IsDisabled(),
p => WithTurret.ZOffsetFromCenter(self, p, 1)),
info.Palette, info.IsPlayerPalette);
}

View File

@@ -27,19 +27,17 @@ namespace OpenRA.Mods.Common.Traits
public class WithRepairAnimation : INotifyRepair
{
IEnumerable<IDisable> disabled;
WithRepairAnimationInfo info;
public WithRepairAnimation(Actor self, WithRepairAnimationInfo info)
{
disabled = self.TraitsImplementing<IDisable>();
this.info = info;
}
public void Repairing(Actor self, Actor host)
{
var building = host.TraitOrDefault<RenderBuilding>();
if (building != null && !(info.PauseOnLowPower && disabled.Any(d => d.Disabled)))
if (building != null && !(info.PauseOnLowPower && self.IsDisabled()))
building.PlayCustomAnim(host, info.Sequence);
}
}

View File

@@ -44,7 +44,6 @@ namespace OpenRA.Mods.Common.Traits
{
var rs = self.Trait<RenderSprites>();
var body = self.Trait<IBodyOrientation>();
var disabled = self.TraitsImplementing<IDisable>();
buildComplete = !self.HasTrait<Building>(); // always render instantly for units
overlay = new Animation(self.World, rs.GetImage(self));
@@ -53,7 +52,7 @@ namespace OpenRA.Mods.Common.Traits
new AnimationWithOffset(overlay,
() => body.LocalToWorld(info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation))),
() => !buildComplete,
() => info.PauseOnLowPower && disabled.Any(d => d.Disabled),
() => info.PauseOnLowPower && self.IsDisabled(),
p => WithTurret.ZOffsetFromCenter(self, p, 1)),
info.Palette, info.IsPlayerPalette);
}

View File

@@ -179,7 +179,7 @@ namespace OpenRA.Mods.Common.Traits
static bool InstanceDisabled(SupportPower sp)
{
return sp.Self.TraitsImplementing<IDisable>().Any(d => d.Disabled);
return sp.Self.IsDisabled();
}
bool notifiedCharging;