Files
2026-10-20 15:21:09 +08:00

65 lines
1.6 KiB
C#

using System;
using UnityEngine;
using CharacterMovement;
// 機器人的血量 : 掛在機器人根物件上
public class BotHealth : MonoBehaviour
{
[Header("血量")]
public float maxHealth = 500f;
[Header("死亡")]
[Tooltip("機器人模型上的 Animator,死亡時觸發 Die")]
public Animator animator;
[Tooltip("倒下後多久移除 (秒)")]
public float destroyDelay = 3f;
public float Health { get; private set; }
public bool IsDead { get; private set; }
// 給 BotSpawner 用 : 倒下時通知,準備重生下一隻
public event Action<BotHealth> Died;
void Awake()
{
Health = maxHealth;
}
public void TakeDamage(float amount, BodyPart part)
{
if (IsDead) return;
Health = Mathf.Max(0f, Health - amount);
if (Health <= 0f)
{
Die();
}
}
void Die()
{
IsDead = true;
// 停止移動與 AI
if (TryGetComponent(out BotBrain brain)) brain.enabled = false;
if (TryGetComponent(out PlayerController controller)) controller.enabled = false;
if (TryGetComponent(out Rigidbody body))
{
body.linearVelocity = Vector3.zero;
body.isKinematic = true;
}
// 關掉命中框,倒下的機器人不會擋子彈
foreach (Hitbox hitbox in GetComponentsInChildren<Hitbox>())
{
if (hitbox.TryGetComponent(out Collider col)) col.enabled = false;
}
if (animator != null) animator.SetTrigger("Die");
Died?.Invoke(this);
Destroy(gameObject, destroyDelay);
}
}