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

@@ -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<CanPowerDown>();
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<IRenderable> IEffect.Render(WorldRenderer wr) { return SpriteRenderable.None; }
IEnumerable<IRenderable> IEffectAboveShroud.RenderAboveShroud(WorldRenderer wr)
{
if (a.Disposed || wr.World.FogObscures(a))
return SpriteRenderable.None;
return anim.Render(a.CenterPosition, wr.Palette(canPowerDown.Info.IndicatorPalette));
}
}
}

View File

@@ -143,7 +143,6 @@
<Compile Include="Effects\FlashTarget.cs" />
<Compile Include="Effects\FloatingText.cs" />
<Compile Include="Effects\MapNotificationEffect.cs" />
<Compile Include="Effects\PowerdownIndicator.cs" />
<Compile Include="Effects\RallyPointIndicator.cs" />
<Compile Include="Effects\RepairIndicator.cs" />
<Compile Include="Effects\SpriteEffect.cs" />

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);
}
}