2023-04-11 19:55:24 -07:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2023-04-25 13:27:12 -07:00
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
using UnityEngine.UI;
|
2023-04-11 19:55:24 -07:00
|
|
|
|
|
|
|
public class StateController : MonoBehaviour {
|
|
|
|
|
2023-04-13 16:52:11 -07:00
|
|
|
[Header("Respawning")]
|
|
|
|
[SerializeField] GameObject player;
|
|
|
|
public GameObject spawnPoint;
|
|
|
|
[SerializeField] private GameObject deathCanvas;
|
2023-04-11 19:55:24 -07:00
|
|
|
|
2023-04-25 13:27:12 -07:00
|
|
|
[Header("Pause Menu")]
|
|
|
|
public bool isPaused = false;
|
|
|
|
public GameObject pauseMenuCanvas;
|
|
|
|
|
2023-04-26 13:27:08 -07:00
|
|
|
[Header("Debug")]
|
|
|
|
public bool inDebugMode;
|
2023-04-26 13:03:48 -07:00
|
|
|
GameObject debugCanvas;
|
|
|
|
|
2023-04-13 16:52:11 -07:00
|
|
|
void Awake() {
|
2023-04-25 15:38:12 -07:00
|
|
|
// check to see if a state controller already exists
|
|
|
|
if (GameObject.FindGameObjectWithTag("StateController") != null) {
|
|
|
|
Destroy(this.gameObject);
|
|
|
|
} else { // if it doesn't, then this is the only one
|
|
|
|
this.gameObject.tag = "StateController";
|
|
|
|
}
|
2023-04-25 13:27:12 -07:00
|
|
|
DontDestroyOnLoad(this.gameObject);
|
|
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
2023-04-26 13:03:48 -07:00
|
|
|
|
2023-04-26 13:27:08 -07:00
|
|
|
if (inDebugMode) {
|
|
|
|
debugCanvas = GameObject.Find("DebugCanvas");
|
|
|
|
debugCanvas.SetActive(false);
|
|
|
|
}
|
2023-04-25 13:27:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
|
|
|
|
deathCanvas = GameObject.Find("DeathUICanvas");
|
|
|
|
if (deathCanvas != null) {
|
|
|
|
Button respawnButton = GameObject.Find("RespawnButton").GetComponent<Button>();
|
|
|
|
respawnButton.onClick.AddListener(RespawnPlayer);
|
|
|
|
deathCanvas.SetActive(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
pauseMenuCanvas = GameObject.Find("PauseMenuCanvas");
|
|
|
|
if (pauseMenuCanvas != null) {
|
2023-04-25 15:38:12 -07:00
|
|
|
Button resumeButton = GameObject.Find("ResumeButton").GetComponent<Button>();
|
|
|
|
resumeButton.onClick.AddListener(Unpause);
|
2023-04-25 13:27:12 -07:00
|
|
|
TogglePauseMenu(false);
|
|
|
|
}
|
|
|
|
|
2023-04-25 15:38:12 -07:00
|
|
|
if (isPaused) {
|
|
|
|
Unpause();
|
|
|
|
}
|
2023-04-26 13:03:48 -07:00
|
|
|
}
|
2023-04-25 15:38:12 -07:00
|
|
|
|
2023-04-26 13:03:48 -07:00
|
|
|
void OnToggleDebugMenu() {
|
2023-04-26 13:27:08 -07:00
|
|
|
if (inDebugMode) {
|
|
|
|
debugCanvas.SetActive(!debugCanvas.activeSelf);
|
|
|
|
}
|
2023-04-25 13:27:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnPause() {
|
2023-04-26 13:03:48 -07:00
|
|
|
if (pauseMenuCanvas != null) {
|
|
|
|
if (!isPaused) {
|
|
|
|
Time.timeScale = 0;
|
|
|
|
TogglePauseMenu(true);
|
|
|
|
} else {
|
|
|
|
Time.timeScale = 1;
|
|
|
|
TogglePauseMenu(false);
|
|
|
|
}
|
|
|
|
isPaused = !isPaused;
|
2023-04-25 13:27:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 15:38:12 -07:00
|
|
|
public void Unpause() {
|
|
|
|
Time.timeScale = 1;
|
|
|
|
TogglePauseMenu(false);
|
|
|
|
isPaused = !isPaused;
|
|
|
|
}
|
|
|
|
|
2023-04-25 13:27:12 -07:00
|
|
|
void TogglePauseMenu(bool showPauseMenu) {
|
|
|
|
pauseMenuCanvas.SetActive(showPauseMenu);
|
2023-04-11 19:55:24 -07:00
|
|
|
}
|
|
|
|
|
2023-04-13 16:52:11 -07:00
|
|
|
public void RespawnPlayer() {
|
2023-04-17 15:22:24 -07:00
|
|
|
SetDeathCanvasActive(false);
|
2023-04-19 21:34:00 -07:00
|
|
|
GameObject.Find("Main Camera").GetComponent<CameraMovement>().FindPlayer();
|
2023-04-25 13:27:12 -07:00
|
|
|
Instantiate(player, spawnPoint.transform.position, player.transform.rotation);
|
2023-04-11 19:55:24 -07:00
|
|
|
}
|
|
|
|
|
2023-04-17 15:22:24 -07:00
|
|
|
public void SetDeathCanvasActive(bool activeState) {
|
|
|
|
deathCanvas.SetActive(activeState);
|
2023-04-11 19:55:24 -07:00
|
|
|
}
|
|
|
|
}
|