Files
Perekyp-2.0/Assets/Scripts/UI/RestorationHUD.cs
T

354 lines
13 KiB
C#
Raw Normal View History

2026-11-19 23:24:44 +03:00
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
[DisallowMultipleComponent]
public class RestorationHUD : MonoBehaviour
{
public static RestorationHUD Instance { get; private set; }
[Header("Настройки анимации и расположения")]
[SerializeField] private Vector2 hudAnchoredPosition = new Vector2(0f, -140f);
[SerializeField] private float fadeSpeed = 12f;
private CanvasGroup canvasGroup;
private RectTransform rootCard;
private TextMeshProUGUI titleText;
private TextMeshProUGUI stageBadgeText;
private Image stageBadgeBg;
private Image progressFill;
private TextMeshProUGUI progressPercentText;
private TextMeshProUGUI hintText;
// Секция залива жидкостей
private GameObject fluidSection;
private Image fluidGaugeFill;
private TextMeshProUGUI fluidLitersText;
private TextMeshProUGUI fluidHintText;
private float targetAlpha = 0f;
private float hideTimer = 0f;
private float currentFillAmount = 0f;
private void Awake()
{
if (Instance == null) Instance = this;
else if (Instance != this) { Destroy(gameObject); return; }
EnsureUIElementsCreated();
}
private void EnsureUIElementsCreated()
{
canvasGroup = GetComponent<CanvasGroup>();
if (canvasGroup == null) canvasGroup = gameObject.AddComponent<CanvasGroup>();
canvasGroup.alpha = 0f;
canvasGroup.blocksRaycasts = false;
canvasGroup.interactable = false;
RectTransform myRect = GetComponent<RectTransform>();
if (myRect == null) myRect = gameObject.AddComponent<RectTransform>();
// Если элементы еще не созданы в иерархии — собираем стильную стеклянную карточку
if (titleText == null)
{
BuildModernHUD(myRect);
}
}
private void BuildModernHUD(RectTransform parent)
{
parent.anchorMin = new Vector2(0.5f, 0.5f);
parent.anchorMax = new Vector2(0.5f, 0.5f);
parent.pivot = new Vector2(0.5f, 0.5f);
parent.anchoredPosition = hudAnchoredPosition;
parent.sizeDelta = new Vector2(420f, 130f);
// 1. Фоновая плашка с эффектом темного стекла и мягкой обводкой
Image cardBg = gameObject.GetComponent<Image>();
if (cardBg == null) cardBg = gameObject.AddComponent<Image>();
cardBg.sprite = ProceduralUISprites.GetSlicedRoundedRect(
new Color(0.08f, 0.10f, 0.14f, 0.88f),
new Color(0.28f, 0.35f, 0.45f, 0.45f),
1.5f, 16);
cardBg.type = Image.Type.Sliced;
cardBg.raycastTarget = false;
// 2. Верхняя строка: Заголовок детали + Бейдж этапа
GameObject topRow = new GameObject("TopRow");
topRow.transform.SetParent(transform, false);
RectTransform trRt = topRow.AddComponent<RectTransform>();
trRt.anchorMin = new Vector2(0f, 1f);
trRt.anchorMax = new Vector2(1f, 1f);
trRt.pivot = new Vector2(0.5f, 1f);
trRt.anchoredPosition = new Vector2(0f, -12f);
trRt.sizeDelta = new Vector2(-24f, 26f);
// Название детали
GameObject titleObj = new GameObject("TitleText");
titleObj.transform.SetParent(topRow.transform, false);
RectTransform titleRt = titleObj.AddComponent<RectTransform>();
titleRt.anchorMin = new Vector2(0f, 0f);
titleRt.anchorMax = new Vector2(0.65f, 1f);
titleRt.pivot = new Vector2(0f, 0.5f);
titleRt.anchoredPosition = Vector2.zero;
titleRt.sizeDelta = Vector2.zero;
titleText = titleObj.AddComponent<TextMeshProUGUI>();
titleText.fontSize = 15f;
titleText.fontStyle = FontStyles.Bold;
titleText.color = new Color(0.95f, 0.96f, 0.98f);
titleText.alignment = TextAlignmentOptions.MidlineLeft;
titleText.text = "Передняя левая дверь";
// Бейдж этапа
GameObject badgeObj = new GameObject("StageBadge");
badgeObj.transform.SetParent(topRow.transform, false);
RectTransform badgeRt = badgeObj.AddComponent<RectTransform>();
badgeRt.anchorMin = new Vector2(0.65f, 0f);
badgeRt.anchorMax = new Vector2(1f, 1f);
badgeRt.pivot = new Vector2(1f, 0.5f);
badgeRt.anchoredPosition = Vector2.zero;
badgeRt.sizeDelta = Vector2.zero;
stageBadgeBg = badgeObj.AddComponent<Image>();
stageBadgeBg.sprite = ProceduralUISprites.GetSlicedRoundedRect(
new Color(0.85f, 0.35f, 0.12f, 0.9f), null, 0f, 8);
stageBadgeBg.type = Image.Type.Sliced;
GameObject badgeTextObj = new GameObject("BadgeText");
badgeTextObj.transform.SetParent(badgeObj.transform, false);
RectTransform btRt = badgeTextObj.AddComponent<RectTransform>();
btRt.anchorMin = Vector2.zero;
btRt.anchorMax = Vector2.one;
btRt.sizeDelta = Vector2.zero;
stageBadgeText = badgeTextObj.AddComponent<TextMeshProUGUI>();
stageBadgeText.fontSize = 12f;
stageBadgeText.fontStyle = FontStyles.Bold;
stageBadgeText.color = Color.white;
stageBadgeText.alignment = TextAlignmentOptions.Center;
stageBadgeText.text = "Ржавчина";
// 3. Полоса прогресса
GameObject progressTrack = new GameObject("ProgressTrack");
progressTrack.transform.SetParent(transform, false);
RectTransform ptRt = progressTrack.AddComponent<RectTransform>();
ptRt.anchorMin = new Vector2(0f, 0.5f);
ptRt.anchorMax = new Vector2(1f, 0.5f);
ptRt.pivot = new Vector2(0.5f, 0.5f);
ptRt.anchoredPosition = new Vector2(0f, 2f);
ptRt.sizeDelta = new Vector2(-24f, 16f);
Image trackBg = progressTrack.AddComponent<Image>();
trackBg.sprite = ProceduralUISprites.GetSlicedRoundedRect(
new Color(0.12f, 0.15f, 0.20f, 0.95f),
new Color(0.22f, 0.26f, 0.35f, 0.5f), 1f, 8);
trackBg.type = Image.Type.Sliced;
GameObject fillObj = new GameObject("ProgressFill");
fillObj.transform.SetParent(progressTrack.transform, false);
RectTransform fillRt = fillObj.AddComponent<RectTransform>();
fillRt.anchorMin = Vector2.zero;
fillRt.anchorMax = Vector2.one;
fillRt.sizeDelta = new Vector2(-4f, -4f);
progressFill = fillObj.AddComponent<Image>();
progressFill.sprite = ProceduralUISprites.GetSlicedRoundedRect(
new Color(0.20f, 0.65f, 0.95f, 0.95f), null, 0f, 6);
progressFill.type = Image.Type.Filled;
progressFill.fillMethod = Image.FillMethod.Horizontal;
progressFill.fillOrigin = 0;
progressFill.fillAmount = 0.5f;
GameObject pctObj = new GameObject("PercentText");
pctObj.transform.SetParent(progressTrack.transform, false);
RectTransform pctRt = pctObj.AddComponent<RectTransform>();
pctRt.anchorMin = Vector2.zero;
pctRt.anchorMax = Vector2.one;
pctRt.sizeDelta = Vector2.zero;
progressPercentText = pctObj.AddComponent<TextMeshProUGUI>();
progressPercentText.fontSize = 11f;
progressPercentText.fontStyle = FontStyles.Bold;
progressPercentText.color = Color.white;
progressPercentText.alignment = TextAlignmentOptions.Center;
progressPercentText.text = "50%";
// 4. Нижняя подсказка / контекстное действие
GameObject hintObj = new GameObject("HintText");
hintObj.transform.SetParent(transform, false);
RectTransform hintRt = hintObj.AddComponent<RectTransform>();
hintRt.anchorMin = new Vector2(0f, 0f);
hintRt.anchorMax = new Vector2(1f, 0f);
hintRt.pivot = new Vector2(0.5f, 0f);
hintRt.anchoredPosition = new Vector2(0f, 10f);
hintRt.sizeDelta = new Vector2(-24f, 32f);
hintText = hintObj.AddComponent<TextMeshProUGUI>();
hintText.fontSize = 12.5f;
hintText.color = new Color(0.82f, 0.86f, 0.92f);
hintText.alignment = TextAlignmentOptions.Center;
hintText.text = "[Удерж. ЛКМ] Водите инструментом по детали";
// 5. Секция жидкостей (по умолчанию скрыта)
fluidSection = new GameObject("FluidSection");
fluidSection.transform.SetParent(transform, false);
RectTransform fsRt = fluidSection.AddComponent<RectTransform>();
fsRt.anchorMin = Vector2.zero;
fsRt.anchorMax = Vector2.one;
fsRt.sizeDelta = Vector2.zero;
fluidSection.SetActive(false);
}
private void Update()
{
if (hideTimer > 0f)
{
hideTimer -= Time.deltaTime;
if (hideTimer <= 0f) targetAlpha = 0f;
}
if (canvasGroup != null)
{
canvasGroup.alpha = Mathf.Lerp(canvasGroup.alpha, targetAlpha, Time.deltaTime * fadeSpeed);
}
if (progressFill != null)
{
progressFill.fillAmount = Mathf.Lerp(progressFill.fillAmount, currentFillAmount, Time.deltaTime * 15f);
}
}
public void ShowRestorationProgress(
string partName,
RepairStage stage,
float progress01,
string contextualHint,
bool isCorrectTool)
{
EnsureUIElementsCreated();
targetAlpha = 1f;
hideTimer = 0.25f;
if (titleText != null) titleText.text = partName;
currentFillAmount = Mathf.Clamp01(progress01);
int percent = Mathf.RoundToInt(currentFillAmount * 100f);
if (progressPercentText != null)
{
if (stage == RepairStage.Healthy || stage == RepairStage.Repaired)
progressPercentText.text = "100%";
else
progressPercentText.text = $"{percent}%";
}
// Обновляем бейдж этапа и цвета
SetupStageBadge(stage);
if (hintText != null)
{
hintText.text = contextualHint;
hintText.color = isCorrectTool ? new Color(0.85f, 0.92f, 1.0f) : new Color(1.0f, 0.65f, 0.35f);
}
}
private void SetupStageBadge(RepairStage stage)
{
if (stageBadgeText == null || stageBadgeBg == null) return;
Color badgeColor;
string badgeName;
switch (stage)
{
case RepairStage.Healthy:
badgeName = "Заводская";
badgeColor = new Color(0.18f, 0.72f, 0.38f);
break;
case RepairStage.Rusted:
badgeName = "Ржавчина";
badgeColor = new Color(0.92f, 0.38f, 0.12f);
break;
case RepairStage.Cleaned:
badgeName = "Зачищено";
badgeColor = new Color(0.55f, 0.62f, 0.70f);
break;
case RepairStage.Puttied:
badgeName = "Шпаклёвка";
badgeColor = new Color(0.82f, 0.70f, 0.35f);
break;
case RepairStage.Sanded:
badgeName = "Шлифовка";
badgeColor = new Color(0.20f, 0.68f, 0.42f);
break;
default:
badgeName = "Восстановлено";
badgeColor = new Color(0.15f, 0.75f, 0.95f);
break;
}
stageBadgeText.text = badgeName;
stageBadgeBg.color = badgeColor;
if (progressFill != null) progressFill.color = badgeColor;
}
public void ShowFluidPouringHUD(
string tankTitle,
FluidType type,
float currentLiters,
float maxLiters,
float canisterRemainingLiters,
Color fluidColor)
{
EnsureUIElementsCreated();
targetAlpha = 1f;
hideTimer = 0.25f;
float fill01 = Mathf.Clamp01(currentLiters / Mathf.Max(maxLiters, 0.01f));
currentFillAmount = fill01;
int percent = Mathf.RoundToInt(fill01 * 100f);
if (titleText != null) titleText.text = tankTitle;
if (stageBadgeText != null)
{
stageBadgeText.text = "Залив жидкости";
stageBadgeBg.color = fluidColor;
}
if (progressFill != null)
{
progressFill.color = fluidColor;
}
if (progressPercentText != null)
{
progressPercentText.text = $"{currentLiters:F1} / {maxLiters:F1} л ({percent}%)";
}
if (hintText != null)
{
hintText.text = $"[Удерж. ЛКМ] Залить жидкость | В канистре: {canisterRemainingLiters:F1} л";
hintText.color = Color.white;
}
}
public void ShowCustomMessage(string message)
{
EnsureUIElementsCreated();
targetAlpha = 1f;
hideTimer = 0.25f;
if (hintText != null)
{
hintText.text = message;
hintText.color = new Color(1.0f, 0.85f, 0.45f);
}
}
public void Hide()
{
targetAlpha = 0f;
}
}