Kursach_StealthAction/Assets/Scripts/PlayerHealth.cs

101 lines
3.7 KiB
C#
Raw Permalink 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 System.Collections;
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public float maxHealth = 100f;
private float currentHealth = 100f;
public bool IsDead => currentHealth <= 0;
public float CurrentHealth => currentHealth;
[Header("Компоненты персонажа (автоматически)")]
public Animator playerAnimator;
public CharacterController characterController;
public PlayerMovement playerMovement;
public PlayerGun playerGun;
public PlayerAttack playerAttack;
public EchoTrigger echoTrigger;
[Header("Экран смерти")]
public DeathScreenUI deathScreenUI;
[Tooltip("Пауза перед показом экрана смерти для проигрывания анимации падения")]
public float deathScreenDelay = 1.2f;
[Header("Защита от мгновенного урона толпой")]
[Tooltip("Минимальная задержка между получением урона от ударов врагов")]
public float damageCooldown = 0.35f;
private float lastDamageTime = -10f;
private bool hasDied = false;
void Start()
{
currentHealth = maxHealth;
if (playerAnimator == null) playerAnimator = GetComponentInChildren<Animator>();
if (characterController == null) characterController = GetComponent<CharacterController>();
if (playerMovement == null) playerMovement = GetComponent<PlayerMovement>();
if (playerGun == null) playerGun = GetComponent<PlayerGun>();
if (playerAttack == null) playerAttack = GetComponent<PlayerAttack>();
if (echoTrigger == null) echoTrigger = GetComponent<EchoTrigger>();
if (deathScreenUI == null) deathScreenUI = FindAnyObjectByType<DeathScreenUI>();
}
public void TakeDamage(float damage)
{
if (IsDead || hasDied) return;
if (Time.time < lastDamageTime + damageCooldown) return;
lastDamageTime = Time.time;
currentHealth -= damage;
Debug.Log($"Игрок получил урон! Осталось здоровья: {currentHealth}");
if (currentHealth <= 0f)
{
currentHealth = 0f;
Die();
}
}
public void Die()
{
if (hasDied) return;
hasDied = true;
Debug.Log("<color=red>[PlayerHealth] ИГРОК ПОГИБ! Запуск экрана смерти.</color>");
// Отключаем управление и физику персонажа
if (playerMovement != null) playerMovement.enabled = false;
if (playerGun != null) playerGun.enabled = false;
if (playerAttack != null) playerAttack.enabled = false;
if (echoTrigger != null) echoTrigger.enabled = false;
if (characterController != null) characterController.enabled = false;
// Отключаем слой удержания оружия и запускаем анимацию смерти
if (playerAnimator != null)
{
if (playerAnimator.layerCount > 1)
{
playerAnimator.SetLayerWeight(1, 0f);
}
playerAnimator.SetTrigger("Die");
}
// Показываем экран смерти с кинематографичной задержкой
StartCoroutine(ShowDeathScreenDelayed());
}
private IEnumerator ShowDeathScreenDelayed()
{
yield return new WaitForSeconds(deathScreenDelay);
if (deathScreenUI != null)
{
deathScreenUI.ShowDeathScreen();
}
else if (DeathScreenUI.Instance != null)
{
DeathScreenUI.Instance.ShowDeathScreen();
}
}
}