Files
dungeons/Assets/Scripts/Components/HealthComponent.cs

60 lines
975 B
C#

using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class HealthComponent : MonoBehaviour
{
[SerializeField]
[Required]
[OnValueChanged("Setup")]
private Enemy enemy;
[ReadOnly]
[SerializeField]
private int health;
[ReadOnly]
[SerializeField]
private bool isDead;
public UnityEvent onDied;
[Button]
public void Setup()
{
health = enemy.health;
isDead = false;
}
public void TakeDamage(int damage)
{
health -= damage;
if (health < 0)
{
health = 0;
}
if (health == 0)
{
isDead = true;
onDied.Invoke();
}
}
[Button]
private void Take10Damage()
{
TakeDamage(10);
}
[Button]
private void Kill()
{
TakeDamage(health);
}
}