From 7f8607e85b99c0520c21e554990908d575d126ef Mon Sep 17 00:00:00 2001 From: reaperrr Date: Fri, 12 Aug 2016 23:44:34 +0200 Subject: [PATCH] Fix granting upgrades on initial damage state Previously the upgrade(s) would only be granted when the damage state changed, regardless of whether the initial DamageState was already valid. This prevented the trait from working on Undamaged actors that had just been created, for example. --- .../Traits/Upgrades/UpgradeOnDamage.cs | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Upgrades/UpgradeOnDamage.cs b/OpenRA.Mods.Common/Traits/Upgrades/UpgradeOnDamage.cs index a66f9fd180..6d3bdda602 100644 --- a/OpenRA.Mods.Common/Traits/Upgrades/UpgradeOnDamage.cs +++ b/OpenRA.Mods.Common/Traits/Upgrades/UpgradeOnDamage.cs @@ -14,7 +14,7 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { [Desc("Applies an upgrade to the actor at specified damage states.")] - public class UpgradeOnDamageInfo : ITraitInfo, Requires + public class UpgradeOnDamageInfo : ITraitInfo, Requires, Requires { [UpgradeGrantedReference, FieldLoader.Require] [Desc("The upgrades to grant.")] @@ -35,16 +35,36 @@ namespace OpenRA.Mods.Common.Traits public object Create(ActorInitializer init) { return new UpgradeOnDamage(init.Self, this); } } - public class UpgradeOnDamage : INotifyDamageStateChanged + public class UpgradeOnDamage : INotifyDamageStateChanged, INotifyCreated { readonly UpgradeOnDamageInfo info; readonly UpgradeManager um; + readonly Health health; bool granted; public UpgradeOnDamage(Actor self, UpgradeOnDamageInfo info) { this.info = info; um = self.Trait(); + health = self.Trait(); + } + + void INotifyCreated.Created(Actor self) + { + GrantUpgradeOnValidDamageState(self, health.DamageState); + } + + void GrantUpgradeOnValidDamageState(Actor self, DamageState state) + { + if (!info.ValidDamageStates.HasFlag(state)) + return; + + granted = true; + var rand = Game.CosmeticRandom; + var sound = info.EnabledSounds.RandomOrDefault(rand); + Game.Sound.Play(sound, self.CenterPosition); + foreach (var u in info.Upgrades) + um.GrantUpgrade(self, u, this); } void INotifyDamageStateChanged.DamageStateChanged(Actor self, AttackInfo e) @@ -52,18 +72,12 @@ namespace OpenRA.Mods.Common.Traits if (granted && info.GrantPermanently) return; - var rand = Game.CosmeticRandom; - if (!granted && info.ValidDamageStates.HasFlag(e.DamageState) && !info.ValidDamageStates.HasFlag(e.PreviousDamageState)) - { - granted = true; - var sound = info.EnabledSounds.RandomOrDefault(rand); - Game.Sound.Play(sound, self.CenterPosition); - foreach (var u in info.Upgrades) - um.GrantUpgrade(self, u, this); - } + if (!granted && !info.ValidDamageStates.HasFlag(e.PreviousDamageState)) + GrantUpgradeOnValidDamageState(self, health.DamageState); else if (granted && !info.ValidDamageStates.HasFlag(e.DamageState) && info.ValidDamageStates.HasFlag(e.PreviousDamageState)) { granted = false; + var rand = Game.CosmeticRandom; var sound = info.DisabledSounds.RandomOrDefault(rand); Game.Sound.Play(sound, self.CenterPosition); foreach (var u in info.Upgrades)