ofb/Assets/Scripts/StateController.cs

97 lines
2.8 KiB
C#
Raw Normal View History

2023-04-11 19:55:24 -07:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
2023-04-11 19:55:24 -07:00
public class StateController : MonoBehaviour {
[Header("Respawning")]
[SerializeField] GameObject player;
public GameObject spawnPoint;
[SerializeField] private GameObject deathCanvas;
2023-04-11 19:55:24 -07:00
[Header("Pause Menu")]
public bool isPaused = false;
public GameObject pauseMenuCanvas;
[Header("Debug")]
public bool inDebugMode;
GameObject debugCanvas;
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";
}
DontDestroyOnLoad(this.gameObject);
SceneManager.sceneLoaded += OnSceneLoaded;
if (inDebugMode) {
debugCanvas = GameObject.Find("DebugCanvas");
debugCanvas.SetActive(false);
}
}
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);
TogglePauseMenu(false);
}
2023-04-25 15:38:12 -07:00
if (isPaused) {
Unpause();
}
}
2023-04-25 15:38:12 -07:00
void OnToggleDebugMenu() {
if (inDebugMode) {
debugCanvas.SetActive(!debugCanvas.activeSelf);
}
}
void OnPause() {
if (pauseMenuCanvas != null) {
if (!isPaused) {
Time.timeScale = 0;
TogglePauseMenu(true);
} else {
Time.timeScale = 1;
TogglePauseMenu(false);
}
isPaused = !isPaused;
}
}
2023-04-25 15:38:12 -07:00
public void Unpause() {
Time.timeScale = 1;
TogglePauseMenu(false);
isPaused = !isPaused;
}
void TogglePauseMenu(bool showPauseMenu) {
pauseMenuCanvas.SetActive(showPauseMenu);
2023-04-11 19:55:24 -07:00
}
public void RespawnPlayer() {
SetDeathCanvasActive(false);
GameObject.Find("Main Camera").GetComponent<CameraMovement>().FindPlayer();
Instantiate(player, spawnPoint.transform.position, player.transform.rotation);
2023-04-11 19:55:24 -07:00
}
public void SetDeathCanvasActive(bool activeState) {
deathCanvas.SetActive(activeState);
2023-04-11 19:55:24 -07:00
}
}