StellarXipher/Assets/Scripts/OpenDoor.cs
zephyr-jchen 42dd1d02ba
Some checks failed
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (push) Successful in 5m54s
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (push) Successful in 5m43s
Build project / Publish to itch.io (StandaloneLinux64) (push) Successful in 11s
Build project / Publish to itch.io (StandaloneWindows64) (push) Successful in 9s
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (pull_request) Has been cancelled
Build project / Publish to itch.io (StandaloneLinux64) (pull_request) Has been cancelled
Build project / Publish to itch.io (StandaloneWindows64) (pull_request) Has been cancelled
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (pull_request) Has been cancelled
add usb for doors
2025-04-19 08:00:08 +08:00

242 lines
7.4 KiB
C#

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 float interactionDistance = 2f;
[SerializeField] private TabletController tabletController = null;
[SerializeField] private TabletScript tabletScript = null;
[SerializeField] private ControlScript controlScript = null;
[SerializeField] private int bombs = 9;
[SerializeField] private int gridSize = 11;
[SerializeField] private int timeLimitSeconds = 10;
[SerializeField] private string overrideWord = string.Empty;
[SerializeField] private int simonSaysPuzzleIndex = 0;
[SerializeField] private TextMeshProUGUI targetDoorText = null;
[SerializeField] private UnityEngine.UI.Image tabletIcon = null;
[SerializeField] private GameObject closeTabletPrompt = null;
public TabletScreen doorPuzzleType = TabletScreen.Mines;
public TMP_Text interactionPrompt;
public bool skipPuzzle = false;
private bool isBeingLookedAt = false;
private bool startedPuzzle = false;
private static int doorCount = 0;
[Header("Required Item to Unlock")]
[SerializeField] private string requiredItemName = "Minesweeper";
[SerializeField] private Inventory inventory = null;
private static bool isImportantPrompt = false;
void Awake()
{
if (interactionPrompt != null && doorCount == 0)
{
interactionPrompt.enabled = false;
}
closeTabletPrompt.SetActive(false);
}
void OnDisable()
{
if (interactionPrompt != null)
{
interactionPrompt.enabled = false;
isImportantPrompt = false;
//Debug.Log("[isImportantPrompt] Reset to FALSE due to OnDisable or look away");
}
if (targetDoorText != null)
{
targetDoorText.text = "Target Door: 0";
}
}
private void Update()
{
if (skipPuzzle)
{
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--;
interactionPrompt.enabled = false;
isImportantPrompt = false;
//Debug.Log("[isImportantPrompt] Reset to FALSE due to OnDisable or look away");
}
if (!tabletController.isShowing && doorCount > 0 && !isImportantPrompt)
{
TextPopUp("Press TAB to open tablet.");
tabletIcon.enabled = true;
}
if (tabletController.isShowing && doorCount > 0)
{
tabletController.setIsOnMainMenu(true);
}
if (tabletController.isShowing && lookedAt)
{
if (!startedPuzzle)
{
if (inventory != null && inventory.HasItem(requiredItemName))
{
tabletScript.SetScreen(TabletScreen.MainMenu);
}
else
{
//isImportantPrompt = true;
string msg = "Missing USB for: " + requiredItemName;
Debug.Log("[USB Check] Showing: " + msg);
TextPopUp(msg, true);
return;
}
}
if (targetDoorText != null)
{
string sceneName = gameObject.scene.name;
int sceneNumber = int.Parse(sceneName.Substring(sceneName.Length - 1));
targetDoorText.text = "Target Door: " + sceneNumber + animationFile.Substring(animationFile.Length - 2);
}
if (!tabletController.isOnMainMenu && controlScript.GameState == EGameState.Uninitialized && !startedPuzzle)
{
StartPuzzle(doorPuzzleType);
startedPuzzle = true;
}
}
}
// Only hide the prompt if it's NOT an important one
if (!isImportantPrompt)
{
if (doorCount <= 0 || tabletController.isShowing)
{
doorCount = 0;
tabletIcon.enabled = false;
if (interactionPrompt != null)
{
// Debug.Log($"[Prompt OFF] Closing prompt. isImportantPrompt={isImportantPrompt}");
// Debug.Log($"[Prompt OFF] Hiding prompt: '{interactionPrompt.text}'");
interactionPrompt.enabled = false;
}
}
}
if (doorCount > 0 && !tabletController.isShowing)
{
tabletIcon.enabled = true;
if (interactionPrompt != null && !isImportantPrompt)
{
interactionPrompt.enabled = true;
}
}
}
private void TextPopUp(string text, bool important = false)
{
if (interactionPrompt != null)
{
Debug.Log($"[TextPopUp] Request: '{text}', important={important}, currentImportant={isImportantPrompt}");
if (!isImportantPrompt || important)
{
interactionPrompt.text = text;
interactionPrompt.enabled = true;
if (important)
{
isImportantPrompt = 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(TabletScreen screen)
{
if (tabletController == null)
{
Debug.LogError("TabletController reference is missing.");
return;
}
Debug.Log("Starting puzzle");
switch (screen)
{
case TabletScreen.Word:
tabletScript.SetScreenWord(overrideWord);
break;
case TabletScreen.Mines:
tabletScript.SetScreenMines(gridSize, bombs, timeLimitSeconds);
break;
case TabletScreen.MainMenu:
tabletScript.SetScreen(TabletScreen.MainMenu);
break;
case TabletScreen.SimonSays:
tabletScript.SetScreenSimonSays(simonSaysPuzzleIndex);
break;
}
StartCoroutine(WaitForPuzzleCompletion());
}
private IEnumerator WaitForPuzzleCompletion()
{
yield return new WaitUntil(() => tabletScript.gameState == PuzzleGameState.Win);
tabletScript.SetScreen(TabletScreen.Clear);
closeTabletPrompt.SetActive(true);
yield return new WaitForSeconds(1.0f);
myDoor.Play(animationFile, 0, 0.0f);
startedPuzzle = false;
tabletScript.gameState = PuzzleGameState.Uninitialized;
gameObject.SetActive(false);
}
}