game: format scripts

This commit is contained in:
Mark Joshwel 2024-07-04 04:37:15 +08:00
parent 4aa6329bdf
commit 73d46c302b
6 changed files with 180 additions and 176 deletions

View file

@ -8,39 +8,15 @@
using UnityEngine;
/// <summary>
/// singleton class for handling audio in the game
/// singleton class for handling audio in the game
/// </summary>
public class AudioManager : MonoBehaviour
{
/// <summary>
/// singleton pattern: define instance field for accessing the singleton elsewhere
/// singleton pattern: define instance field for accessing the singleton elsewhere
/// </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")]
@ -68,7 +44,31 @@ private void Awake()
public AudioClip menuButtonHover;
/// <summary>
/// function to set default volumes for the audio sources
/// 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>
public void Start()
{
@ -80,7 +80,7 @@ public void Start()
}
/// <summary>
/// plays the audio clip once on the music source/channel
/// plays the audio clip once on the music source/channel
/// </summary>
/// <param name="clip">audio clip to play</param>
public void PlayOnMusicChannel(AudioClip clip)
@ -89,7 +89,7 @@ public void PlayOnMusicChannel(AudioClip clip)
}
/// <summary>
/// plays the audio clip once on the sound effects (sfx) source/channel
/// plays the audio clip once on the sound effects (sfx) source/channel
/// </summary>
/// <param name="clip">audio clip to play</param>
public void PlayOnSFXChannel(AudioClip clip)
@ -98,7 +98,7 @@ public void PlayOnSFXChannel(AudioClip clip)
}
/// <summary>
/// function to get the current volume of the music source/channel
/// function to get the current volume of the music source/channel
/// </summary>
/// <returns>volume as float from 0.0 to 1.0</returns>
public float GetMusicVolume()
@ -107,7 +107,7 @@ public float GetMusicVolume()
}
/// <summary>
/// sets the volume of the music source/channel
/// sets the volume of the music source/channel
/// </summary>
/// <param name="volume">float (0.0-1.0) to set the channel volume to</param>
public void SetMusicVolume(float volume)
@ -116,7 +116,7 @@ public void SetMusicVolume(float volume)
}
/// <summary>
/// function to get the current volume of the sound effects (sfx) source/channel
/// function to get the current volume of the sound effects (sfx) source/channel
/// </summary>
/// <returns>volume as float from 0.0 to 1.0</returns>
public float GetSfxVolume()
@ -125,7 +125,7 @@ public float GetSfxVolume()
}
/// <summary>
/// sets the volume of the sound effects (sfx) source/channel
/// sets the volume of the sound effects (sfx) source/channel
/// </summary>
/// <param name="volume">float (0.0-1.0) to set the channel volume to</param>
public void SetSfxVolume(float volume)

View file

@ -6,40 +6,55 @@
using System;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UIElements;
/// <summary>
/// common menu class for hover and click sound effects
/// on ui toolkit buttons.
/// override <c>OnEnable()</c> with the first call to <c>base.OnEnable()</c> or <c>PostEnable()</c>,
/// and set the variable <c>GameManager.DisplayState associatedState</c> to the respective menu state
/// common menu class for hover and click sound effects
/// on ui toolkit buttons.
/// override <c>OnEnable()</c> with the first call to <c>base.OnEnable()</c> or <c>PostEnable()</c>,
/// and set the variable <c>GameManager.DisplayState associatedState</c> to the respective menu state
/// </summary>
public class CommonMenu : MonoBehaviour
{
/// <summary>
/// associated display state with the menu for the game manager to filter out menus in a scene
/// associated display state with the menu for the game manager to filter out menus in a scene
/// </summary>
public GameManager.DisplayState associatedState = GameManager.DisplayState.UnassociatedState;
/// <summary>
/// the visual element object for the menu
/// </summary>
public VisualElement UI;
/// <summary>
/// manager for the game state
/// </summary>
protected GameManager Game;
/// <summary>
/// manager for audio
/// manager for audio
/// </summary>
protected AudioManager Audio;
/// <summary>
/// override this class but call <c>base.OnEnable()</c> first.
/// also set the <c>associatedState</c> variable to the respective menu state
/// manager for the game state
/// </summary>
protected GameManager Game;
/// <summary>
/// the visual element object for the menu
/// </summary>
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.
/// also set the <c>associatedState</c> variable to the respective menu state
/// </summary>
public virtual void OnEnable()
{
@ -48,7 +63,7 @@ public virtual void OnEnable()
}
/// <summary>
/// function to subscribe to mouse events and assign managers
/// function to subscribe to mouse events and assign managers
/// </summary>
public void PostEnable()
{
@ -65,23 +80,7 @@ public void PostEnable()
}
/// <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
/// function listener for <c>PointerOverEvents</c> and plays a hover sound if it's a button
/// </summary>
/// <param name="evt">event from UIE callback</param>
public virtual void HoverListener(PointerOverEvent evt)
@ -93,7 +92,7 @@ public virtual void HoverListener(PointerOverEvent evt)
}
/// <summary>
/// function listener for <c>ClickEvents</c> and plays a click sound if it's a button
/// function listener for <c>ClickEvents</c> and plays a click sound if it's a button
/// </summary>
/// <param name="evt">event from UIE callback</param>
public virtual void ClickListener(ClickEvent evt)
@ -105,7 +104,7 @@ public virtual void ClickListener(ClickEvent evt)
}
/// <summary>
/// generic decoupled function to play click sound
/// generic decoupled function to play click sound
/// </summary>
public virtual void PlayClick()
{
@ -114,7 +113,7 @@ public virtual void PlayClick()
}
/// <summary>
/// generic decoupled function to play hover sound
/// generic decoupled function to play hover sound
/// </summary>
public virtual void PlayHover()
{

View file

@ -4,22 +4,21 @@
* description: credits menu script for handling credits menu button functions
*/
using UnityEngine;
using UnityEngine.UIElements;
/// <summary>
/// class managing the credits menu and button function invocations
/// class managing the credits menu and button function invocations
/// </summary>
public class CreditsMenu : CommonMenu
{
/// <summary>
/// button to return to main menu
/// button to return to main menu
/// </summary>
public Button ButtonReturn;
/// <summary>
/// function to associate a display state with the menu,
/// and subscribe button events to their respective functions
/// function to associate a display state with the menu,
/// and subscribe button events to their respective functions
/// </summary>
public override void OnEnable()
{
@ -33,8 +32,8 @@ public override void OnEnable()
}
/// <summary>
/// handles return to main menu button press,
/// signals the game manager appropriately
/// handles return to main menu button press,
/// signals the game manager appropriately
/// </summary>
private void OptionReturnToMainMenu()
{

View file

@ -4,32 +4,46 @@
* 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
/// singleton class for managing the game state as a single source of truth
/// </summary>
public class GameManager : MonoBehaviour
{
/// <summary>
/// singleton pattern: define instance field for accessing the singleton elsewhere
/// 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>
public static GameManager Instance;
/// <summary>
/// the current state of the game
/// the current state of the game
/// </summary>
private DisplayState _state = DisplayState.UnassociatedState;
/// <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>
// TODO: remove this if not needed
public bool Paused => _state != DisplayState.Game;
/// <summary>
/// function to set don't destroy on load and check for multiple instances
/// function to set don't destroy on load and check for multiple instances
/// </summary>
private void Awake()
{
@ -54,7 +68,7 @@ private void Awake()
}
/// <summary>
/// called when game starts, sets state to main menu
/// called when game starts, sets state to main menu
/// </summary>
private void Start()
{
@ -62,40 +76,23 @@ private void Start()
}
/// <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
/// helper function to hide any menu that is currently showing
/// </summary>
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)
{
// hide each menu under the parent object\
foreach (Transform menuChild in menu.transform)
{
Debug.Log($"GameManager: HideMenuHelper - hiding menu '{menuChild}'");
menuChild.gameObject.SetActive(false);
}
Debug.Log($"GameManager: HideMenuHelper - hiding menu '{menuChild}'");
menuChild.gameObject.SetActive(false);
}
}
/// <summary>
/// helper function for <c>SetDisplayState()</c> to pause the game,
/// called before the incoming game state is set
/// helper function for <c>SetDisplayState()</c> to pause the game,
/// called before the incoming game state is set
/// </summary>
/// <param name="incomingState">the to-be-set state of the game</param>
private void PauseGameHelper(DisplayState incomingState)
@ -118,44 +115,50 @@ 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. 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}");
// 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}");
// 3. if the associated state is the same as the incoming state, then show the menu
if (associatedState != incomingState)
continue;
// 3. if the associated state is the same as the incoming state, then show the menu
if (associatedState != incomingState)
continue;
Debug.Log($"GameManager: PauseGameHelper - showing menu for {incomingState}");
menu.gameObject.SetActive(true);
}
Debug.Log($"GameManager: PauseGameHelper - showing menu for {incomingState}");
menu.gameObject.SetActive(true);
}
}
/// <summary>
/// helper function for <c>SetDisplayState()</c> to resume the game,
/// called before the incoming game state is set
/// helper function for <c>SetDisplayState()</c> to resume the game,
/// called before the incoming game state is set
/// </summary>
/// <param name="incomingState">the to-be-set state of the game</param>
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;
@ -167,8 +170,8 @@ private void ResumeGameHelper(DisplayState incomingState)
}
/// <summary>
/// function to show a menu based on the enum passed,
/// and any other necessary actions
/// function to show a menu based on the enum passed,
/// and any other necessary actions
/// </summary>
/// <param name="displayState">the game menu to show</param>
public void SetDisplayState(DisplayState displayState)
@ -188,26 +191,26 @@ 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;
}
}
/// <summary>
/// wrapper function to quit the game
/// in case of any cleanup needed
/// wrapper function to quit the game
/// in case of any cleanup needed
/// </summary>
public void Quit()
{
@ -216,10 +219,13 @@ public void Quit()
}
/// <summary>
/// resets game state and starts a new game, will call <c>SetDisplayState()</c>
/// resets game state and starts a new game, will call <c>SetDisplayState()</c>
/// </summary>
public void NewGame()
{
// set to game state
SetDisplayState(DisplayState.Game);
// TODO
}
}

View file

@ -8,33 +8,33 @@
using UnityEngine.UIElements;
/// <summary>
/// class managing the main menu and button function invocations
/// class managing the main menu and button function invocations
/// </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
/// button to show credits menu
/// </summary>
public Button ButtonCredits;
/// <summary>
/// button to quit game
/// button to quit game
/// </summary>
public Button ButtonExit;
/// <summary>
/// function to associate a display state with the menu,
/// and subscribe button events to their respective functions
/// 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
/// </summary>
public override void OnEnable()
{
@ -63,18 +63,18 @@ public override void OnEnable()
}
/// <summary>
/// handles start button press,
/// signals the game manager appropriately
/// handles start button press,
/// signals the game manager appropriately
/// </summary>
private void OptionStartGame()
{
// start game
Game.SetDisplayState(GameManager.DisplayState.Game);
Game.NewGame();
}
/// <summary>
/// handles credits button press,
/// signals the game manager appropriately
/// handles credits button press,
/// signals the game manager appropriately
/// </summary>
private void OptionShowCredits()
{
@ -83,8 +83,8 @@ private void OptionShowCredits()
}
/// <summary>
/// handles options button press,
/// signals the game manager appropriately
/// handles options button press,
/// signals the game manager appropriately
/// </summary>
private void OptionShowOptions()
{
@ -93,8 +93,8 @@ private void OptionShowOptions()
}
/// <summary>
/// handles quit button press,
/// signals the game manager appropriately
/// handles quit button press,
/// signals the game manager appropriately
/// </summary>
private void OptionQuitGame()
{

View file

@ -8,28 +8,28 @@
using UnityEngine.UIElements;
/// <summary>
/// class managing the credits menu and button function invocations
/// class managing the credits menu and button function invocations
/// </summary>
public class OptionsMenu : CommonMenu
{
/// <summary>
/// button to return to main menu
/// button to return to main menu
/// </summary>
public Button ButtonReturn;
/// <summary>
/// slider for music volume
/// slider for music volume
/// </summary>
public Slider SliderAudioMusic;
/// <summary>
/// slider for sfx volume
/// slider for sfx volume
/// </summary>
public Slider SliderAudioSfx;
/// <summary>
/// function to associate a display state with the menu,
/// and subscribe button events to their respective functions
/// function to associate a display state with the menu,
/// and subscribe button events to their respective functions
/// </summary>
public override void OnEnable()
{
@ -58,8 +58,8 @@ public override void OnEnable()
}
/// <summary>
/// handles return to main menu button press,
/// signals the game manager appropriately
/// handles return to main menu button press,
/// signals the game manager appropriately
/// </summary>
private void OptionReturnToMainMenu()
{
@ -68,8 +68,8 @@ private void OptionReturnToMainMenu()
}
/// <summary>
/// handle music volume slider change,
/// sets the music channel volume in the audio manager appropriately
/// handle music volume slider change,
/// sets the music channel volume in the audio manager appropriately
/// </summary>
/// <param name="evt"></param>
private void OptionSetMusicVolume(ChangeEvent<float> evt)
@ -80,8 +80,8 @@ private void OptionSetMusicVolume(ChangeEvent<float> evt)
}
/// <summary>
/// handle sfx volume slider change,
/// sets the sfx channel volume in the audio manager appropriately
/// handle sfx volume slider change,
/// sets the sfx channel volume in the audio manager appropriately
/// </summary>
/// <param name="evt"></param>
private void OptionSetSfxVolume(ChangeEvent<float> evt)