game: format scripts
This commit is contained in:
parent
4aa6329bdf
commit
73d46c302b
|
@ -17,30 +17,6 @@ public class AudioManager : MonoBehaviour
|
|||
/// </summary>
|
||||
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
|
||||
// so we can control their volumes separately
|
||||
[Header("Audio Sources")]
|
||||
|
@ -67,6 +43,30 @@ private void Awake()
|
|||
// audio clip for menu button hover
|
||||
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>
|
||||
/// function to set default volumes for the audio sources
|
||||
/// </summary>
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
/// <summary>
|
||||
|
@ -23,9 +22,9 @@ public class CommonMenu : MonoBehaviour
|
|||
public GameManager.DisplayState associatedState = GameManager.DisplayState.UnassociatedState;
|
||||
|
||||
/// <summary>
|
||||
/// the visual element object for the menu
|
||||
/// manager for audio
|
||||
/// </summary>
|
||||
public VisualElement UI;
|
||||
protected AudioManager Audio;
|
||||
|
||||
/// <summary>
|
||||
/// manager for the game state
|
||||
|
@ -33,9 +32,25 @@ public class CommonMenu : MonoBehaviour
|
|||
protected GameManager Game;
|
||||
|
||||
/// <summary>
|
||||
/// manager for audio
|
||||
/// the visual element object for the menu
|
||||
/// </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>
|
||||
/// override this class but call <c>base.OnEnable()</c> first.
|
||||
|
@ -64,22 +79,6 @@ public void PostEnable()
|
|||
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>
|
||||
/// function listener for <c>PointerOverEvents</c> and plays a hover sound if it's a button
|
||||
/// </summary>
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
* description: credits menu script for handling credits menu button functions
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -4,15 +4,28 @@
|
|||
* description: game manager singleton for single source of truth state management
|
||||
*/
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
/// <summary>
|
||||
/// singleton class for managing the game state as a single source of truth
|
||||
/// </summary>
|
||||
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>
|
||||
/// singleton pattern: define instance field for accessing the singleton elsewhere
|
||||
/// </summary>
|
||||
|
@ -26,6 +39,7 @@ public class GameManager : MonoBehaviour
|
|||
/// <summary>
|
||||
/// property to check if the game is paused based on the current <c>DisplayState</c>
|
||||
/// </summary>
|
||||
// TODO: remove this if not needed
|
||||
public bool Paused => _state != DisplayState.Game;
|
||||
|
||||
/// <summary>
|
||||
|
@ -61,21 +75,6 @@ private void Start()
|
|||
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>
|
||||
/// helper function to hide any menu that is currently showing
|
||||
/// </summary>
|
||||
|
@ -83,7 +82,6 @@ private void HideMenuHelper()
|
|||
{
|
||||
// get parent object tagged "Menus"
|
||||
foreach (var menu in GameObject.FindGameObjectsWithTag("Menus"))
|
||||
{
|
||||
// hide each menu under the parent object\
|
||||
foreach (Transform menuChild in menu.transform)
|
||||
{
|
||||
|
@ -91,7 +89,6 @@ private void HideMenuHelper()
|
|||
menuChild.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// helper function for <c>SetDisplayState()</c> to pause the game,
|
||||
|
@ -118,22 +115,21 @@ private void PauseGameHelper(DisplayState incomingState)
|
|||
return;
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
// hide any menu that is currently showing
|
||||
HideMenuHelper();
|
||||
|
||||
// show the menu based on the incoming state
|
||||
// 1. get all menus via the parent object tagged "Menus"
|
||||
foreach (var menuParent in GameObject.FindGameObjectsWithTag("Menus"))
|
||||
{
|
||||
// 2. get all menus under the parent object
|
||||
foreach (Transform menu in menuParent.transform)
|
||||
{
|
||||
// 2. check its associated state
|
||||
var associatedState = menu.gameObject.GetComponent<CommonMenu>().associatedState;
|
||||
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
|
||||
if (associatedState != incomingState)
|
||||
|
@ -143,7 +139,6 @@ private void PauseGameHelper(DisplayState incomingState)
|
|||
menu.gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// helper function for <c>SetDisplayState()</c> to resume the game,
|
||||
|
@ -153,9 +148,17 @@ private void PauseGameHelper(DisplayState incomingState)
|
|||
private void ResumeGameHelper(DisplayState incomingState)
|
||||
{
|
||||
// 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,
|
||||
// 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
|
||||
Time.timeScale = 1f;
|
||||
|
@ -188,19 +191,19 @@ public void SetDisplayState(DisplayState displayState)
|
|||
// set the state of the game to the incoming state
|
||||
_state = displayState;
|
||||
|
||||
// TODO: post-state change actions case switch
|
||||
// post-state change actions case switch
|
||||
switch (displayState)
|
||||
{
|
||||
// if we're transitioning to the game state,
|
||||
// change camera to the player camera
|
||||
case DisplayState.Game:
|
||||
// TODO
|
||||
// TODO: change camera to player camera
|
||||
return;
|
||||
|
||||
// if we're transitioning to the main menu state,
|
||||
// change camera to the main menu camera
|
||||
case DisplayState.ScreenMainMenu:
|
||||
// TODO
|
||||
// TODO: change camera to main menu camera
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -220,6 +223,9 @@ public void Quit()
|
|||
/// </summary>
|
||||
public void NewGame()
|
||||
{
|
||||
// set to game state
|
||||
SetDisplayState(DisplayState.Game);
|
||||
|
||||
// TODO
|
||||
}
|
||||
}
|
|
@ -12,16 +12,6 @@
|
|||
/// </summary>
|
||||
public class MainMenu : CommonMenu
|
||||
{
|
||||
/// <summary>
|
||||
/// button to play game
|
||||
/// </summary>
|
||||
public Button ButtonPlay;
|
||||
|
||||
/// <summary>
|
||||
/// button to show options menu
|
||||
/// </summary>
|
||||
public Button ButtonOptions;
|
||||
|
||||
/// <summary>
|
||||
/// button to show credits menu
|
||||
/// </summary>
|
||||
|
@ -32,6 +22,16 @@ public class MainMenu : CommonMenu
|
|||
/// </summary>
|
||||
public Button ButtonExit;
|
||||
|
||||
/// <summary>
|
||||
/// button to show options menu
|
||||
/// </summary>
|
||||
public Button ButtonOptions;
|
||||
|
||||
/// <summary>
|
||||
/// button to play game
|
||||
/// </summary>
|
||||
public Button ButtonPlay;
|
||||
|
||||
/// <summary>
|
||||
/// function to associate a display state with the menu,
|
||||
/// and subscribe button events to their respective functions
|
||||
|
@ -69,7 +69,7 @@ public override void OnEnable()
|
|||
private void OptionStartGame()
|
||||
{
|
||||
// start game
|
||||
Game.SetDisplayState(GameManager.DisplayState.Game);
|
||||
Game.NewGame();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Reference in a new issue