TurnBasedStrategyCourse/Assets/Scripts/HealthSystem.cs

26 lines
646 B
C#

using System;
using UnityEngine;
using UnityEngine.Serialization;
public class HealthSystem : MonoBehaviour {
private int health;
[SerializeField] private int maxHealth = 100;
public float HealthNormalized => (float)health / maxHealth;
public event EventHandler OnDead;
public event EventHandler OnDamage;
private void Awake() => health = maxHealth;
public void Damage(int damageAmount) {
health -= damageAmount;
if (health < 0) health = 0;
if (health == 0) Die();
OnDamage?.Invoke(this, EventArgs.Empty);
}
private void Die() => OnDead?.Invoke(this, EventArgs.Empty);
}