TigerProject/Assets/Scripts/Monster/Entity.cs

63 lines
1.1 KiB
C#

using System;
using UnityEngine;
using UnityEngine.Events;
public class Entity : MonoBehaviour
{
protected int maxHp;
protected int currentHp;
protected int attackDamage;
private bool isSuicided = false;
[SerializeField] protected EntityData data;
public static event Action<int> OnEnemyDeathEvent;
protected virtual void Awake()
{
if (data != null)
{
maxHp = data.maxHealth;
currentHp = data.maxHealth;
attackDamage = data.attackDamage;
}
}
public virtual void TakeDamage(int damage)
{
currentHp -= damage;
if (currentHp <= 0)
{
Die();
}
}
protected virtual void Die()
{
currentHp = 0;
if (!isSuicided)
{
OnEnemyDeathEvent?.Invoke(1);
}
Destroy(gameObject);
}
public virtual void OnHitPlayer(Player player)
{
player.TakeDamage(attackDamage);
isSuicided = true;
Die();
}
public virtual void RunAI()
{
}
void Update()
{
RunAI();
}
}