using System; using UnityEngine; public sealed class DamageableActor : MonoBehaviour { [SerializeField, Min(1)] private int startingHealth = 10; private HealthModel health; private void Awake() { health = new HealthModel(Mathf.Max(1, startingHealth)); } [ContextMenu("Apply Three Damage")] public void ApplyThreeDamage() { if (!Application.isPlaying || health == null) return; health.Damage(3); Debug.Log("Health=" + health.Current + ", Alive=" + health.Alive, this); } public sealed class HealthModel { public int Current { get; private set; } public bool Alive { get { return Current > 0; } } public HealthModel(int maximum) { if (maximum < 1) throw new ArgumentOutOfRangeException(nameof(maximum)); Current = maximum; } public void Damage(int amount) { if (amount < 0) throw new ArgumentOutOfRangeException(nameof(amount)); Current = Math.Max(0, Current - amount); } } }