using System.Collections; using UnityEngine; using UnityEngine.Events; public class TruthFountainGameManager : MonoBehaviour { [Header("References")] [SerializeField] private TruthFountainData fountainData; [SerializeField] private TruthFountainUI ui; [SerializeField] private TruthFountainReward reward; [Header("Start Option")] [SerializeField] private bool startOnAwake = false; [Header("Events")] public UnityEvent onGameStarted; public UnityEvent onQuestionAnswered; public UnityEvent onGameSuccess; public UnityEvent onGameFailed; public UnityEvent onFinalConfirmed; [Header("Debug")] [SerializeField] private bool showDebugLog = true; private int currentQuestionIndex; private int lieScore; private bool isRunning; private bool isWaitingForNextQuestion; private bool finalSuccess; private Coroutine choiceRoutine; public int CurrentQuestionIndex => currentQuestionIndex; public int LieScore => lieScore; public bool IsRunning => isRunning; private void Awake() { if (ui == null) { ui = FindFirstObjectByType(); } if (reward == null) { reward = GetComponent(); } if (ui != null) { ui.Bind(this); } } private void Start() { if (startOnAwake) { StartGame(); } } public void StartGame() { if (fountainData == null) { Debug.LogWarning($"[{nameof(TruthFountainGameManager)}] TruthFountainData가 연결되지 않았습니다."); return; } if (fountainData.QuestionCount <= 0) { Debug.LogWarning($"[{nameof(TruthFountainGameManager)}] 질문 데이터가 비어 있습니다."); return; } if (choiceRoutine != null) { StopCoroutine(choiceRoutine); choiceRoutine = null; } currentQuestionIndex = 0; lieScore = 0; isRunning = true; isWaitingForNextQuestion = false; finalSuccess = false; if (ui != null) { ui.Initialize(fountainData); ui.ShowQuestion(fountainData.GetQuestion(currentQuestionIndex), currentQuestionIndex, fountainData.QuestionCount, this); ui.UpdateNoseGauge(lieScore, fountainData.MaxLieScore); } onGameStarted?.Invoke(); if (showDebugLog) { Debug.Log($"[{nameof(TruthFountainGameManager)}] 진실의 샘 시작"); } } public void SelectChoice(TruthChoiceData choice, TruthChoiceButtonUI selectedButton = null) { if (!isRunning || isWaitingForNextQuestion || choice == null) { return; } if (choiceRoutine != null) { StopCoroutine(choiceRoutine); } choiceRoutine = StartCoroutine(HandleChoiceRoutine(choice, selectedButton)); } private IEnumerator HandleChoiceRoutine(TruthChoiceData choice, TruthChoiceButtonUI selectedButton) { isWaitingForNextQuestion = true; lieScore = Mathf.Max(0, lieScore + choice.LieAmount); if (ui != null) { ui.SetChoiceButtonsInteractable(false); ui.ShowChoiceFeedback(selectedButton); ui.SetResultMessage(choice.ResultMessage, true); ui.UpdateNoseGauge(lieScore, fountainData.MaxLieScore); } onQuestionAnswered?.Invoke(); if (showDebugLog) { Debug.Log($"[{nameof(TruthFountainGameManager)}] 선택: {choice.ChoiceText}, lie +{choice.LieAmount}, 현재 lieScore={lieScore}"); } yield return new WaitForSeconds(fountainData.ResultDelay); bool reachedMaxLieScore = lieScore >= fountainData.MaxLieScore; if (reachedMaxLieScore && fountainData.FailImmediatelyAtMaxLieScore) { FinishGame(false); yield break; } currentQuestionIndex++; if (currentQuestionIndex >= fountainData.QuestionCount) { bool success = lieScore < fountainData.MaxLieScore; FinishGame(success); yield break; } if (ui != null) { ui.ShowQuestion(fountainData.GetQuestion(currentQuestionIndex), currentQuestionIndex, fountainData.QuestionCount, this); } isWaitingForNextQuestion = false; choiceRoutine = null; } private void FinishGame(bool success) { isRunning = false; isWaitingForNextQuestion = false; finalSuccess = success; choiceRoutine = null; if (ui != null) { string title = success ? fountainData.SuccessTitle : fountainData.FailTitle; string description = success ? fountainData.SuccessDescription : fountainData.FailDescription; ui.SetResultMessage(string.Empty, false); ui.ShowFinalResult(true, title, description); } if (success) { reward?.GiveReward(); onGameSuccess?.Invoke(); } else { onGameFailed?.Invoke(); } if (showDebugLog) { Debug.Log($"[{nameof(TruthFountainGameManager)}] 게임 종료: {(success ? "성공" : "실패")}, lieScore={lieScore}"); } } public void ConfirmFinalResult() { if (ui != null) { ui.ShowFinalResult(false, string.Empty, string.Empty); } onFinalConfirmed?.Invoke(); if (showDebugLog) { Debug.Log($"[{nameof(TruthFountainGameManager)}] 최종 결과 확인: {(finalSuccess ? "성공" : "실패")}"); } } public void ResetGameOnly() { if (choiceRoutine != null) { StopCoroutine(choiceRoutine); choiceRoutine = null; } currentQuestionIndex = 0; lieScore = 0; isRunning = false; isWaitingForNextQuestion = false; if (ui != null) { ui.Initialize(fountainData); } } }