TigerProject/Assets/Scripts/Monster/Entity.cs

63 lines
1.1 KiB
C#
Raw Normal View History

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