StellarXipher/Assets/Scripts/OpenDoor.cs
EthanPisani c7e6fad050
Some checks failed
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (push) Has been cancelled
Build project / Publish to itch.io (StandaloneLinux64) (push) Has been cancelled
Build project / Publish to itch.io (StandaloneWindows64) (push) Has been cancelled
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (push) Has been cancelled
fix puzzles level3
2025-04-20 12:57:56 -04:00

231 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 Transform playerCamera = null;
[SerializeField] private float interactionDistance = 2f;
[SerializeField] private TabletController tabletController = null;
[SerializeField] private TabletScript tabletScript = null;
[SerializeField] private ControlScript controlScript = null;
// PUZZLES CONFIGS
// Mines puzzle
[SerializeField] private int bombs = 9;
[SerializeField] private int gridSize = 11;
[SerializeField] private int timeLimitSeconds = 10;
// Word puzzle
[SerializeField] private string overrideWord = string.Empty;
// Simon Says puzzle
[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;
// 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;
}
closeTabletPrompt.SetActive(false);
}
void OnDisable()
{
if (interactionPrompt != null)
{
interactionPrompt.enabled = false;
}
// set target door text to empty
if (targetDoorText != null)
{
targetDoorText.text = "Target Door: 0";
}
}
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 TAB to open tablet.");
tabletIcon.enabled = true;
// tabletScript.SetScreen(TabletScreen.MainMenu);
}
if (tabletController.isShowing && doorCount > 0)
{
tabletController.setIsOnMainMenu(true);
// tabletScript.SetScreen(TabletScreen.MainMenu);
}
// 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 (!startedPuzzle) {
tabletScript.SetScreen(TabletScreen.MainMenu);
}
if (targetDoorText != null)
{
string sceneName = gameObject.scene.name;
// check if scene has underscore, if yes remove underscore and everything after it
if (sceneName.Contains("_"))
{
sceneName = sceneName.Substring(0, sceneName.IndexOf("_"));
}
int sceneNumber = int.Parse(sceneName.Substring(sceneName.Length - 1));
targetDoorText.text = "Target Door: " +sceneNumber + animationFile.Substring(animationFile.Length - 2);
// 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 == EGameState.Uninitialized && !startedPuzzle)
{
StartPuzzle(doorPuzzleType);
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(TabletScreen screen)
{
if (tabletController == null)
{
Debug.LogError("TabletController reference is missing.");
return;
}
Debug.Log("Starting puzzle");
// get function to call for the set screen
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);
// set prompt on screen to close tablet
myDoor.Play(animationFile, 0, 0.0f);
startedPuzzle = false;
// TextPopUp("Press T to Close tablet.");
tabletScript.gameState = PuzzleGameState.Uninitialized; // reset the game state to default state
gameObject.SetActive(false);
}
}