Support multiple Power traits

This commit is contained in:
atlimit8
2014-11-29 15:56:25 -06:00
parent 47357addeb
commit 92779cc496
7 changed files with 47 additions and 25 deletions

View File

@@ -16,18 +16,21 @@ namespace OpenRA.Mods.Common.Power
[Desc("The player can disable the power individually on this actor.")]
public class CanPowerDownInfo : UpgradableTraitInfo, ITraitInfo, Requires<PowerInfo>
{
[Desc("Restore power when this trait is disabled.")]
public readonly bool CancelWhenDisabled = false;
public object Create(ActorInitializer init) { return new CanPowerDown(init.self, this); }
}
public class CanPowerDown : UpgradableTrait<CanPowerDownInfo>, IPowerModifier, IResolveOrder, IDisable
public class CanPowerDown : UpgradableTrait<CanPowerDownInfo>, IPowerModifier, IResolveOrder, IDisable, INotifyOwnerChanged
{
[Sync] bool disabled = false;
readonly Power power;
PowerManager power;
public CanPowerDown(Actor self, CanPowerDownInfo info)
: base(info)
{
power = self.Trait<Power>();
power = self.Owner.PlayerActor.Trait<PowerManager>();
}
public bool Disabled { get { return disabled; } }
@@ -38,7 +41,7 @@ namespace OpenRA.Mods.Common.Power
{
disabled = !disabled;
Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Sounds", disabled ? "EnablePower" : "DisablePower", self.Owner.Country.Race);
power.PlayerPower.UpdateActor(self);
power.UpdateActor(self);
if (disabled)
self.World.AddFrameEndTask(w => w.Add(new PowerdownIndicator(self)));
@@ -49,5 +52,19 @@ namespace OpenRA.Mods.Common.Power
{
return !IsTraitDisabled && disabled ? 0 : 100;
}
public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{
power = newOwner.PlayerActor.Trait<PowerManager>();
}
protected override void UpgradeDisabled(Actor self)
{
if (!disabled || !Info.CancelWhenDisabled)
return;
disabled = false;
Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Sounds", "EnablePower", self.Owner.Country.Race);
power.UpdateActor(self);
}
}
}

View File

@@ -18,14 +18,14 @@ namespace OpenRA.Mods.Common.Power
public object Create(ActorInitializer init) { return new ScalePowerWithHealth(init.self); }
}
public class ScalePowerWithHealth : IPowerModifier, INotifyDamage
public class ScalePowerWithHealth : IPowerModifier, INotifyDamage, INotifyOwnerChanged
{
readonly Power power;
readonly Health health;
PowerManager power;
public ScalePowerWithHealth(Actor self)
{
power = self.Trait<Power>();
power = self.Owner.PlayerActor.Trait<PowerManager>();
health = self.Trait<Health>();
}
@@ -34,9 +34,10 @@ namespace OpenRA.Mods.Common.Power
return 100 * health.HP / health.MaxHP;
}
public void Damaged(Actor self, AttackInfo e)
public void Damaged(Actor self, AttackInfo e) { power.UpdateActor(self); }
public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{
power.PlayerPower.UpdateActor(self);
power = newOwner.PlayerActor.Trait<PowerManager>();
}
}
}