168 lines
5.6 KiB
C#
168 lines
5.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
namespace YachtDice.UI
|
|
{
|
|
public class GameInfoView : MonoBehaviour
|
|
{
|
|
[Header("Run HUD")]
|
|
[SerializeField] private TMP_Text turnText;
|
|
[SerializeField] private TMP_Text phaseText;
|
|
[SerializeField] private TMP_Text betText;
|
|
[SerializeField] private TMP_Text stageText;
|
|
[SerializeField] private TMP_Text targetText;
|
|
[SerializeField] private TMP_Text quotaText;
|
|
[SerializeField] private TMP_Text storedRollsText;
|
|
|
|
[Header("Currency")]
|
|
[SerializeField] private TMP_Text currencyText;
|
|
|
|
[Header("Navigation")]
|
|
[SerializeField] private Button shopButton;
|
|
[SerializeField] private Button inventoryButton;
|
|
|
|
[Header("Game Over Overlay")]
|
|
[SerializeField] private GameObject gameOverPanel;
|
|
[SerializeField] private TMP_Text finalScoreText;
|
|
[SerializeField] private Button newGameButton;
|
|
|
|
public event Action OnNewGameClicked;
|
|
public event Action OnShopClicked;
|
|
public event Action OnInventoryClicked;
|
|
|
|
private void Awake()
|
|
{
|
|
EnsureHudBindings();
|
|
EnsureGameOverBindings();
|
|
|
|
newGameButton.onClick.AddListener(() => OnNewGameClicked?.Invoke());
|
|
gameOverPanel.SetActive(false);
|
|
|
|
if (shopButton != null)
|
|
shopButton.onClick.AddListener(() => OnShopClicked?.Invoke());
|
|
if (inventoryButton != null)
|
|
inventoryButton.onClick.AddListener(() => OnInventoryClicked?.Invoke());
|
|
}
|
|
|
|
public void SetTurnText(int turn, int maxTurns)
|
|
{
|
|
turnText.text = $"Ход {turn} / {maxTurns}";
|
|
}
|
|
|
|
public void SetRunInfoText(string text)
|
|
{
|
|
if (phaseText != null)
|
|
phaseText.text = text;
|
|
}
|
|
|
|
public void SetRunHud(string phase, int bet, int stage, int stageCount, int target, int quota, int storedRolls)
|
|
{
|
|
if (turnText != null)
|
|
turnText.text = $"Turn {stage}/{stageCount}";
|
|
if (phaseText != null)
|
|
phaseText.text = $"Phase: {phase}";
|
|
if (betText != null)
|
|
betText.text = $"Bet: {bet}";
|
|
if (stageText != null)
|
|
stageText.text = $"Stage: {stage}/{stageCount}";
|
|
if (targetText != null)
|
|
targetText.text = $"Target: {target}";
|
|
if (quotaText != null)
|
|
quotaText.text = $"Quota: {quota}";
|
|
if (storedRollsText != null)
|
|
storedRollsText.text = $"Stored Rolls: {storedRolls}";
|
|
}
|
|
|
|
public void SetCurrencyText(int amount)
|
|
{
|
|
if (currencyText != null)
|
|
currencyText.text = amount.ToString();
|
|
}
|
|
|
|
public void SetShopButtonInteractable(bool interactable)
|
|
{
|
|
if (shopButton != null)
|
|
shopButton.interactable = interactable;
|
|
}
|
|
|
|
public void ShowGameOver(int finalScore)
|
|
{
|
|
gameOverPanel.SetActive(true);
|
|
finalScoreText.text = $"Итого: {finalScore}";
|
|
}
|
|
|
|
public void HideGameOver()
|
|
{
|
|
gameOverPanel.SetActive(false);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
newGameButton.onClick.RemoveAllListeners();
|
|
|
|
if (shopButton != null)
|
|
shopButton.onClick.RemoveAllListeners();
|
|
if (inventoryButton != null)
|
|
inventoryButton.onClick.RemoveAllListeners();
|
|
}
|
|
|
|
private void EnsureHudBindings()
|
|
{
|
|
if (turnText == null)
|
|
return;
|
|
|
|
phaseText ??= turnText;
|
|
betText ??= CreateHudLabel("Bet Text", -34);
|
|
stageText ??= CreateHudLabel("Stage Text", -68);
|
|
targetText ??= CreateHudLabel("Target Text", -102);
|
|
quotaText ??= CreateHudLabel("Quota Text", -136);
|
|
storedRollsText ??= CreateHudLabel("Stored Rolls Text", -170);
|
|
}
|
|
|
|
private void EnsureGameOverBindings()
|
|
{
|
|
if (gameOverPanel == null)
|
|
return;
|
|
|
|
if (finalScoreText == null || finalScoreText == currencyText)
|
|
{
|
|
TMP_Text source = turnText != null ? turnText : currencyText;
|
|
if (source == null)
|
|
return;
|
|
|
|
finalScoreText = Instantiate(source, gameOverPanel.transform);
|
|
finalScoreText.name = "Final Score Text";
|
|
|
|
if (finalScoreText.transform is RectTransform rect)
|
|
{
|
|
rect.anchorMin = new Vector2(0.5f, 0.5f);
|
|
rect.anchorMax = new Vector2(0.5f, 0.5f);
|
|
rect.pivot = new Vector2(0.5f, 0.5f);
|
|
rect.anchoredPosition = new Vector2(0f, 40f);
|
|
rect.sizeDelta = new Vector2(360f, 60f);
|
|
}
|
|
}
|
|
}
|
|
|
|
private TMP_Text CreateHudLabel(string objectName, float yOffset)
|
|
{
|
|
var clone = Instantiate(turnText, turnText.transform.parent);
|
|
clone.name = objectName;
|
|
clone.text = string.Empty;
|
|
|
|
if (clone.transform is RectTransform rect && turnText.transform is RectTransform sourceRect)
|
|
{
|
|
rect.anchorMin = sourceRect.anchorMin;
|
|
rect.anchorMax = sourceRect.anchorMax;
|
|
rect.pivot = sourceRect.pivot;
|
|
rect.sizeDelta = sourceRect.sizeDelta;
|
|
rect.anchoredPosition = sourceRect.anchoredPosition + new Vector2(0f, yOffset);
|
|
}
|
|
|
|
return clone;
|
|
}
|
|
}
|
|
}
|