85 lines
1.9 KiB
C#
85 lines
1.9 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
public class PlayerHealth : MonoBehaviour
|
|
{
|
|
public int health = 100;
|
|
|
|
public bool IsAlive => health > 0;
|
|
public bool HasCompletedLevel => levelCompleted;
|
|
public event Action Died;
|
|
public event Action LevelCompleted;
|
|
|
|
private bool hasTriggeredDeath;
|
|
private bool levelCompleted;
|
|
|
|
public void TakeDamage(int amount)
|
|
{
|
|
if (!IsAlive)
|
|
{
|
|
return;
|
|
}
|
|
|
|
health = Mathf.Max(0, health - Mathf.Abs(amount));
|
|
|
|
if (!IsAlive && !hasTriggeredDeath)
|
|
{
|
|
HandleDeath();
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Time.timeScale = 1f;
|
|
}
|
|
|
|
public void CompleteLevel()
|
|
{
|
|
if (!IsAlive || levelCompleted)
|
|
{
|
|
return;
|
|
}
|
|
|
|
levelCompleted = true;
|
|
DisableGameplayControl();
|
|
Debug.Log("Игрок достиг зоны выхода.");
|
|
LevelCompleted?.Invoke();
|
|
}
|
|
|
|
private void HandleDeath()
|
|
{
|
|
hasTriggeredDeath = true;
|
|
DisableGameplayControl();
|
|
|
|
Debug.Log("Игрок обнаружен и выведен из строя.");
|
|
Died?.Invoke();
|
|
}
|
|
|
|
private void DisableGameplayControl()
|
|
{
|
|
enabled = false;
|
|
|
|
SimpleFPSController fpsController = GetComponent<SimpleFPSController>();
|
|
if (fpsController != null)
|
|
{
|
|
fpsController.enabled = false;
|
|
}
|
|
|
|
PlayerStealth stealth = GetComponent<PlayerStealth>();
|
|
if (stealth != null)
|
|
{
|
|
stealth.enabled = false;
|
|
}
|
|
|
|
CharacterController characterController = GetComponent<CharacterController>();
|
|
if (characterController != null)
|
|
{
|
|
characterController.enabled = false;
|
|
}
|
|
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
Time.timeScale = 0f;
|
|
}
|
|
}
|