Refactor CanPowerDown to provide a condition instead of triggering IsDisabled

This commit is contained in:
reaperrr
2017-08-04 12:25:46 +02:00
committed by Pavel Penev
parent 8d7eb0bc47
commit 6c02e3f2b7
3 changed files with 43 additions and 72 deletions

View File

@@ -17,14 +17,14 @@ namespace OpenRA.Mods.Common.Traits
[Desc("The player can disable the power individually on this actor.")]
public class CanPowerDownInfo : ConditionalTraitInfo, Requires<PowerInfo>
{
[FieldLoader.Require]
[GrantedConditionReference]
[Desc("Condition to grant.")]
public readonly string PowerdownCondition = null;
[Desc("Restore power when this trait is disabled.")]
public readonly bool CancelWhenDisabled = false;
public readonly string IndicatorImage = "poweroff";
[SequenceReference("IndicatorImage")] public readonly string IndicatorSequence = "offline";
[PaletteReference] public readonly string IndicatorPalette = "chrome";
public readonly string PowerupSound = null;
public readonly string PowerdownSound = null;
@@ -34,47 +34,70 @@ namespace OpenRA.Mods.Common.Traits
public override object Create(ActorInitializer init) { return new CanPowerDown(init.Self, this); }
}
public class CanPowerDown : ConditionalTrait<CanPowerDownInfo>, IPowerModifier, IResolveOrder, IDisable, INotifyOwnerChanged
public class CanPowerDown : ConditionalTrait<CanPowerDownInfo>, IPowerModifier, IResolveOrder, INotifyOwnerChanged
{
[Sync] bool disabled = false;
[Sync] bool poweredDown = false;
PowerManager power;
ConditionManager conditionManager;
int conditionToken = ConditionManager.InvalidConditionToken;
public CanPowerDown(Actor self, CanPowerDownInfo info)
: base(info)
{
power = self.Owner.PlayerActor.Trait<PowerManager>();
}
public bool Disabled { get { return disabled; } }
protected override void Created(Actor self)
{
base.Created(self);
conditionManager = self.TraitOrDefault<ConditionManager>();
}
protected override void TraitEnabled(Actor self)
{
Update(self);
power.UpdateActor(self);
}
void Update(Actor self)
{
if (conditionManager == null)
return;
if (poweredDown && conditionToken == ConditionManager.InvalidConditionToken)
conditionToken = conditionManager.GrantCondition(self, Info.PowerdownCondition);
else if (!poweredDown && conditionToken != ConditionManager.InvalidConditionToken)
conditionToken = conditionManager.RevokeCondition(self, conditionToken);
}
void IResolveOrder.ResolveOrder(Actor self, Order order)
{
if (!IsTraitDisabled && order.OrderString == "PowerDown")
{
disabled = !disabled;
poweredDown = !poweredDown;
if (Info.PowerupSound != null && disabled)
if (Info.PowerupSound != null && poweredDown)
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Sounds", Info.PowerupSound, self.Owner.Faction.InternalName);
if (Info.PowerdownSound != null && !disabled)
if (Info.PowerdownSound != null && !poweredDown)
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Sounds", Info.PowerdownSound, self.Owner.Faction.InternalName);
if (Info.PowerupSpeech != null && disabled)
if (Info.PowerupSpeech != null && poweredDown)
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", Info.PowerupSpeech, self.Owner.Faction.InternalName);
if (Info.PowerdownSpeech != null && !disabled)
if (Info.PowerdownSpeech != null && !poweredDown)
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", Info.PowerdownSpeech, self.Owner.Faction.InternalName);
Update(self);
power.UpdateActor(self);
if (disabled)
self.World.AddFrameEndTask(w => w.Add(new PowerdownIndicator(self)));
}
}
int IPowerModifier.GetPowerModifier()
{
return !IsTraitDisabled && disabled ? 0 : 100;
return !IsTraitDisabled && poweredDown ? 0 : 100;
}
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
@@ -84,10 +107,10 @@ namespace OpenRA.Mods.Common.Traits
protected override void TraitDisabled(Actor self)
{
if (!disabled || !Info.CancelWhenDisabled)
if (!poweredDown || !Info.CancelWhenDisabled)
return;
disabled = false;
poweredDown = false;
if (Info.PowerupSound != null)
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Sound", Info.PowerupSound, self.Owner.Faction.InternalName);
@@ -95,6 +118,7 @@ namespace OpenRA.Mods.Common.Traits
if (Info.PowerupSpeech != null)
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", Info.PowerupSpeech, self.Owner.Faction.InternalName);
Update(self);
power.UpdateActor(self);
}
}