Загрузка данных
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
[Header("Здоровье")]
public int maxHealth = 100;
public int currentHealth;
[Header("Интерфейс (UI)")]
public Slider healthSlider;
[Header("Визуальный отклик урона")]
public Renderer playerRenderer;
public Color damageFlashColor = Color.red;
public float flashDuration = 0.15f;
private Color originalColor;
[Header("Настройки возрождения")]
public float respawnTime = 3f;
private Vector3 spawnPosition;
private Quaternion spawnRotation;
[Header("Состояние")]
public bool isDead = false;
// Ссылки на компоненты
private CharacterController controller;
private PlayerMove playerMove;
private PlayerAttack playerAttack;
private Animator anim;
void Start()
{
currentHealth = maxHealth;
controller = GetComponent<CharacterController>();
playerMove = GetComponent<PlayerMove>();
playerAttack = GetComponent<PlayerAttack>();
anim = GetComponent<Animator>();
spawnPosition = transform.position;
spawnRotation = transform.rotation;
if (healthSlider != null)
{
healthSlider.maxValue = maxHealth;
healthSlider.value = currentHealth;
}
if (playerRenderer == null)
{
playerRenderer = GetComponentInChildren<Renderer>();
}
if (playerRenderer != null && playerRenderer.material.HasProperty("_Color"))
{
originalColor = playerRenderer.material.color;
}
}
void Update()
{
// Тест урона на H
if (Input.GetKeyDown(KeyCode.H) && !isDead)
{
TakeDamage(35);
}
}
public void TakeDamage(int damage)
{
if (isDead) return;
currentHealth -= damage;
currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
Debug.Log($"{gameObject.name} получил {damage} урона. Осталось HP: {currentHealth}");
if (healthSlider != null)
{
healthSlider.value = currentHealth;
}
// Вспышка текстуры
if (playerRenderer != null)
{
StopCoroutine("FlashRoutine");
StartCoroutine(FlashRoutine());
}
// Тряска камеры от получения урона батей
if (CameraShake.Instance != null)
{
CameraShake.Instance.Shake(0.18f, 0.25f);
}
if (currentHealth <= 0)
{
Die();
}
}
private IEnumerator FlashRoutine()
{
playerRenderer.material.color = damageFlashColor;
yield return new WaitForSeconds(flashDuration);
playerRenderer.material.color = originalColor;
}
void Die()
{
if (isDead) return;
isDead = true;
Debug.Log($"{gameObject.name} погиб!");
// Отключаем физику капсулы сразу, чтобы персонаж ровно ложился на снег
if (controller != null) controller.enabled = false;
if (playerMove != null) playerMove.enabled = false;
if (playerAttack != null) playerAttack.enabled = false;
if (anim != null)
{
anim.SetTrigger("Die");
}
if (DeathScreenUI.Instance != null)
{
DeathScreenUI.Instance.ShowDeathScreen(respawnTime);
}
StartCoroutine(RespawnRoutine());
}
private IEnumerator RespawnRoutine()
{
yield return new WaitForSeconds(respawnTime);
if (DeathScreenUI.Instance != null)
{
DeathScreenUI.Instance.HideDeathScreen();
}
currentHealth = maxHealth;
if (healthSlider != null)
{
healthSlider.value = maxHealth;
}
// Возврат на координаты спавна
if (controller != null) controller.enabled = false;
transform.position = spawnPosition;
transform.rotation = spawnRotation;
if (controller != null) controller.enabled = true;
// Сброс дерева анимаций
if (anim != null)
{
anim.Rebind();
anim.Update(0f);
}
if (playerMove != null) playerMove.enabled = true;
if (playerAttack != null) playerAttack.enabled = true;
isDead = false;
Debug.Log($"{gameObject.name} возродился!");
}
}