StellarXipher/Assets/Scripts/OpenDoor.cs
EthanPisani e5e9a77b01
All checks were successful
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (push) Successful in 15m19s
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (push) Successful in 9m28s
Build project / Publish to itch.io (StandaloneLinux64) (push) Successful in 6s
Build project / Publish to itch.io (StandaloneWindows64) (push) Successful in 5s
added alpha version of word puzzle and simon says puzzle to tablet
2025-03-24 23:51:27 -04:00

203 lines
6.3 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;
[SerializeField] private TextMeshProUGUI targetDoorText = null;
[SerializeField] private UnityEngine.UI.Image tabletIcon = null;
[SerializeField] private TextMeshProUGUI 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;
}
}
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 == 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();
break;
}
StartCoroutine(WaitForPuzzleCompletion());
}
private IEnumerator WaitForPuzzleCompletion()
{
yield return new WaitUntil(() => tabletScript.gameState == PuzzleGameState.Win);
yield return new WaitForSeconds(1.0f);
// set prompt on screen to close tablet
closeTabletPrompt.enabled = true;
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);
}
}