118 lines
3.6 KiB
C#
118 lines
3.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
namespace YachtDice.UI
|
|
{
|
|
public class DicePanelView : MonoBehaviour
|
|
{
|
|
[Serializable] public struct DiceValue
|
|
{
|
|
public Button diceButton;
|
|
public TextMeshProUGUI diceValueText;
|
|
public Image diceBackground;
|
|
}
|
|
|
|
[Header("Dice Display")]
|
|
[SerializeField] private DiceValue[] diceValues = new DiceValue[5];
|
|
|
|
[Header("Roll")]
|
|
[SerializeField] private Button rollButton;
|
|
[SerializeField] private TMP_Text rollButtonText;
|
|
|
|
[Header("Colors")]
|
|
[SerializeField] private Color unlockedColor = Color.white;
|
|
[SerializeField] private Color lockedColor = new Color(1f, 0.85f, 0.4f, 1f);
|
|
|
|
public event Action<int> OnDiceToggled;
|
|
public event Action OnRollClicked;
|
|
|
|
private void Awake()
|
|
{
|
|
for (int i = 0; i < diceValues.Length; i++)
|
|
{
|
|
int capturedIndex = i;
|
|
diceValues[i].diceButton.onClick.AddListener(() => OnDiceToggled?.Invoke(capturedIndex));
|
|
}
|
|
|
|
rollButton.onClick.AddListener(() => OnRollClicked?.Invoke());
|
|
|
|
for (int i = 0; i < diceValues.Length; i++)
|
|
{
|
|
diceValues[i].diceValueText.text = "?";
|
|
diceValues[i]. diceBackground.color = unlockedColor;
|
|
diceValues[i].diceButton.interactable = false;
|
|
}
|
|
|
|
SetRollButtonState(true, 0, 3);
|
|
}
|
|
|
|
public void SetDiceValue(int index, int value)
|
|
{
|
|
if (index >= 0 && index < diceValues.Length)
|
|
diceValues[index].diceValueText.text = value.ToString();
|
|
}
|
|
|
|
public void SetAllDiceValues(int[] values)
|
|
{
|
|
for (int i = 0; i < values.Length && i < diceValues.Length; i++)
|
|
diceValues[i].diceValueText.text = values[i].ToString();
|
|
}
|
|
|
|
public void SetDiceLocked(int index, bool isLocked)
|
|
{
|
|
if (index >= 0 && index < diceValues.Length)
|
|
diceValues[index].diceBackground.color = isLocked ? lockedColor : unlockedColor;
|
|
}
|
|
|
|
public void SetDiceInteractable(bool interactable)
|
|
{
|
|
for (int i = 0; i < diceValues.Length; i++)
|
|
diceValues[i].diceButton.interactable = interactable;
|
|
}
|
|
|
|
public void SetRollButtonState(bool interactable, int currentRoll, int maxRolls)
|
|
{
|
|
rollButton.interactable = interactable;
|
|
|
|
if (currentRoll >= maxRolls)
|
|
rollButtonText.text = "Выберите категорию";
|
|
else
|
|
rollButtonText.text = $"Бросок {currentRoll + 1}/{maxRolls}";
|
|
}
|
|
|
|
public void SetRollButtonInteractable(bool interactable)
|
|
{
|
|
rollButton.interactable = interactable;
|
|
}
|
|
|
|
public void ResetForNewTurn()
|
|
{
|
|
for (int i = 0; i < diceValues.Length; i++)
|
|
{
|
|
diceValues[i].diceValueText.text = "?";
|
|
diceValues[i].diceBackground.color = unlockedColor;
|
|
diceValues[i].diceButton.interactable = false;
|
|
}
|
|
}
|
|
|
|
public void ResetForNewGame(int maxRolls = 3)
|
|
{
|
|
ResetForNewTurn();
|
|
SetRollButtonState(true, 0, maxRolls);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
for (var i = 0; i < diceValues.Length; i++)
|
|
{
|
|
var t = diceValues[i].diceButton;
|
|
t.onClick.RemoveAllListeners();
|
|
}
|
|
|
|
rollButton.onClick.RemoveAllListeners();
|
|
}
|
|
}
|
|
}
|