50 lines
1.0 KiB
C#
50 lines
1.0 KiB
C#
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.Events;
|
||
|
|
|
||
|
|
public class Entity : MonoBehaviour
|
||
|
|
{
|
||
|
|
public float maxHealth { get; private set; }
|
||
|
|
public float currentHealth { get; private set; }
|
||
|
|
public float attackDamage { get; private set; }
|
||
|
|
|
||
|
|
public UnityEvent OnDeath;
|
||
|
|
public UnityEvent<float> OnTakeDamage;
|
||
|
|
|
||
|
|
|
||
|
|
[SerializeField] private EntityData data;
|
||
|
|
|
||
|
|
void Awake()
|
||
|
|
{
|
||
|
|
maxHealth = data.maxHealth;
|
||
|
|
currentHealth = data.maxHealth;
|
||
|
|
attackDamage = data.attackDamage;
|
||
|
|
}
|
||
|
|
|
||
|
|
public virtual void TakeDaamge(float damage)
|
||
|
|
{
|
||
|
|
currentHealth -= damage;
|
||
|
|
OnTakeDamage?.Invoke(damage);
|
||
|
|
if (currentHealth <= 0)
|
||
|
|
{
|
||
|
|
currentHealth = 0;
|
||
|
|
Die();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
protected virtual void Die()
|
||
|
|
{
|
||
|
|
Destroy(gameObject);
|
||
|
|
OnDeath?.Invoke();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Heal(float heal)
|
||
|
|
{
|
||
|
|
if (heal < 0) return;
|
||
|
|
currentHealth += heal;
|
||
|
|
if(currentHealth > maxHealth)
|
||
|
|
{
|
||
|
|
currentHealth = maxHealth;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|