Files
gold_dolphin/unity/Assets/2.5D Engine/Scripts/HealthSystem.cs
T
2026-07-04 15:50:39 +08:00

59 lines
1.6 KiB
C#

using UnityEngine;
using GameFramework;
namespace IndianOceanAssets.Engine2_5D
{
// Handles health, damage, and death for entities
public class HealthSystem : MonoBehaviour
{
[Range(1, 100)]
[SerializeField]
private int maxHealth = 5; // Maximum health
private int health = 5; // Current health
[SerializeField]
private GameObject deathEffect; // Effect prefab on death
[SerializeField]
private bool isPlayer; // Is this the player?
private bool _isDead = false; // 防止重复触发死亡
// Initializes health
private void Start()
{
health = maxHealth;
}
// Applies damage and checks for death
public void Damage(int damageAmount)
{
if (_isDead) return; // 已死亡,不再处理
health -= damageAmount;
// If dead
if (health <= 0)
{
_isDead = true;
if (isPlayer)
{
// 玩家死亡:触发过场动画,不立即销毁(由 GameManager 处理)
GameManager.GameOver();
}
else
{
// 非玩家:正常播放死亡特效并销毁
Die();
}
}
}
// Handles death logic and effects
public void Die()
{
Instantiate(deathEffect, transform.position + new Vector3(0f, .5f, 0f), Quaternion.identity);
Destroy(gameObject);
}
}
}