Created IHealth interface and cleanup.

To decouple the Health trait from OpenRA.Traits.
DisplayHp renamed to DisplayHP and HealthExts moved
moved next to Actor.Kill() for consistency.
This commit is contained in:
Huw Pascoe
2015-10-11 01:12:08 +01:00
parent a79b71c608
commit 5a81d5dfa7
4 changed files with 53 additions and 40 deletions

View File

@@ -26,15 +26,13 @@ namespace OpenRA.Traits
public virtual object Create(ActorInitializer init) { return new Health(init, this); }
}
public enum DamageState { Undamaged, Light, Medium, Heavy, Critical, Dead }
public class Health : ISync, ITick
public class Health : IHealth, ISync, ITick
{
public readonly HealthInfo Info;
[Sync] int hp;
public int DisplayHp { get; private set; }
public int DisplayHP { get; private set; }
public Health(ActorInitializer init, HealthInfo info)
{
@@ -43,11 +41,11 @@ namespace OpenRA.Traits
hp = init.Contains<HealthInit>() ? init.Get<HealthInit, int>() * MaxHP / 100 : MaxHP;
DisplayHp = hp;
DisplayHP = hp;
}
public int HP { get { return hp; } }
public int MaxHP;
public int MaxHP { get; private set; }
public bool IsDead { get { return hp <= 0; } }
public bool RemoveOnDeath = true;
@@ -162,13 +160,18 @@ namespace OpenRA.Traits
}
}
public void Kill(Actor self, Actor attacker)
{
InflictDamage(self, attacker, MaxHP, null, true);
}
public void Tick(Actor self)
{
if (hp > DisplayHp)
DisplayHp = hp;
if (hp > DisplayHP)
DisplayHP = hp;
if (DisplayHp > hp)
DisplayHp = (2 * DisplayHp + hp) / 3;
if (DisplayHP > hp)
DisplayHP = (2 * DisplayHP + hp) / 3;
}
}
@@ -191,24 +194,4 @@ namespace OpenRA.Traits
return value;
}
}
public static class HealthExts
{
public static DamageState GetDamageState(this Actor self)
{
if (self.Disposed)
return DamageState.Dead;
var health = self.TraitOrDefault<Health>();
return (health == null) ? DamageState.Undamaged : health.DamageState;
}
public static void InflictDamage(this Actor self, Actor attacker, int damage, IWarhead warhead)
{
if (self.Disposed) return;
var health = self.TraitOrDefault<Health>();
if (health == null) return;
health.InflictDamage(self, attacker, damage, warhead, false);
}
}
}

View File

@@ -19,6 +19,20 @@ using OpenRA.Primitives;
namespace OpenRA.Traits
{
public enum DamageState { Undamaged, Light, Medium, Heavy, Critical, Dead }
public interface IHealth
{
DamageState DamageState { get; }
int HP { get; }
int MaxHP { get; }
int DisplayHP { get; }
bool IsDead { get; }
void InflictDamage(Actor self, Actor attacker, int damage, IWarhead warhead, bool ignoreModifiers);
void Kill(Actor self, Actor attacker);
}
// depends on the order of pips in WorldRenderer.cs!
public enum PipType { Transparent, Green, Yellow, Red, Gray, Blue, Ammo, AmmoEmpty }
public enum TagType { None, Fake, Primary }