Files
Kursach_StealthAction/Assets/Scripts/MutantHealth.cs
T

73 lines
1.6 KiB
C#
Raw Normal View History

2026-10-19 20:14:24 +03:00
using UnityEngine;
2026-10-11 19:16:11 +03:00
public class MutantHealth : MonoBehaviour
2026-10-11 19:15:15 +03:00
{
2026-10-11 19:16:11 +03:00
public float maxHealth = 50f;
private float currentHealth;
2026-10-19 20:14:24 +03:00
public bool IsDead => currentHealth <= 0;
public float CurrentHealth => currentHealth;
2026-10-11 19:16:11 +03:00
2026-10-23 15:44:09 +03:00
public System.Action<float, float> OnHealthChanged;
public System.Action OnDied;
2026-10-19 20:14:24 +03:00
private BlindMutantAI mutantAI;
void Awake()
2026-10-11 19:15:15 +03:00
{
2026-10-19 20:14:24 +03:00
currentHealth = maxHealth;
mutantAI = GetComponent<BlindMutantAI>();
}
public void TakeDamage(float damage, Transform attacker = null)
{
if (IsDead) return;
2026-10-11 19:16:11 +03:00
currentHealth -= damage;
2026-10-19 20:14:24 +03:00
Debug.Log($"Мутант получил урон: {damage}! Осталось: {currentHealth}");
2026-10-23 15:44:09 +03:00
OnHealthChanged?.Invoke(currentHealth, maxHealth);
2026-10-19 20:14:24 +03:00
if (mutantAI != null && attacker != null)
{
mutantAI.OnDamagedByPlayer(attacker);
}
2026-10-11 19:16:11 +03:00
if (currentHealth <= 0)
{
Die();
2026-10-11 19:16:11 +03:00
}
2026-10-11 19:15:15 +03:00
}
private void Die()
{
Debug.Log("МУТАНТ УМЕР!");
2026-10-23 15:44:09 +03:00
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;
}
Destroy(gameObject, 3.5f);
}
2026-10-11 19:15:15 +03:00
}