Files
Souljack/Assets/_Project/Scripts/PlayerUtils/PlayerUI.cs
T

163 lines
6.3 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
namespace _Project.Scripts.PlayerUtils
{
public class PlayerUI : MonoBehaviour
{
[Header("Настройки Интерфейса")]
[SerializeField] private bool showHUD = true;
[Header("Панель проигрыша (Canvas UI)")]
[Tooltip("Перетащи сюда панель поражения из Canvas. Если пусто — отрисуется встроенная панель проигрыша.")]
[SerializeField] private GameObject losePanel;
private PlayerMovement movement;
private PlayerPossession possession;
private bool isGameOver = false;
private void Start()
{
movement = Player.Instance.Movement;
possession = Player.Instance.Possession;
if (losePanel != null)
{
losePanel.SetActive(false);
}
if (possession != null)
{
possession.OnPlayerDied += HandlePlayerDied;
}
}
private void OnDestroy()
{
if (possession != null)
{
possession.OnPlayerDied -= HandlePlayerDied;
}
}
private void HandlePlayerDied()
{
isGameOver = true;
if (losePanel != null)
{
losePanel.SetActive(true);
}
}
private void Update()
{
// Быстрый перезапуск по клавише R после смерти
if (isGameOver && Keyboard.current != null && Keyboard.current.rKey.wasPressedThisFrame)
{
RestartLevel();
}
}
// Этот метод также можно повесить на кнопку (Button -> OnClick) на твоём Canvas!
public void RestartLevel()
{
Time.timeScale = 1f;
Time.fixedDeltaTime = 0.02f;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
private void OnGUI()
{
// 1. Если игрок погиб, а кастомная Canvas-панель не назначена — рисуем экран проигрыша
if (isGameOver)
{
if (losePanel == null)
{
DrawFallbackLoseScreen();
}
return;
}
// 2. Обычный боевой HUD
if (!showHUD || movement == null) return;
float speed = movement.CurrentSpeed;
float csUnits = speed * 38.5f;
GUIStyle style = new GUIStyle { fontSize = 22, fontStyle = FontStyle.Bold };
string state = "";
if (movement.IsSliding) { style.normal.textColor = Color.yellow; state = " [SLIDE]"; }
else if (!movement.IsGrounded) { style.normal.textColor = Color.green; state = " [AIR]"; }
else style.normal.textColor = Color.white;
GUI.Label(new Rect(30, 30, 600, 40), $"Speed: {speed:F1} m/s ({csUnits:F0} u/s){state}", style);
if (possession != null && possession.CurrentProfile != null)
{
style.normal.textColor = possession.IsInSpiritForm ? Color.cyan : Color.green;
string bodyInfo = $"Форма: {possession.CurrentProfile.displayName} | HP: {possession.CurrentHealth:F0}/{possession.MaxHealth:F0}";
if (!possession.IsInSpiritForm) bodyInfo += " ([Q] Покинуть тело)";
GUI.Label(new Rect(30, 65, 800, 40), bodyInfo, style);
if (possession.AimedEnemy != null)
{
if (possession.IsTargetInPossessZone)
{
style.normal.textColor = Color.cyan;
GUI.Label(new Rect(Screen.width / 2f - 160f, Screen.height / 2f + 40f, 450f, 40f),
$"[E] ВСЕЛИТЬСЯ: {possession.AimedEnemy.enemyClass}", style);
}
else
{
style.normal.textColor = new Color(1f, 0.5f, 0.5f);
GUI.Label(new Rect(Screen.width / 2f - 180f, Screen.height / 2f + 40f, 500f, 40f),
$"ДАЛЕКО — ВОЙДИ В СФЕРУ ({possession.AimedEnemy.enemyClass})", style);
}
}
}
if (_Project.Scripts.LevelGenerating.TowerOrchestrator.Instance != null)
{
var orch = _Project.Scripts.LevelGenerating.TowerOrchestrator.Instance;
GUI.Label(new Rect(30, 100, 500, 40), $"Этаж башни: {orch.CurrentFloorIndex + 1} / {orch.TotalFloors}", style);
}
}
private void DrawFallbackLoseScreen()
{
// Полупрозрачное тёмно-красное/чёрное затемнение на весь экран
Color prevColor = GUI.color;
GUI.color = new Color(0.08f, 0.01f, 0.02f, 0.85f);
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Texture2D.whiteTexture);
GUI.color = prevColor;
GUIStyle titleStyle = new GUIStyle
{
fontSize = 48,
fontStyle = FontStyle.Bold,
alignment = TextAnchor.MiddleCenter
};
titleStyle.normal.textColor = new Color(1f, 0.2f, 0.25f);
GUIStyle subStyle = new GUIStyle
{
fontSize = 22,
alignment = TextAnchor.MiddleCenter
};
subStyle.normal.textColor = new Color(0.85f, 0.85f, 0.85f);
float cx = Screen.width / 2f;
float cy = Screen.height / 2f;
GUI.Label(new Rect(cx - 300f, cy - 110f, 600f, 60f), "ДУХ МЩЕНИЯ РАЗВЕЯН", titleStyle);
GUI.Label(new Rect(cx - 300f, cy - 45f, 600f, 40f), "Вы потеряли последнюю связь с этим миром.", subStyle);
if (GUI.Button(new Rect(cx - 150f, cy + 25f, 300f, 50f), "ПЕРЕЗАПУСТИТЬ ЭТАЖ [R]"))
{
RestartLevel();
}
}
}
}