AI/Agents/Agent.Health.cs
using System;
namespace HC3;
public partial class Agent : Component.IDamageable
{
/// <summary>
/// The agent's health. If this reaches 0, the agent is considered dead.
/// </summary>
[Sync, Group( "Health" )]
public int Health { get; set; } = 100;
[Property, Group( "Health" )]
public int MaxHealth { get; set; } = 100;
[Property, Group( "Health" )]
public GameObject BloodSplatter { get; set; }
/// <summary>
/// Is this agent alive?
/// </summary>
public bool IsAlive => Health > 0;
/// <summary>
/// Implement IDamageable interface
/// </summary>
void IDamageable.OnDamage( in DamageInfo damage )
{
int damageAmount = (int)damage.Damage;
Damage( damageAmount );
if ( BloodSplatter.IsValid() )
{
BloodSplatter?.Clone( damage.Position + Vector3.Up * 16f );
}
}
/// <summary>
/// Apply damage to this agent
/// </summary>
public void Damage( int damage )
{
if ( !IsAlive || damage <= 0 )
return;
Health -= damage;
Health = Math.Max( 0, MaxHealth );
OnDamaged();
if ( Health <= 0 )
{
Kill();
}
}
/// <summary>
/// Called when the agent takes damage
/// </summary>
protected virtual void OnDamaged()
{
}
/// <summary>
/// Kill this agent immediately
/// </summary>
public void Kill()
{
if ( !IsAlive )
return;
Health = 0;
OnKilled();
}
/// <summary>
/// Called when the agent is killed
/// </summary>
protected virtual void OnKilled()
{
}
}