71 lines
2.0 KiB
C#
71 lines
2.0 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using GameFramework;
|
|
namespace IndianOceanAssets.Engine2_5D
|
|
{
|
|
// Handles health, damage, and death for entities
|
|
public class HealthSystem : MonoBehaviour
|
|
{
|
|
/// <summary>玩家受伤时触发(用于受击特效等)</summary>
|
|
public static Action onPlayerDamaged;
|
|
|
|
[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 (isPlayer && onPlayerDamaged != null)
|
|
onPlayerDamaged.Invoke();
|
|
|
|
// 播放受伤音效
|
|
if (isPlayer && AudioManager.Instance != null)
|
|
AudioManager.Instance.PlaySFXFromResources("SFX/Hurt", 0.7f);
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
} |