diff --git a/OpenRA.Mods.Common/Traits/Cloak.cs b/OpenRA.Mods.Common/Traits/Cloak.cs index 5f67f8327e..95860b4ed6 100644 --- a/OpenRA.Mods.Common/Traits/Cloak.cs +++ b/OpenRA.Mods.Common/Traits/Cloak.cs @@ -38,13 +38,18 @@ namespace OpenRA.Mods.Common.Traits public readonly string[] CloakTypes = { "Cloak" }; + [UpgradeGrantedReference] + [Desc("The upgrades to grant to self while cloaked.")] + public readonly string[] WhileCloakedUpgrades = { }; + public override object Create(ActorInitializer init) { return new Cloak(init.Self, this); } } - public class Cloak : UpgradableTrait, IRenderModifier, INotifyDamageStateChanged, INotifyAttack, ITick, IVisibilityModifier, IRadarColorModifier + public class Cloak : UpgradableTrait, IRenderModifier, INotifyDamageStateChanged, INotifyAttack, ITick, IVisibilityModifier, IRadarColorModifier, INotifyCreated { [Sync] int remainingTime; [Sync] bool damageDisabled; + UpgradeManager upgradeManager; Actor self; CPos? lastPos; @@ -57,6 +62,17 @@ namespace OpenRA.Mods.Common.Traits remainingTime = info.InitialDelay; } + public void Created(Actor self) + { + upgradeManager = self.TraitOrDefault(); + if (remainingTime == 0) + { + if (upgradeManager != null) + foreach (var u in Info.WhileCloakedUpgrades) + upgradeManager.GrantUpgrade(self, u, this); + } + } + protected override void UpgradeDisabled(Actor self) { Uncloak(); @@ -68,7 +84,12 @@ namespace OpenRA.Mods.Common.Traits public void Uncloak(int time) { if (Cloaked) + { Sound.Play(Info.UncloakSound, self.CenterPosition); + if (upgradeManager != null) + foreach (var u in Info.WhileCloakedUpgrades) + upgradeManager.RevokeUpgrade(self, u, this); + } remainingTime = Math.Max(remainingTime, time); } @@ -106,7 +127,12 @@ namespace OpenRA.Mods.Common.Traits return; if (remainingTime > 0 && !IsTraitDisabled && !damageDisabled && --remainingTime <= 0) + { Sound.Play(Info.CloakSound, self.CenterPosition); + if (upgradeManager != null) + foreach (var u in Info.WhileCloakedUpgrades) + upgradeManager.GrantUpgrade(self, u, this); + } if (self.IsDisabled()) Uncloak();