game: format scripts
This commit is contained in:
parent
4aa6329bdf
commit
73d46c302b
6 changed files with 180 additions and 176 deletions
|
@ -17,30 +17,6 @@ public class AudioManager : MonoBehaviour
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static AudioManager Instance;
|
public static AudioManager Instance;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// function to set don't destroy on load and check for multiple instances
|
|
||||||
/// </summary>
|
|
||||||
private void Awake()
|
|
||||||
{
|
|
||||||
// check if instance hasn't been set yet
|
|
||||||
if (Instance == null)
|
|
||||||
{
|
|
||||||
// set this instance as the singleton instance
|
|
||||||
Instance = this;
|
|
||||||
// don't destroy this instance on scene load
|
|
||||||
DontDestroyOnLoad(gameObject);
|
|
||||||
|
|
||||||
Debug.Log("AudioManager: Awake as singleton instance");
|
|
||||||
}
|
|
||||||
// check if instance is already set and it's not this instance
|
|
||||||
else if (Instance != null && Instance != this)
|
|
||||||
{
|
|
||||||
Debug.Log("AudioManager: Awake as non-singleton instance, destroying self");
|
|
||||||
// destroy the new instance if it's not the singleton instance
|
|
||||||
Destroy(gameObject);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// declare separate audio sources for music and sfx
|
// declare separate audio sources for music and sfx
|
||||||
// so we can control their volumes separately
|
// so we can control their volumes separately
|
||||||
[Header("Audio Sources")]
|
[Header("Audio Sources")]
|
||||||
|
@ -67,6 +43,30 @@ public class AudioManager : MonoBehaviour
|
||||||
// audio clip for menu button hover
|
// audio clip for menu button hover
|
||||||
public AudioClip menuButtonHover;
|
public AudioClip menuButtonHover;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// function to set don't destroy on load and check for multiple instances
|
||||||
|
/// </summary>
|
||||||
|
private void Awake()
|
||||||
|
{
|
||||||
|
// check if instance hasn't been set yet
|
||||||
|
if (Instance == null)
|
||||||
|
{
|
||||||
|
// set this instance as the singleton instance
|
||||||
|
Instance = this;
|
||||||
|
// don't destroy this instance on scene load
|
||||||
|
DontDestroyOnLoad(gameObject);
|
||||||
|
|
||||||
|
Debug.Log("AudioManager: Awake as singleton instance");
|
||||||
|
}
|
||||||
|
// check if instance is already set and it's not this instance
|
||||||
|
else if (Instance != null && Instance != this)
|
||||||
|
{
|
||||||
|
Debug.Log("AudioManager: Awake as non-singleton instance, destroying self");
|
||||||
|
// destroy the new instance if it's not the singleton instance
|
||||||
|
Destroy(gameObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// function to set default volumes for the audio sources
|
/// function to set default volumes for the audio sources
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Serialization;
|
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -23,9 +22,9 @@ public class CommonMenu : MonoBehaviour
|
||||||
public GameManager.DisplayState associatedState = GameManager.DisplayState.UnassociatedState;
|
public GameManager.DisplayState associatedState = GameManager.DisplayState.UnassociatedState;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// the visual element object for the menu
|
/// manager for audio
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public VisualElement UI;
|
protected AudioManager Audio;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// manager for the game state
|
/// manager for the game state
|
||||||
|
@ -33,9 +32,25 @@ public class CommonMenu : MonoBehaviour
|
||||||
protected GameManager Game;
|
protected GameManager Game;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// manager for audio
|
/// the visual element object for the menu
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected AudioManager Audio;
|
public VisualElement UI;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// checks if The Menu (2022) was set up correctly
|
||||||
|
/// </summary>
|
||||||
|
/// <exception cref="Exception">throws an exception if UI, Game and Audio are not set</exception>
|
||||||
|
private void Start()
|
||||||
|
{
|
||||||
|
if (associatedState == GameManager.DisplayState.UnassociatedState)
|
||||||
|
throw new Exception("CommonMenu: associatedState not set");
|
||||||
|
|
||||||
|
if (Game == null)
|
||||||
|
throw new Exception("CommonMenu: Game not set (was base.OnEnable() or PostEnable() called?)");
|
||||||
|
|
||||||
|
if (Audio == null)
|
||||||
|
throw new Exception("CommonMenu: Audio not set (was base.OnEnable() or PostEnable() called?)");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// override this class but call <c>base.OnEnable()</c> first.
|
/// override this class but call <c>base.OnEnable()</c> first.
|
||||||
|
@ -64,22 +79,6 @@ public class CommonMenu : MonoBehaviour
|
||||||
UI.RegisterCallback<PointerOverEvent>(HoverListener);
|
UI.RegisterCallback<PointerOverEvent>(HoverListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// checks if The Menu (2022) was set up correctly
|
|
||||||
/// </summary>
|
|
||||||
/// <exception cref="Exception">throws an exception if UI, Game and Audio are not set</exception>
|
|
||||||
private void Start()
|
|
||||||
{
|
|
||||||
if (associatedState == GameManager.DisplayState.UnassociatedState)
|
|
||||||
throw new Exception("CommonMenu: associatedState not set");
|
|
||||||
|
|
||||||
if (Game == null)
|
|
||||||
throw new Exception("CommonMenu: Game not set (was base.OnEnable() or PostEnable() called?)");
|
|
||||||
|
|
||||||
if (Audio == null)
|
|
||||||
throw new Exception("CommonMenu: Audio not set (was base.OnEnable() or PostEnable() called?)");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// function listener for <c>PointerOverEvents</c> and plays a hover sound if it's a button
|
/// function listener for <c>PointerOverEvents</c> and plays a hover sound if it's a button
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
* description: credits menu script for handling credits menu button functions
|
* description: credits menu script for handling credits menu button functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using UnityEngine;
|
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -4,15 +4,28 @@
|
||||||
* description: game manager singleton for single source of truth state management
|
* description: game manager singleton for single source of truth state management
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.SceneManagement;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// singleton class for managing the game state as a single source of truth
|
/// singleton class for managing the game state as a single source of truth
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class GameManager : MonoBehaviour
|
public class GameManager : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// enum for available menus in the game, for use with <c>ShowMenu()</c>
|
||||||
|
/// </summary>
|
||||||
|
public enum DisplayState
|
||||||
|
{
|
||||||
|
Game,
|
||||||
|
ScreenMainMenu,
|
||||||
|
ScreenOptionsMenu,
|
||||||
|
ScreenCreditsMenu,
|
||||||
|
ScreenPauseMenu,
|
||||||
|
ScreenCaughtPause,
|
||||||
|
ScreenEscapedMenu,
|
||||||
|
UnassociatedState
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// singleton pattern: define instance field for accessing the singleton elsewhere
|
/// singleton pattern: define instance field for accessing the singleton elsewhere
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -26,6 +39,7 @@ public class GameManager : MonoBehaviour
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// property to check if the game is paused based on the current <c>DisplayState</c>
|
/// property to check if the game is paused based on the current <c>DisplayState</c>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
// TODO: remove this if not needed
|
||||||
public bool Paused => _state != DisplayState.Game;
|
public bool Paused => _state != DisplayState.Game;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -61,21 +75,6 @@ public class GameManager : MonoBehaviour
|
||||||
SetDisplayState(DisplayState.ScreenMainMenu);
|
SetDisplayState(DisplayState.ScreenMainMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// enum for available menus in the game, for use with <c>ShowMenu()</c>
|
|
||||||
/// </summary>
|
|
||||||
public enum DisplayState
|
|
||||||
{
|
|
||||||
Game,
|
|
||||||
ScreenMainMenu,
|
|
||||||
ScreenOptionsMenu,
|
|
||||||
ScreenCreditsMenu,
|
|
||||||
ScreenPauseMenu,
|
|
||||||
ScreenCaughtPause,
|
|
||||||
ScreenEscapedMenu,
|
|
||||||
UnassociatedState
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// helper function to hide any menu that is currently showing
|
/// helper function to hide any menu that is currently showing
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -83,7 +82,6 @@ public class GameManager : MonoBehaviour
|
||||||
{
|
{
|
||||||
// get parent object tagged "Menus"
|
// get parent object tagged "Menus"
|
||||||
foreach (var menu in GameObject.FindGameObjectsWithTag("Menus"))
|
foreach (var menu in GameObject.FindGameObjectsWithTag("Menus"))
|
||||||
{
|
|
||||||
// hide each menu under the parent object\
|
// hide each menu under the parent object\
|
||||||
foreach (Transform menuChild in menu.transform)
|
foreach (Transform menuChild in menu.transform)
|
||||||
{
|
{
|
||||||
|
@ -91,7 +89,6 @@ public class GameManager : MonoBehaviour
|
||||||
menuChild.gameObject.SetActive(false);
|
menuChild.gameObject.SetActive(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// helper function for <c>SetDisplayState()</c> to pause the game,
|
/// helper function for <c>SetDisplayState()</c> to pause the game,
|
||||||
|
@ -118,22 +115,21 @@ public class GameManager : MonoBehaviour
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
;
|
|
||||||
|
|
||||||
// hide any menu that is currently showing
|
// hide any menu that is currently showing
|
||||||
HideMenuHelper();
|
HideMenuHelper();
|
||||||
|
|
||||||
// show the menu based on the incoming state
|
// show the menu based on the incoming state
|
||||||
// 1. get all menus via the parent object tagged "Menus"
|
// 1. get all menus via the parent object tagged "Menus"
|
||||||
foreach (var menuParent in GameObject.FindGameObjectsWithTag("Menus"))
|
foreach (var menuParent in GameObject.FindGameObjectsWithTag("Menus"))
|
||||||
{
|
|
||||||
// 2. get all menus under the parent object
|
// 2. get all menus under the parent object
|
||||||
foreach (Transform menu in menuParent.transform)
|
foreach (Transform menu in menuParent.transform)
|
||||||
{
|
{
|
||||||
// 2. check its associated state
|
// 2. check its associated state
|
||||||
var associatedState = menu.gameObject.GetComponent<CommonMenu>().associatedState;
|
var associatedState = menu.gameObject.GetComponent<CommonMenu>().associatedState;
|
||||||
Debug.Log(
|
Debug.Log(
|
||||||
$"GameManager: PauseGameHelper - found menu '{menu}' with associated state {associatedState} against incoming state {incomingState}");
|
$"GameManager: PauseGameHelper - found menu '{menu}' "
|
||||||
|
+ $"with associated state {associatedState} "
|
||||||
|
+ $"against incoming state {incomingState}");
|
||||||
|
|
||||||
// 3. if the associated state is the same as the incoming state, then show the menu
|
// 3. if the associated state is the same as the incoming state, then show the menu
|
||||||
if (associatedState != incomingState)
|
if (associatedState != incomingState)
|
||||||
|
@ -143,7 +139,6 @@ public class GameManager : MonoBehaviour
|
||||||
menu.gameObject.SetActive(true);
|
menu.gameObject.SetActive(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// helper function for <c>SetDisplayState()</c> to resume the game,
|
/// helper function for <c>SetDisplayState()</c> to resume the game,
|
||||||
|
@ -153,9 +148,17 @@ public class GameManager : MonoBehaviour
|
||||||
private void ResumeGameHelper(DisplayState incomingState)
|
private void ResumeGameHelper(DisplayState incomingState)
|
||||||
{
|
{
|
||||||
// if we're NOT transitioning from a state of non-gameplay to a state of gameplay,
|
// if we're NOT transitioning from a state of non-gameplay to a state of gameplay,
|
||||||
|
// (which means currently we are in a state of gameplay),
|
||||||
// then we shouldn't do anything, because the game is already running,
|
// then we shouldn't do anything, because the game is already running,
|
||||||
// so we return early
|
// so we return early
|
||||||
if (_state != DisplayState.Game && incomingState == DisplayState.Game) return;
|
if (_state == DisplayState.Game || incomingState != DisplayState.Game)
|
||||||
|
{
|
||||||
|
Debug.Log(
|
||||||
|
"GameManager: ResumeGameHelper - returning prematurely as" +
|
||||||
|
$" _state={_state} and incomingState={incomingState}");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// else, we should resume the game
|
// else, we should resume the game
|
||||||
Time.timeScale = 1f;
|
Time.timeScale = 1f;
|
||||||
|
@ -188,19 +191,19 @@ public class GameManager : MonoBehaviour
|
||||||
// set the state of the game to the incoming state
|
// set the state of the game to the incoming state
|
||||||
_state = displayState;
|
_state = displayState;
|
||||||
|
|
||||||
// TODO: post-state change actions case switch
|
// post-state change actions case switch
|
||||||
switch (displayState)
|
switch (displayState)
|
||||||
{
|
{
|
||||||
// if we're transitioning to the game state,
|
// if we're transitioning to the game state,
|
||||||
// change camera to the player camera
|
// change camera to the player camera
|
||||||
case DisplayState.Game:
|
case DisplayState.Game:
|
||||||
// TODO
|
// TODO: change camera to player camera
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// if we're transitioning to the main menu state,
|
// if we're transitioning to the main menu state,
|
||||||
// change camera to the main menu camera
|
// change camera to the main menu camera
|
||||||
case DisplayState.ScreenMainMenu:
|
case DisplayState.ScreenMainMenu:
|
||||||
// TODO
|
// TODO: change camera to main menu camera
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -220,6 +223,9 @@ public class GameManager : MonoBehaviour
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void NewGame()
|
public void NewGame()
|
||||||
{
|
{
|
||||||
|
// set to game state
|
||||||
|
SetDisplayState(DisplayState.Game);
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -12,16 +12,6 @@ using UnityEngine.UIElements;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class MainMenu : CommonMenu
|
public class MainMenu : CommonMenu
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// button to play game
|
|
||||||
/// </summary>
|
|
||||||
public Button ButtonPlay;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// button to show options menu
|
|
||||||
/// </summary>
|
|
||||||
public Button ButtonOptions;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// button to show credits menu
|
/// button to show credits menu
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -32,6 +22,16 @@ public class MainMenu : CommonMenu
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Button ButtonExit;
|
public Button ButtonExit;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// button to show options menu
|
||||||
|
/// </summary>
|
||||||
|
public Button ButtonOptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// button to play game
|
||||||
|
/// </summary>
|
||||||
|
public Button ButtonPlay;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// function to associate a display state with the menu,
|
/// function to associate a display state with the menu,
|
||||||
/// and subscribe button events to their respective functions
|
/// and subscribe button events to their respective functions
|
||||||
|
@ -69,7 +69,7 @@ public class MainMenu : CommonMenu
|
||||||
private void OptionStartGame()
|
private void OptionStartGame()
|
||||||
{
|
{
|
||||||
// start game
|
// start game
|
||||||
Game.SetDisplayState(GameManager.DisplayState.Game);
|
Game.NewGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Reference in a new issue