Kursach_StealthAction/Assets/Scripts/MutantHealth.cs

82 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}
}