diff --git a/OpenRA.Mods.Common/Effects/PowerdownIndicator.cs b/OpenRA.Mods.Common/Effects/PowerdownIndicator.cs deleted file mode 100644 index 908f8bb347..0000000000 --- a/OpenRA.Mods.Common/Effects/PowerdownIndicator.cs +++ /dev/null @@ -1,52 +0,0 @@ -#region Copyright & License Information -/* - * Copyright 2007-2017 The OpenRA Developers (see AUTHORS) - * This file is part of OpenRA, which is free software. It is made - * available to you under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 of - * the License, or (at your option) any later version. For more - * information, see COPYING. - */ -#endregion - -using System.Collections.Generic; -using OpenRA.Effects; -using OpenRA.Graphics; -using OpenRA.Mods.Common.Traits; - -namespace OpenRA.Mods.Common.Effects -{ - class PowerdownIndicator : IEffect, IEffectAboveShroud - { - readonly Actor a; - readonly Animation anim; - readonly CanPowerDown canPowerDown; - - public PowerdownIndicator(Actor a) - { - this.a = a; - canPowerDown = a.Trait(); - - anim = new Animation(a.World, canPowerDown.Info.IndicatorImage); - anim.PlayRepeating(canPowerDown.Info.IndicatorSequence); - } - - void IEffect.Tick(World world) - { - if (!a.IsInWorld || a.IsDead || !canPowerDown.Disabled) - world.AddFrameEndTask(w => w.Remove(this)); - - anim.Tick(); - } - - IEnumerable IEffect.Render(WorldRenderer wr) { return SpriteRenderable.None; } - - IEnumerable IEffectAboveShroud.RenderAboveShroud(WorldRenderer wr) - { - if (a.Disposed || wr.World.FogObscures(a)) - return SpriteRenderable.None; - - return anim.Render(a.CenterPosition, wr.Palette(canPowerDown.Info.IndicatorPalette)); - } - } -} diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 333fc71d68..3e4ca674ca 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -143,7 +143,6 @@ - diff --git a/OpenRA.Mods.Common/Traits/Power/CanPowerDown.cs b/OpenRA.Mods.Common/Traits/Power/CanPowerDown.cs index 5acadf8a09..57ae72383a 100644 --- a/OpenRA.Mods.Common/Traits/Power/CanPowerDown.cs +++ b/OpenRA.Mods.Common/Traits/Power/CanPowerDown.cs @@ -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 { + [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, IPowerModifier, IResolveOrder, IDisable, INotifyOwnerChanged + public class CanPowerDown : ConditionalTrait, 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(); } - public bool Disabled { get { return disabled; } } + protected override void Created(Actor self) + { + base.Created(self); + + conditionManager = self.TraitOrDefault(); + } + + 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); } }