Combat/HealthComponent.cs
using System;

namespace Prototype;

public sealed class HealthComponent : Component
{
	[Sync] public float Health { get; set; } = 100f;
	[Sync] public float MaxHealth { get; set; } = 100f;
	public bool IsDead => Health <= 0f;
	public Action<float, GameObject> Damaged;
	public Action<GameObject> Died;

	public void ResetTo( float amount ) { MaxHealth = amount; Health = amount; }

	public void ApplyDamage( float amount, GameObject source )
	{
		if ( !float.IsFinite(amount) || amount <= 0f || IsDead ) return;
		if ( IsProxy && Components.Get<EnemyBrain>().IsValid() ) return;
		Health = MathF.Max( 0f, Health - amount );
		Damaged?.Invoke( amount, source );
		if ( IsDead ) Died?.Invoke( source );
	}
}