1 #region Copyright & License Information 2 /* 3 * Copyright 2007-2020 The OpenRA Developers (see AUTHORS) 4 * This file is part of OpenRA, which is free software. It is made 5 * available to you under the terms of the GNU General Public License 6 * as published by the Free Software Foundation, either version 3 of 7 * the License, or (at your option) any later version. For more 8 * information, see COPYING. 9 */ 10 #endregion 11 12 using OpenRA.Traits; 13 14 namespace OpenRA.Mods.Common.Traits 15 { 16 [Desc("Scale power amount with the current health.")] 17 public class ScalePowerWithHealthInfo : ITraitInfo, Requires<PowerInfo>, Requires<IHealthInfo> 18 { Create(ActorInitializer init)19 public object Create(ActorInitializer init) { return new ScalePowerWithHealth(init.Self); } 20 } 21 22 public class ScalePowerWithHealth : IPowerModifier, INotifyDamage, INotifyOwnerChanged 23 { 24 readonly IHealth health; 25 PowerManager power; 26 ScalePowerWithHealth(Actor self)27 public ScalePowerWithHealth(Actor self) 28 { 29 power = self.Owner.PlayerActor.Trait<PowerManager>(); 30 health = self.Trait<IHealth>(); 31 } 32 IPowerModifier.GetPowerModifier()33 int IPowerModifier.GetPowerModifier() 34 { 35 // Cast to long to avoid overflow when multiplying by the health 36 return (int)(100L * health.HP / health.MaxHP); 37 } 38 INotifyDamage.Damaged(Actor self, AttackInfo e)39 void INotifyDamage.Damaged(Actor self, AttackInfo e) { power.UpdateActor(self); } 40 INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)41 void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) 42 { 43 power = newOwner.PlayerActor.Trait<PowerManager>(); 44 } 45 } 46 } 47