using UnityEngine; using UnityEngine.Events; using UnityEngine.InputSystem; public class ShellGameManager : MonoBehaviour { [Header("References")] [SerializeField] private PlayerMovement player; [SerializeField] private UIManager ui; [SerializeField] private ShellController shellController; [Header("Shell Target")] [Tooltip("Á¶°³ Àüü Transform ¶Ç´Â º¸»ó À§Ä¡ TransformÀ» ³ÖÀ¸¼¼¿ä.")] [SerializeField] private Transform shell; [Header("Reward Settings")] [SerializeField] private float collectDistance = 1.5f; [SerializeField] private bool failWhenClosed = true; [Header("VR Distance Option")] [Tooltip("VR¿¡¼­´Â Ç÷¹ÀÌ¾î ¸Ó¸® ³ôÀÌ ¶§¹®¿¡ YÃà °Å¸®¸¦ ¹«½ÃÇÏ´Â °ÍÀÌ ¾ÈÁ¤ÀûÀÔ´Ï´Ù.")] [SerializeField] private bool ignoreHeight = true; [Header("Input Actions")] [Tooltip("¿À¸¥¼Õ Trigger ¶Ç´Â XRI RightHand Interaction / Activate ¾×¼ÇÀ» ¿¬°áÇϼ¼¿ä.")] [SerializeField] private InputActionReference collectAction; [Tooltip("¼±Åà »çÇ×ÀÔ´Ï´Ù. ¸®¼Â ¹öư ¾×¼ÇÀ» ¿¬°áÇÏ°í ½ÍÀ» ¶§ »ç¿ëÇÕ´Ï´Ù.")] [SerializeField] private InputActionReference resetAction; [Header("Events")] [SerializeField] private UnityEvent onSuccess; [SerializeField] private UnityEvent onFail; [Header("Debug")] [SerializeField] private bool showDebugLog = true; private bool gameFinished; private void OnEnable() { if (showDebugLog) Debug.Log("[ShellGameManager] OnEnable ½ÇÇàµÊ"); RegisterInputActions(); } private void OnDisable() { if (showDebugLog) Debug.Log("[ShellGameManager] OnDisable ½ÇÇàµÊ"); UnregisterInputActions(); } private void Start() { if (showDebugLog) Debug.Log("[ShellGameManager] Start ½ÇÇàµÊ / ResetGame È£Ãâ"); ResetGame(); } private void Update() { UpdateUI(); } private void RegisterInputActions() { if (collectAction == null) { Debug.LogWarning("[ShellGameManager] Collect ActionÀÌ ¿¬°áµÇÁö ¾Ê¾Ò½À´Ï´Ù."); } else if (collectAction.action == null) { Debug.LogWarning("[ShellGameManager] Collect Action ¾ÈÀÇ actionÀÌ nullÀÔ´Ï´Ù."); } else { collectAction.action.performed += OnCollectPerformed; collectAction.action.Enable(); if (showDebugLog) { Debug.Log( $"[ShellGameManager] Collect Action µî·Ï ¿Ï·á: {collectAction.action.name} / Enabled: {collectAction.action.enabled}" ); } } if (resetAction != null && resetAction.action != null) { resetAction.action.performed += OnResetPerformed; resetAction.action.Enable(); if (showDebugLog) { Debug.Log( $"[ShellGameManager] Reset Action µî·Ï ¿Ï·á: {resetAction.action.name} / Enabled: {resetAction.action.enabled}" ); } } } private void UnregisterInputActions() { if (collectAction != null && collectAction.action != null) { collectAction.action.performed -= OnCollectPerformed; if (showDebugLog) Debug.Log("[ShellGameManager] Collect Action µî·Ï ÇØÁ¦"); } if (resetAction != null && resetAction.action != null) { resetAction.action.performed -= OnResetPerformed; if (showDebugLog) Debug.Log("[ShellGameManager] Reset Action µî·Ï ÇØÁ¦"); } } private void UpdateUI() { if (ui == null) return; if (player != null) { ui.SetControl(player.IsReversed); ui.SetMovementDebug(player.CurrentInput, player.CurrentOutput); } if (shellController != null) { ui.SetShellState(shellController.IsOpen); ui.SetShellTimer(shellController.StateTimer01, shellController.IsOpen); } if (!gameFinished) UpdatePromptUI(); } private void UpdatePromptUI() { if (ui == null || player == null || shell == null || shellController == null) return; float distance = GetDistance(player.transform.position, shell.position); if (distance > collectDistance) { ui.SetPromptFarFromShell(); } else { ui.SetPromptNearShell(shellController.IsOpen); } } public void TryCollectReward() { Debug.Log("[ShellGameManager] TryCollectReward ½ÇÇàµÊ"); if (gameFinished) { Debug.Log("[ShellGameManager] ÀÌ¹Ì °ÔÀÓÀÌ ³¡³­ »óŶó ÀÔ·ÂÀ» ¹«½ÃÇÕ´Ï´Ù."); return; } if (player == null) { Debug.LogWarning("[ShellGameManager] Player ÂüÁ¶°¡ ºñ¾î ÀÖ½À´Ï´Ù."); return; } if (shell == null) { Debug.LogWarning("[ShellGameManager] Shell Transform ÂüÁ¶°¡ ºñ¾î ÀÖ½À´Ï´Ù."); return; } if (shellController == null) { Debug.LogWarning("[ShellGameManager] ShellController ÂüÁ¶°¡ ºñ¾î ÀÖ½À´Ï´Ù."); return; } float distance = GetDistance(player.transform.position, shell.position); Debug.Log( $"[ShellGameManager] ÆÇÁ¤ Á¤º¸ / °Å¸®: {distance:F2} / ¼º°ø °¡´É °Å¸®: {collectDistance:F2} / Á¶°³ ¿­¸²: {shellController.IsOpen} / FailWhenClosed: {failWhenClosed}" ); if (distance > collectDistance) { if (ui != null) ui.SetPromptFarFromShell(); Debug.Log("[ShellGameManager] Á¶°³¿Í ³Ê¹« ¸Ö¾î¼­ º¸»óÀ» ȹµæÇÒ ¼ö ¾ø½À´Ï´Ù."); return; } if (shellController.IsOpen) { Debug.Log("[ShellGameManager] ¼º°ø Á¶°Ç ¸¸Á·: Á¶°³ ±Ùó + Á¶°³ ¿­¸²"); Success(); } else { if (failWhenClosed) { Debug.Log("[ShellGameManager] ½ÇÆÐ Á¶°Ç ¸¸Á·: Á¶°³ ±Ùó + Á¶°³ ´ÝÈû"); Fail(); } else { if (ui != null) ui.SetPromptNearShell(false); Debug.Log("[ShellGameManager] Á¶°³°¡ ´ÝÇô À־ ¾ÆÁ÷ ȹµæÇÒ ¼ö ¾ø½À´Ï´Ù. FailWhenClosed°¡ ²¨Á® ÀÖ½À´Ï´Ù."); } } } private float GetDistance(Vector3 a, Vector3 b) { if (ignoreHeight) { a.y = 0f; b.y = 0f; } return Vector3.Distance(a, b); } private void Success() { gameFinished = true; if (ui != null) { ui.ShowReward(true); Debug.Log("[ShellGameManager] UIManager.ShowReward(true) È£ÃâµÊ"); } else { Debug.LogWarning("[ShellGameManager] UI ÂüÁ¶°¡ ºñ¾î À־ ¼º°ø UI¸¦ Ç¥½ÃÇÒ ¼ö ¾ø½À´Ï´Ù."); } onSuccess?.Invoke(); Debug.Log("[ShellGameManager] SUCCESS! Á¶°³°¡ ¿­¸° ŸÀֿ̹¡ º¸»óÀ» ȹµæÇß½À´Ï´Ù."); } private void Fail() { gameFinished = true; if (ui != null) { ui.ShowReward(false); Debug.Log("[ShellGameManager] UIManager.ShowReward(false) È£ÃâµÊ"); } else { Debug.LogWarning("[ShellGameManager] UI ÂüÁ¶°¡ ºñ¾î À־ ½ÇÆÐ UI¸¦ Ç¥½ÃÇÒ ¼ö ¾ø½À´Ï´Ù."); } onFail?.Invoke(); Debug.Log("[ShellGameManager] FAIL! Á¶°³°¡ ´ÝÈù ŸÀֿ̹¡ ½ÃµµÇß½À´Ï´Ù."); } public void ResetGame() { gameFinished = false; if (ui != null) { ui.HideReward(); ui.SetPromptDefault(); if (showDebugLog) Debug.Log("[ShellGameManager] UI ÃʱâÈ­ ¿Ï·á"); } else { Debug.LogWarning("[ShellGameManager] UI ÂüÁ¶°¡ ºñ¾î ÀÖ½À´Ï´Ù."); } if (shellController != null) { shellController.ResetShell(); if (showDebugLog) Debug.Log("[ShellGameManager] ShellController.ResetShell È£ÃâµÊ"); } else { Debug.LogWarning("[ShellGameManager] ShellController ÂüÁ¶°¡ ºñ¾î ÀÖ½À´Ï´Ù."); } if (showDebugLog) Debug.Log("[ShellGameManager] ResetGame ¿Ï·á"); } private void OnCollectPerformed(InputAction.CallbackContext context) { Debug.Log( $"[ShellGameManager] Collect Action ÀÔ·ÂµÊ / Action: {context.action.name} / Control: {context.control?.path} / Phase: {context.phase}" ); TryCollectReward(); } private void OnResetPerformed(InputAction.CallbackContext context) { Debug.Log( $"[ShellGameManager] Reset Action ÀÔ·ÂµÊ / Action: {context.action.name} / Control: {context.control?.path} / Phase: {context.phase}" ); ResetGame(); } // PlayerInput Send Messages ¹æ½ÄÀ» ¾µ ¶§µµ ȣȯµÇ°Ô ³²°ÜµÐ ÇÔ¼öÀÔ´Ï´Ù. public void OnCollect(InputValue value) { Debug.Log("[ShellGameManager] OnCollect(InputValue) È£ÃâµÊ"); if (value.isPressed) { Debug.Log("[ShellGameManager] OnCollect ÀÔ·Â Pressed »óÅÂ"); TryCollectReward(); } else { Debug.Log("[ShellGameManager] OnCollect ÀÔ·ÂÀÌ Pressed »óŰ¡ ¾Æ´Ô"); } } public void OnReset(InputValue value) { Debug.Log("[ShellGameManager] OnReset(InputValue) È£ÃâµÊ"); if (value.isPressed) { Debug.Log("[ShellGameManager] OnReset ÀÔ·Â Pressed »óÅÂ"); ResetGame(); } } }