73 lines
1.6 KiB
C#
73 lines
1.6 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 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;
|
|
}
|
|
|
|
Destroy(gameObject, 3.5f);
|
|
}
|
|
} |