Files
Kursach_StealthAction/Assets/Scripts/MutantHealth.cs
T
2026-10-24 20:54:38 +03:00

82 lines
1.8 KiB
C#

using UnityEngine;
public class MutantHealth : MonoBehaviour
{
public float maxHealth = 50f;
private float currentHealth;
public bool IsDead => currentHealth <= 0;
public float CurrentHealth => currentHealth;
public System.Action<float, float> OnHealthChanged;
public System.Action OnDied;
private BlindMutantAI mutantAI;
void Awake()
{
currentHealth = maxHealth;
mutantAI = GetComponent<BlindMutantAI>();
}
public void SetMaxHealthForTest(float val)
{
maxHealth = val;
currentHealth = val;
}
public void TakeDamage(float damage, Transform attacker = null)
{
if (IsDead) return;
currentHealth -= damage;
Debug.Log($"Мутант получил урон: {damage}! Осталось: {currentHealth}");
OnHealthChanged?.Invoke(currentHealth, maxHealth);
if (mutantAI != null && attacker != null)
{
mutantAI.OnDamagedByPlayer(attacker);
}
if (currentHealth <= 0)
{
Die();
}
}
private void Die()
{
Debug.Log("МУТАНТ УМЕР!");
OnDied?.Invoke();
var anim = GetComponentInChildren<Animator>();
if (anim != null)
{
anim.SetBool("IsDead", true);
anim.SetTrigger("Die");
}
if (mutantAI != null)
{
mutantAI.enabled = false;
}
var agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
if (agent != null)
{
agent.isStopped = true;
agent.enabled = false;
}
var col = GetComponent<Collider>();
if (col != null)
{
col.enabled = false;
}
if (Application.isPlaying)
{
Destroy(gameObject, 3.5f);
}
}
}