using System.Collections; using UnityEngine; using TMPro; using UnityEngine.UIElements; public class DoorInteraction : MonoBehaviour { [SerializeField] private Animator myDoor = null; [SerializeField] private string animationFile = "DoorAnimation.013"; // [SerializeField] private Transform playerCamera = null; [SerializeField] private float interactionDistance = 2f; [SerializeField] private TabletController tabletController = null; [SerializeField] private ControlScript controlScript = null; [SerializeField] private int bombs = 9; [SerializeField] private int gridSize = 11; [SerializeField] private int timeLimitSeconds = 10; [SerializeField] private TextMeshProUGUI targetDoorText = null; [SerializeField] private UnityEngine.UI.Image tabletIcon = null; [SerializeField] private TextMeshProUGUI closeTabletPrompt = null; public TMP_Text interactionPrompt; // private bool isTabletOpen = false; public bool skipPuzzle = false; private bool isBeingLookedAt = false; private bool startedPuzzle = false; private static int doorCount = 0; // n doors being looked at void Awake() { if (interactionPrompt != null && doorCount == 0) { interactionPrompt.enabled = false; } } void OnDisable() { if (interactionPrompt != null) { interactionPrompt.enabled = false; } } private void Update() { if (skipPuzzle) { // open door animation and delete self myDoor.Play(animationFile, 0, 0.0f); gameObject.SetActive(false); } else { bool lookedAt = IsPlayerLookingAtDoor(); if (lookedAt && !isBeingLookedAt) { isBeingLookedAt = true; Debug.Log("Door is being looked at" + gameObject.name); doorCount++; } else if (!lookedAt && isBeingLookedAt) { isBeingLookedAt = false; doorCount--; } // Debug.Log("Door count: " + doorCount); if (!tabletController.isShowing && doorCount > 0) { TextPopUp("Press T to open tablet."); tabletIcon.enabled = true; } // if (tabletController.isShowing && doorCount < 1) { // // we can tell to put away the tablet // if (interactionPrompt != null) // { // interactionPrompt.enabled = true; // } // // TextPopUp("Press T to open and close tablet."); // } if (tabletController.isShowing && lookedAt) { // set text on door to be Target Door: 000 (last 3 characters of animation file) if (targetDoorText != null) { targetDoorText.text = "Target Door: " + animationFile.Substring(animationFile.Length - 3); Debug.Log("Target Door: " + animationFile.Substring(animationFile.Length - 3)); } // if player is not on main menu and game is not initialized and puzzle has not been started we start the puzzle for this door if (!tabletController.isOnMainMenu && controlScript.GameState == ControlScript.EGameState.Uninitialized && !startedPuzzle) { StartPuzzle(); startedPuzzle = true; } } } if (doorCount <= 0 || tabletController.isShowing) { doorCount = 0; tabletIcon.enabled = false; if (interactionPrompt != null) { interactionPrompt.enabled = false; } } if (doorCount > 0 && !tabletController.isShowing) { // enable tablet icon on a screen tabletIcon.enabled = true; if (interactionPrompt != null) { interactionPrompt.enabled = true; } } } private void TextPopUp(string text) { if (interactionPrompt != null) { interactionPrompt.text = text; interactionPrompt.enabled = true; } } private bool IsPlayerLookingAtDoor() { Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)); if (Physics.Raycast(ray, out RaycastHit hit, interactionDistance)) { return hit.collider.gameObject == gameObject; } return false; } private void StartPuzzle() { if (tabletController == null) { Debug.LogError("TabletController reference is missing."); return; } controlScript.GridSizeX = gridSize; controlScript.GridSizeY = gridSize; controlScript.TotalMines = bombs; controlScript.timeLimitSeconds = timeLimitSeconds; Debug.Log("Starting puzzle"); controlScript.Restart(); StartCoroutine(WaitForPuzzleCompletion()); } private IEnumerator WaitForPuzzleCompletion() { yield return new WaitUntil(() => controlScript.GameState == ControlScript.EGameState.Win); yield return new WaitForSeconds(1.0f); controlScript.GameState = ControlScript.EGameState.Uninitialized; // set prompt on screen to close tablet closeTabletPrompt.enabled = true; myDoor.Play(animationFile, 0, 0.0f); startedPuzzle = false; // TextPopUp("Press T to Close tablet."); gameObject.SetActive(false); } }