/
KasatkinGV
/
CourseWork
Обзор
Документация
Войти
/
KasatkinGV
/
CourseWork
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
PlayerHealth.cs
84 строки
2 KB
KasatkinGV
upload files
11 июн 2025, 01:06
11 июн 2025, 01:06
3efd0d1
Код
Авторство
О чём код?
using UnityEngine; using UnityEngine.SceneManagement; using System.Collections; public class PlayerHealth : MonoBehaviour { [SerializeField] private PauseMenuController pauseMenuController; [SerializeField] private HealthBarUI healthUI; private bool isBlocked; private int healAmount = 35; private Player player; private Animator animator; private PlayerBlock playerBlock; private PlayerInventory playerInventory; private Rigidbody2D rb; public bool IsHurted { get; private set; } private void Start() { player = GetComponent<Player>(); animator = GetComponent<Animator>(); playerBlock = GetComponent<PlayerBlock>(); playerInventory = GetComponent<PlayerInventory>(); rb = GetComponent<Rigidbody2D>(); } private void Update() { isBlocked = playerBlock.IsBlocked; } public void Heal() { if (playerInventory.PotionsCount > 0 && player.Health < 100) { playerInventory.UsePotion(); player.Health = Mathf.Clamp(player.Health + healAmount, 0, 100); healthUI.UpdateBar(player.Health); } } public void TakeDamage(int amount) { if (isBlocked) { int reducedDamage = Mathf.RoundToInt(amount * 0.3f); player.Health -= reducedDamage; } else { player.Health -= amount; } healthUI.UpdateBar(player.Health); if (player.Health > 0) { IsHurted = true; animator.SetTrigger("isHurted"); StartCoroutine(ResetHurtStatus()); } else { animator.SetBool("isDied", true); rb.constraints = RigidbodyConstraints2D.FreezeAll; StartCoroutine(RespawnAfterDeath()); } } private IEnumerator ResetHurtStatus() { yield return new WaitForSeconds(0.3f); IsHurted = false; } private IEnumerator RespawnAfterDeath() { yield return new WaitForSeconds(0.9f); pauseMenuController.ShowDeathMenu(); } }