192 lines
5.2 KiB
C#
192 lines
5.2 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class TruthFountainUI : MonoBehaviour
|
|
{
|
|
[Header("Root")]
|
|
[SerializeField] private GameObject uiRoot;
|
|
|
|
[Header("Texts")]
|
|
[SerializeField] private TMP_Text titleText;
|
|
[SerializeField] private TMP_Text questionText;
|
|
[SerializeField] private TMP_Text questionHintText;
|
|
[SerializeField] private TMP_Text progressText;
|
|
[SerializeField] private TMP_Text resultText;
|
|
|
|
[Header("Choices")]
|
|
[SerializeField] private TruthChoiceButtonUI[] choiceButtons;
|
|
|
|
[Header("Nose Gauge")]
|
|
[SerializeField] private Image noseGaugeFill;
|
|
[SerializeField] private TMP_Text noseValueText;
|
|
|
|
[Header("Final Result")]
|
|
[SerializeField] private GameObject finalResultPanel;
|
|
[SerializeField] private TMP_Text finalTitleText;
|
|
[SerializeField] private TMP_Text finalDescriptionText;
|
|
[SerializeField] private Button confirmButton;
|
|
|
|
private TruthFountainGameManager gameManager;
|
|
|
|
private void Reset()
|
|
{
|
|
uiRoot = gameObject;
|
|
}
|
|
|
|
public void Bind(TruthFountainGameManager manager)
|
|
{
|
|
gameManager = manager;
|
|
|
|
if (confirmButton != null)
|
|
{
|
|
confirmButton.onClick.RemoveAllListeners();
|
|
confirmButton.onClick.AddListener(HandleConfirmClicked);
|
|
}
|
|
}
|
|
|
|
public void SetVisible(bool visible)
|
|
{
|
|
GameObject targetRoot = uiRoot != null ? uiRoot : gameObject;
|
|
targetRoot.SetActive(visible);
|
|
}
|
|
|
|
public void Initialize(TruthFountainData data)
|
|
{
|
|
if (titleText != null)
|
|
{
|
|
titleText.text = data != null ? data.Title : "진실의 샘";
|
|
}
|
|
|
|
SetResultMessage(string.Empty, false);
|
|
ShowFinalResult(false, string.Empty, string.Empty);
|
|
UpdateNoseGauge(0, data != null ? data.MaxLieScore : 1);
|
|
}
|
|
|
|
public void ShowQuestion(TruthQuestionData question, int questionIndex, int totalQuestions, TruthFountainGameManager manager)
|
|
{
|
|
if (questionText != null)
|
|
{
|
|
questionText.text = question != null ? question.QuestionText : "질문 데이터가 없습니다.";
|
|
}
|
|
|
|
if (questionHintText != null)
|
|
{
|
|
questionHintText.text = question != null ? question.HintText : string.Empty;
|
|
}
|
|
|
|
if (progressText != null)
|
|
{
|
|
progressText.text = $"질문 {questionIndex + 1} / {Mathf.Max(1, totalQuestions)}";
|
|
}
|
|
|
|
SetResultMessage(string.Empty, false);
|
|
SetupChoiceButtons(question, manager);
|
|
}
|
|
|
|
public void SetupChoiceButtons(TruthQuestionData question, TruthFountainGameManager manager)
|
|
{
|
|
TruthChoiceData[] choices = question != null ? question.Choices : null;
|
|
|
|
for (int i = 0; i < choiceButtons.Length; i++)
|
|
{
|
|
TruthChoiceData choice = choices != null && i < choices.Length ? choices[i] : null;
|
|
|
|
if (choiceButtons[i] != null)
|
|
{
|
|
choiceButtons[i].Setup(choice, manager);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetChoiceButtonsInteractable(bool interactable)
|
|
{
|
|
foreach (TruthChoiceButtonUI choiceButton in choiceButtons)
|
|
{
|
|
if (choiceButton != null && choiceButton.gameObject.activeSelf)
|
|
{
|
|
choiceButton.SetInteractable(interactable);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ShowChoiceFeedback(TruthChoiceButtonUI selectedButton)
|
|
{
|
|
foreach (TruthChoiceButtonUI choiceButton in choiceButtons)
|
|
{
|
|
if (choiceButton == null || !choiceButton.gameObject.activeSelf)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (choiceButton == selectedButton)
|
|
{
|
|
choiceButton.ShowTruthOrLieFeedback();
|
|
}
|
|
else
|
|
{
|
|
choiceButton.ResetVisual();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetResultMessage(string message, bool visible = true)
|
|
{
|
|
if (resultText == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
resultText.text = message;
|
|
resultText.gameObject.SetActive(visible && !string.IsNullOrWhiteSpace(message));
|
|
}
|
|
|
|
public void UpdateNoseGauge(int lieScore, int maxLieScore)
|
|
{
|
|
int safeMax = Mathf.Max(1, maxLieScore);
|
|
float fillAmount = Mathf.Clamp01((float)lieScore / safeMax);
|
|
|
|
if (noseGaugeFill != null)
|
|
{
|
|
noseGaugeFill.fillAmount = fillAmount;
|
|
}
|
|
|
|
if (noseValueText != null)
|
|
{
|
|
noseValueText.text = $"코 길이 {Mathf.RoundToInt(fillAmount * 100f)}%";
|
|
}
|
|
}
|
|
|
|
public void ShowFinalResult(bool visible, string title, string description)
|
|
{
|
|
if (finalResultPanel != null)
|
|
{
|
|
finalResultPanel.SetActive(visible);
|
|
}
|
|
|
|
if (finalTitleText != null)
|
|
{
|
|
finalTitleText.text = title;
|
|
}
|
|
|
|
if (finalDescriptionText != null)
|
|
{
|
|
finalDescriptionText.text = description;
|
|
}
|
|
|
|
SetChoiceButtonsInteractable(!visible);
|
|
}
|
|
|
|
private void HandleConfirmClicked()
|
|
{
|
|
if (gameManager != null)
|
|
{
|
|
gameManager.ConfirmFinalResult();
|
|
}
|
|
else
|
|
{
|
|
ShowFinalResult(false, string.Empty, string.Empty);
|
|
}
|
|
}
|
|
}
|