change: Switched the state controller and scene controller to a singleton class

This doesnt' change any of the logic, but simplifies a lot of the main
game loop code.

Many things still rely on the singleton class, but shouldn't so this
will be fixed in a later commit
This commit is contained in:
Nicholas Novak
2023-05-04 02:19:51 -07:00
parent afbcb920ac
commit 5a7ce8fb2b
8 changed files with 301 additions and 211 deletions

View File

@@ -4,7 +4,19 @@ using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class StateController : MonoBehaviour {
public enum UnlockedItems
{
None,
Trumpet,
Tambourine,
Clarinet,
}
public class StateController : MonoBehaviour
{
// Singleton class
public static StateController Instance = null;
[Header("Respawning")]
[SerializeField] GameObject player;
@@ -17,7 +29,7 @@ public class StateController : MonoBehaviour {
[Header("Debug")]
public bool inDebugMode;
GameObject debugCanvas;
public GameObject debugCanvas;
[Header("Enemies")]
GameObject[] enemiesInScene;
@@ -26,33 +38,53 @@ public class StateController : MonoBehaviour {
GameObject victoryCanvas;
[Header("Unlocked Items")]
[SerializeField] public bool unlockedTrumpet = false;
[SerializeField] public bool unlockedTambourine = false;
[SerializeField] public bool unlockedClarinet = false;
public UnlockedItems itemProgression = UnlockedItems.None;
void Awake() {
// 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";
void Awake()
{
// TODO: Remove this when done
AudioListener.pause = true;
if (Instance == null)
{
Instance = this;
}
DontDestroyOnLoad(this.gameObject);
//DontDestroyOnLoad(this.gameObject);
SceneManager.sceneLoaded += OnSceneLoaded;
if (inDebugMode) {
if (this.inDebugMode)
{
debugCanvas = GameObject.Find("DebugCanvas");
debugCanvas.SetActive(false);
}
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
public bool HasTrumpet()
{
return this.itemProgression >= UnlockedItems.Trumpet;
}
public bool HasTambourine()
{
return this.itemProgression >= UnlockedItems.Tambourine;
}
public bool HasClarinet()
{
return this.itemProgression >= UnlockedItems.Clarinet;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
#region FIND OBJECTS
deathCanvas = GameObject.Find("DeathUICanvas");
if (deathCanvas != null) {
if (deathCanvas != null)
{
Button respawnButton = GameObject.Find("RespawnButton").GetComponent<Button>();
if (respawnButton == null) {
if (respawnButton == null)
{
print("respawn button not found!");
}
respawnButton.onClick.AddListener(RespawnPlayer);
@@ -60,14 +92,16 @@ public class StateController : MonoBehaviour {
}
pauseMenuCanvas = GameObject.Find("PauseMenuCanvas");
if (pauseMenuCanvas != null) {
if (pauseMenuCanvas != null)
{
Button resumeButton = GameObject.Find("ResumeButton").GetComponent<Button>();
resumeButton.onClick.AddListener(Unpause);
TogglePauseMenu(false);
}
victoryCanvas = GameObject.Find("VictoryCanvas");
if (victoryCanvas != null) {
if (victoryCanvas != null)
{
Button continueButton = GameObject.Find("ContinueButton").GetComponent<Button>();
continueButton.onClick.AddListener(ContinueToNextLevel);
victoryCanvas.SetActive(false);
@@ -77,49 +111,47 @@ public class StateController : MonoBehaviour {
enemiesInScene = GameObject.FindGameObjectsWithTag("Enemy");
// print(enemiesInScene);
if (isPaused) {
if (isPaused)
{
Unpause();
}
#endregion
#region UNLOCK ITEMS
if (SceneManager.GetActiveScene().name == "GrenouilleVillage") {
unlockedTrumpet = false;
unlockedTambourine = false;
unlockedClarinet = false;
}
else if (SceneManager.GetActiveScene().name == "Brasslands")
switch (SceneManager.GetActiveScene().name)
{
unlockedTrumpet = true;
unlockedTambourine = false;
unlockedClarinet = false;
}
else if (SceneManager.GetActiveScene().name == "GrappleScene") {
unlockedTrumpet = true;
unlockedTambourine = true;
unlockedClarinet = false;
}
else if (SceneManager.GetActiveScene().name == "ClarinetScene") {
unlockedTrumpet = true;
unlockedTambourine = true;
unlockedClarinet = true;
}
case "GrenouilleVillage":
this.itemProgression = UnlockedItems.None;
break;
case "GrappleScene":
this.itemProgression = UnlockedItems.Trumpet;
break;
case "ClarinetScene":
this.itemProgression = UnlockedItems.Clarinet;
break;
};
#endregion
}
void OnToggleDebugMenu() {
if (inDebugMode) {
debugCanvas.SetActive(!debugCanvas.activeSelf);
void OnToggleDebugMenu()
{
if (inDebugMode)
{
debugCanvas.SetActive(!this.debugCanvas.activeSelf);
}
}
void OnPause() {
if (pauseMenuCanvas != null) {
if (!isPaused) {
void OnPause()
{
if (pauseMenuCanvas != null)
{
if (!isPaused)
{
Time.timeScale = 0;
TogglePauseMenu(true);
} else {
}
else
{
Time.timeScale = 1;
TogglePauseMenu(false);
}
@@ -127,17 +159,20 @@ public class StateController : MonoBehaviour {
}
}
public void Unpause() {
public void Unpause()
{
Time.timeScale = 1;
TogglePauseMenu(false);
isPaused = !isPaused;
}
void TogglePauseMenu(bool showPauseMenu) {
void TogglePauseMenu(bool showPauseMenu)
{
pauseMenuCanvas.SetActive(showPauseMenu);
}
public void RespawnPlayer() {
public void RespawnPlayer()
{
Destroy(GameObject.FindGameObjectWithTag("Player"));
SetDeathCanvasActive(false);
GameObject.Find("Main Camera").GetComponent<CameraMovement>().FindPlayer();
@@ -145,21 +180,26 @@ public class StateController : MonoBehaviour {
Instantiate(player, spawnPoint.transform.position, player.transform.rotation);
}
public void RespawnEnemies() {
foreach (GameObject enemy in enemiesInScene) {
public void RespawnEnemies()
{
foreach (GameObject enemy in enemiesInScene)
{
enemy.SetActive(true);
}
}
public void SetDeathCanvasActive(bool activeState) {
public void SetDeathCanvasActive(bool activeState)
{
deathCanvas.SetActive(activeState);
}
public void ShowVictoryCanvas() {
public void ShowVictoryCanvas()
{
victoryCanvas.SetActive(true);
}
public void ContinueToNextLevel() {
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneController>().NextScene();
public void ContinueToNextLevel()
{
SceneController.Instance.NextScene();
}
}