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

@@ -7,38 +7,46 @@ using UnityEngine.UI;
public class SceneController : MonoBehaviour
{
public static SceneController Instance = null;
// this object will always exist!
void Awake() {
// check to see if a state controller already exists
if (GameObject.FindGameObjectWithTag("SceneManager") != null) {
Destroy(this.gameObject);
} else { // if it doesn't, then this is the only one
this.gameObject.tag = "SceneManager";
void Awake()
{
if (Instance == null)
{
Instance = this;
}
// Make this object stay around when switching scenes
DontDestroyOnLoad(this.gameObject);
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
GameObject pauseMenu = GameObject.Find("PauseMenuCanvas");
if (pauseMenu != null) {
if (pauseMenu != null)
{
Button quitButton = GameObject.Find("QuitButton").GetComponent<Button>();
quitButton.onClick.AddListener(BackToMainMenu);
}
if (scene.buildIndex == 0) { // if this is the menu
if (scene.buildIndex == 0)
{ // if this is the menu
GameObject.Find("NewGameButton").GetComponent<Button>().onClick.AddListener(NextScene);
}
}
public void NextScene() {
public void NextScene()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void BackToMainMenu() {
public void BackToMainMenu()
{
SceneManager.LoadScene(0); // main menu scene should be 0
}
public void LoadChosenScene(int index) {
public void LoadChosenScene(int index)
{
SceneManager.LoadScene(index);
}