From 73d46c302b858dd2adb4ffa70a24c8ec94bd9fbf Mon Sep 17 00:00:00 2001 From: Mark Joshwel Date: Thu, 4 Jul 2024 04:37:15 +0800 Subject: [PATCH] game: format scripts --- .../Assets/Scripts/AudioManager.cs | 66 +++++----- .../Assets/Scripts/CommonMenu.cs | 81 ++++++------ .../Assets/Scripts/CreditsMenu.cs | 13 +- .../Assets/Scripts/GameManager.cs | 122 +++++++++--------- .../Assets/Scripts/MainMenu.cs | 50 +++---- .../Assets/Scripts/OptionsMenu.cs | 24 ++-- 6 files changed, 180 insertions(+), 176 deletions(-) diff --git a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/AudioManager.cs b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/AudioManager.cs index ea8303d..ba9a6ac 100644 --- a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/AudioManager.cs +++ b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/AudioManager.cs @@ -8,39 +8,15 @@ using UnityEngine; /// -/// singleton class for handling audio in the game +/// singleton class for handling audio in the game /// public class AudioManager : MonoBehaviour { /// - /// singleton pattern: define instance field for accessing the singleton elsewhere + /// singleton pattern: define instance field for accessing the singleton elsewhere /// public static AudioManager Instance; - /// - /// function to set don't destroy on load and check for multiple instances - /// - 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; /// - /// function to set default volumes for the audio sources + /// function to set don't destroy on load and check for multiple instances + /// + 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); + } + } + + /// + /// function to set default volumes for the audio sources /// public void Start() { @@ -80,7 +80,7 @@ public void Start() } /// - /// plays the audio clip once on the music source/channel + /// plays the audio clip once on the music source/channel /// /// audio clip to play public void PlayOnMusicChannel(AudioClip clip) @@ -89,7 +89,7 @@ public void PlayOnMusicChannel(AudioClip clip) } /// - /// plays the audio clip once on the sound effects (sfx) source/channel + /// plays the audio clip once on the sound effects (sfx) source/channel /// /// audio clip to play public void PlayOnSFXChannel(AudioClip clip) @@ -98,7 +98,7 @@ public void PlayOnSFXChannel(AudioClip clip) } /// - /// function to get the current volume of the music source/channel + /// function to get the current volume of the music source/channel /// /// volume as float from 0.0 to 1.0 public float GetMusicVolume() @@ -107,7 +107,7 @@ public float GetMusicVolume() } /// - /// sets the volume of the music source/channel + /// sets the volume of the music source/channel /// /// float (0.0-1.0) to set the channel volume to public void SetMusicVolume(float volume) @@ -116,7 +116,7 @@ public void SetMusicVolume(float volume) } /// - /// 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 /// /// volume as float from 0.0 to 1.0 public float GetSfxVolume() @@ -125,7 +125,7 @@ public float GetSfxVolume() } /// - /// sets the volume of the sound effects (sfx) source/channel + /// sets the volume of the sound effects (sfx) source/channel /// /// float (0.0-1.0) to set the channel volume to public void SetSfxVolume(float volume) diff --git a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/CommonMenu.cs b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/CommonMenu.cs index e83e1e5..c226354 100644 --- a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/CommonMenu.cs +++ b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/CommonMenu.cs @@ -6,49 +6,64 @@ using System; using UnityEngine; -using UnityEngine.Serialization; using UnityEngine.UIElements; /// -/// common menu class for hover and click sound effects -/// on ui toolkit buttons. -/// override OnEnable() with the first call to base.OnEnable() or PostEnable(), -/// and set the variable GameManager.DisplayState associatedState to the respective menu state +/// common menu class for hover and click sound effects +/// on ui toolkit buttons. +/// override OnEnable() with the first call to base.OnEnable() or PostEnable(), +/// and set the variable GameManager.DisplayState associatedState to the respective menu state /// public class CommonMenu : MonoBehaviour { /// - /// 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 /// public GameManager.DisplayState associatedState = GameManager.DisplayState.UnassociatedState; /// - /// the visual element object for the menu - /// - public VisualElement UI; - - /// - /// manager for the game state - /// - protected GameManager Game; - - /// - /// manager for audio + /// manager for audio /// protected AudioManager Audio; /// - /// override this class but call base.OnEnable() first. - /// also set the associatedState variable to the respective menu state + /// manager for the game state + /// + protected GameManager Game; + + /// + /// the visual element object for the menu + /// + public VisualElement UI; + + /// + /// checks if The Menu (2022) was set up correctly + /// + /// throws an exception if UI, Game and Audio are not set + 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?)"); + } + + /// + /// override this class but call base.OnEnable() first. + /// also set the associatedState variable to the respective menu state /// public virtual void OnEnable() { // Debug.Log("CommonMenu: OnEnable"); PostEnable(); } - + /// - /// function to subscribe to mouse events and assign managers + /// function to subscribe to mouse events and assign managers /// public void PostEnable() { @@ -65,23 +80,7 @@ public void PostEnable() } /// - /// checks if The Menu (2022) was set up correctly - /// - /// throws an exception if UI, Game and Audio are not set - 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?)"); - } - - /// - /// function listener for PointerOverEvents and plays a hover sound if it's a button + /// function listener for PointerOverEvents and plays a hover sound if it's a button /// /// event from UIE callback public virtual void HoverListener(PointerOverEvent evt) @@ -93,7 +92,7 @@ public virtual void HoverListener(PointerOverEvent evt) } /// - /// function listener for ClickEvents and plays a click sound if it's a button + /// function listener for ClickEvents and plays a click sound if it's a button /// /// event from UIE callback public virtual void ClickListener(ClickEvent evt) @@ -105,7 +104,7 @@ public virtual void ClickListener(ClickEvent evt) } /// - /// generic decoupled function to play click sound + /// generic decoupled function to play click sound /// public virtual void PlayClick() { @@ -114,7 +113,7 @@ public virtual void PlayClick() } /// - /// generic decoupled function to play hover sound + /// generic decoupled function to play hover sound /// public virtual void PlayHover() { diff --git a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/CreditsMenu.cs b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/CreditsMenu.cs index 29273c8..21878f3 100644 --- a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/CreditsMenu.cs +++ b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/CreditsMenu.cs @@ -4,22 +4,21 @@ * description: credits menu script for handling credits menu button functions */ -using UnityEngine; using UnityEngine.UIElements; /// -/// class managing the credits menu and button function invocations +/// class managing the credits menu and button function invocations /// public class CreditsMenu : CommonMenu { /// - /// button to return to main menu + /// button to return to main menu /// public Button ButtonReturn; /// - /// 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 /// public override void OnEnable() { @@ -33,8 +32,8 @@ public override void OnEnable() } /// - /// handles return to main menu button press, - /// signals the game manager appropriately + /// handles return to main menu button press, + /// signals the game manager appropriately /// private void OptionReturnToMainMenu() { diff --git a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/GameManager.cs b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/GameManager.cs index 8a3086d..3b4230b 100644 --- a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/GameManager.cs +++ b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/GameManager.cs @@ -4,32 +4,46 @@ * description: game manager singleton for single source of truth state management */ -using System; using UnityEngine; -using UnityEngine.SceneManagement; /// -/// 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 /// public class GameManager : MonoBehaviour { /// - /// singleton pattern: define instance field for accessing the singleton elsewhere + /// enum for available menus in the game, for use with ShowMenu() + /// + public enum DisplayState + { + Game, + ScreenMainMenu, + ScreenOptionsMenu, + ScreenCreditsMenu, + ScreenPauseMenu, + ScreenCaughtPause, + ScreenEscapedMenu, + UnassociatedState + } + + /// + /// singleton pattern: define instance field for accessing the singleton elsewhere /// public static GameManager Instance; /// - /// the current state of the game + /// the current state of the game /// private DisplayState _state = DisplayState.UnassociatedState; /// - /// property to check if the game is paused based on the current DisplayState + /// property to check if the game is paused based on the current DisplayState /// + // TODO: remove this if not needed public bool Paused => _state != DisplayState.Game; /// - /// 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 /// private void Awake() { @@ -54,7 +68,7 @@ private void Awake() } /// - /// called when game starts, sets state to main menu + /// called when game starts, sets state to main menu /// private void Start() { @@ -62,40 +76,23 @@ private void Start() } /// - /// enum for available menus in the game, for use with ShowMenu() - /// - public enum DisplayState - { - Game, - ScreenMainMenu, - ScreenOptionsMenu, - ScreenCreditsMenu, - ScreenPauseMenu, - ScreenCaughtPause, - ScreenEscapedMenu, - UnassociatedState - } - - /// - /// helper function to hide any menu that is currently showing + /// helper function to hide any menu that is currently showing /// 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); } } /// - /// helper function for SetDisplayState() to pause the game, - /// called before the incoming game state is set + /// helper function for SetDisplayState() to pause the game, + /// called before the incoming game state is set /// /// the to-be-set state of the game 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().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().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); } } /// - /// helper function for SetDisplayState() to resume the game, - /// called before the incoming game state is set + /// helper function for SetDisplayState() to resume the game, + /// called before the incoming game state is set /// /// the to-be-set state of the game 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) } /// - /// 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 /// /// the game menu to show 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; } } /// - /// wrapper function to quit the game - /// in case of any cleanup needed + /// wrapper function to quit the game + /// in case of any cleanup needed /// public void Quit() { @@ -216,10 +219,13 @@ public void Quit() } /// - /// resets game state and starts a new game, will call SetDisplayState() + /// resets game state and starts a new game, will call SetDisplayState() /// public void NewGame() { + // set to game state + SetDisplayState(DisplayState.Game); + // TODO } } \ No newline at end of file diff --git a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/MainMenu.cs b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/MainMenu.cs index 7d5cb3d..6771d75 100644 --- a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/MainMenu.cs +++ b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/MainMenu.cs @@ -8,33 +8,33 @@ using UnityEngine.UIElements; /// -/// class managing the main menu and button function invocations +/// class managing the main menu and button function invocations /// public class MainMenu : CommonMenu { /// - /// button to play game - /// - public Button ButtonPlay; - - /// - /// button to show options menu - /// - public Button ButtonOptions; - - /// - /// button to show credits menu + /// button to show credits menu /// public Button ButtonCredits; - + /// - /// button to quit game + /// button to quit game /// public Button ButtonExit; /// - /// function to associate a display state with the menu, - /// and subscribe button events to their respective functions + /// button to show options menu + /// + public Button ButtonOptions; + + /// + /// button to play game + /// + public Button ButtonPlay; + + /// + /// function to associate a display state with the menu, + /// and subscribe button events to their respective functions /// public override void OnEnable() { @@ -63,18 +63,18 @@ public override void OnEnable() } /// - /// handles start button press, - /// signals the game manager appropriately + /// handles start button press, + /// signals the game manager appropriately /// private void OptionStartGame() { // start game - Game.SetDisplayState(GameManager.DisplayState.Game); + Game.NewGame(); } /// - /// handles credits button press, - /// signals the game manager appropriately + /// handles credits button press, + /// signals the game manager appropriately /// private void OptionShowCredits() { @@ -83,8 +83,8 @@ private void OptionShowCredits() } /// - /// handles options button press, - /// signals the game manager appropriately + /// handles options button press, + /// signals the game manager appropriately /// private void OptionShowOptions() { @@ -93,8 +93,8 @@ private void OptionShowOptions() } /// - /// handles quit button press, - /// signals the game manager appropriately + /// handles quit button press, + /// signals the game manager appropriately /// private void OptionQuitGame() { diff --git a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/OptionsMenu.cs b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/OptionsMenu.cs index 314654f..18b38ec 100644 --- a/SheKnowsWhatYouAreToHerGame/Assets/Scripts/OptionsMenu.cs +++ b/SheKnowsWhatYouAreToHerGame/Assets/Scripts/OptionsMenu.cs @@ -8,28 +8,28 @@ using UnityEngine.UIElements; /// -/// class managing the credits menu and button function invocations +/// class managing the credits menu and button function invocations /// public class OptionsMenu : CommonMenu { /// - /// button to return to main menu + /// button to return to main menu /// public Button ButtonReturn; /// - /// slider for music volume + /// slider for music volume /// public Slider SliderAudioMusic; /// - /// slider for sfx volume + /// slider for sfx volume /// public Slider SliderAudioSfx; /// - /// 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 /// public override void OnEnable() { @@ -58,8 +58,8 @@ public override void OnEnable() } /// - /// handles return to main menu button press, - /// signals the game manager appropriately + /// handles return to main menu button press, + /// signals the game manager appropriately /// private void OptionReturnToMainMenu() { @@ -68,8 +68,8 @@ private void OptionReturnToMainMenu() } /// - /// 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 /// /// private void OptionSetMusicVolume(ChangeEvent evt) @@ -80,8 +80,8 @@ private void OptionSetMusicVolume(ChangeEvent evt) } /// - /// 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 /// /// private void OptionSetSfxVolume(ChangeEvent evt)