From c0850e44f43e88e5be08017dd77af86ac41c3cb4 Mon Sep 17 00:00:00 2001 From: DArcy Rush Date: Thu, 22 Oct 2015 23:56:07 +0100 Subject: [PATCH] Explodes trait has new 'DamageThreshold' property. An actor will explode when its health is below the defined percentage of its max health. --- OpenRA.Mods.Common/Traits/Explodes.cs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Explodes.cs b/OpenRA.Mods.Common/Traits/Explodes.cs index 4eaa18c33a..3bc55dcd95 100644 --- a/OpenRA.Mods.Common/Traits/Explodes.cs +++ b/OpenRA.Mods.Common/Traits/Explodes.cs @@ -18,7 +18,7 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { [Desc("This actor explodes when killed.")] - public class ExplodesInfo : ITraitInfo, IRulesetLoaded + public class ExplodesInfo : ITraitInfo, IRulesetLoaded, Requires { [WeaponReference, FieldLoader.Require, Desc("Weapon to use for explosion if ammo/payload is loaded.")] public readonly string Weapon = "UnitExplode"; @@ -32,13 +32,16 @@ namespace OpenRA.Mods.Common.Traits [Desc("Chance that this actor will explode at all.")] public readonly int Chance = 100; + [Desc("Health level at which actor will explode.")] + public readonly int DamageThreshold = 0; + [Desc("DeathType(s) to apply upon explosion.")] public readonly HashSet DeathType = new HashSet(); public WeaponInfo WeaponInfo { get; private set; } public WeaponInfo EmptyWeaponInfo { get; private set; } - public object Create(ActorInitializer init) { return new Explodes(this); } + public object Create(ActorInitializer init) { return new Explodes(this, init.Self); } public void RulesetLoaded(Ruleset rules, ActorInfo ai) { WeaponInfo = string.IsNullOrEmpty(Weapon) ? null : rules.Weapons[Weapon.ToLowerInvariant()]; @@ -46,11 +49,17 @@ namespace OpenRA.Mods.Common.Traits } } - public class Explodes : INotifyKilled + public class Explodes : INotifyKilled, INotifyDamage { readonly ExplodesInfo info; - public Explodes(ExplodesInfo info) { this.info = info; } + readonly Health health; + + public Explodes(ExplodesInfo info, Actor self) + { + this.info = info; + health = self.Trait(); + } public void Killed(Actor self, AttackInfo e) { @@ -81,5 +90,14 @@ namespace OpenRA.Mods.Common.Traits var useFullExplosion = self.World.SharedRandom.Next(100) <= info.LoadedChance; return (shouldExplode && useFullExplosion) ? info.WeaponInfo : info.EmptyWeaponInfo; } + + public void Damaged(Actor self, AttackInfo e) + { + if (info.DamageThreshold == 0) + return; + + if (health.HP * 100 < info.DamageThreshold * health.MaxHP) + self.World.AddFrameEndTask(w => self.Kill(e.Attacker)); + } } }