game(scripts): add old scripts from everellia

This commit is contained in:
Mark Joshwel 2024-08-10 13:13:40 +08:00
parent e74332781d
commit db13087900
8 changed files with 601 additions and 0 deletions

View file

@ -0,0 +1,139 @@
/*
* author: mark joshwel
* date: 29/5/2024
* description: audio manager for handling audio in the game
*/
using System;
using UnityEngine;
/// <summary>
/// singleton class for handling audio in the game
/// </summary>
public class AudioManager : MonoBehaviour
{
/// <summary>
/// singleton pattern: define instance field for accessing the singleton elsewhere
/// </summary>
public static AudioManager Instance;
/// <summary>
/// music audio source
/// </summary>
[SerializeField] private AudioSource musicSource;
/// <summary>
/// music source default volume
/// </summary>
[SerializeField] private float musicSourceDefaultVolume = 0.6f;
/// <summary>
/// sound effects (sfx) audio source
/// </summary>
[SerializeField] private AudioSource sfxSource;
/// <summary>
/// sound effects (sfx) source default volume
/// </summary>
[SerializeField] private float sfxSourceDefaultVolume = 0.6f;
/// <summary>
/// audio clip for menu button click
/// </summary>
[SerializeField] public AudioClip menuButtonClick;
/// <summary>
/// audio clip for menu button hover
/// </summary>
[SerializeField] public AudioClip menuButtonHover;
/// <summary>
/// function to set doesn't destroy on load and checks 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 a 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()
{
// set the default volume for the music source
musicSource.volume = musicSourceDefaultVolume;
// set the default volume for the sfx source
sfxSource.volume = sfxSourceDefaultVolume;
}
/// <summary>
/// plays the audio clip once on the music source/channel
/// </summary>
/// <param name="clip">audio clip to play</param>
public void PlayOnMusicChannel(AudioClip clip)
{
musicSource.PlayOneShot(clip);
}
/// <summary>
/// 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)
{
sfxSource.PlayOneShot(clip);
}
/// <summary>
/// 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()
{
return musicSource.volume;
}
/// <summary>
/// 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)
{
musicSource.volume = Math.Min(volume, 1.0f);
}
/// <summary>
/// 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()
{
return sfxSource.volume;
}
/// <summary>
/// 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)
{
sfxSource.volume = Math.Min(volume, 1.0f);
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 723a7675db014cb3aca02f49d069e931
timeCreated: 1723266436

View file

@ -0,0 +1,119 @@
/*
* author: mark joshwel
* date: 29/5/2024
* description: common menu script for hover and click sound effects on ui toolkit buttons
*/
using System;
using UnityEngine;
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
/// </summary>
public class CommonMenu : MonoBehaviour
{
/// <summary>
/// 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>
/// manager for audio
/// </summary>
protected AudioManager Audio;
/// <summary>
/// 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()
{
PostEnable();
}
/// <summary>
/// function to subscribe to mouse events and assign managers
/// </summary>
public void PostEnable()
{
// get audio manager singleton instance from the world
UI = GetComponent<UIDocument>().rootVisualElement;
Audio = AudioManager.Instance;
Game = GameManager.Instance;
// subscribe to hover events
UI.RegisterCallback<PointerOverEvent>(HoverListener);
}
/// <summary>
/// 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)
{
// check for button
if (evt.target is Button)
// play hover sound
PlayHover();
}
/// <summary>
/// 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)
{
// check for button
if (evt.target is Button)
// play click sound
PlayClick();
}
/// <summary>
/// generic decoupled function to play click sound
/// </summary>
public virtual void PlayClick()
{
// play click sound
Audio.PlayOnSFXChannel(Audio.menuButtonClick);
}
/// <summary>
/// generic decoupled function to play hover sound
/// </summary>
public virtual void PlayHover()
{
// play hover sound
Audio.PlayOnSFXChannel(Audio.menuButtonHover);
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 41be3570aaa04d079869df1b9a2b91b8
timeCreated: 1723266536

View file

@ -0,0 +1,296 @@
/*
* author: mark joshwel
* date: 29/5/2024
* description: game manager singleton for a single source of truth state management
*/
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,
ScreenCaughtMenu,
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
/// </summary>
private DisplayState _state = DisplayState.UnassociatedState;
/// <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>
// /// the current scene of the game, used for scene switching and state transitions
// /// </summary>
// private Scene _currentScene;
//
// /// <summary>
// /// the previous scene of the game, used for scene switching and state transitions
// /// </summary>
// private Scene _previousScene;
/// <summary>
/// function to set doesn't destroy on load and checks 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 a scene load
DontDestroyOnLoad(gameObject);
Debug.Log("GameManager: Awake as singleton instance");
}
// check if instance is already set and it's not this instance
else if (Instance != null && Instance != this)
{
Debug.Log("GameManager: Awake as non-singleton instance, destroying self");
// destroy the new instance if it's not the singleton instance
Destroy(gameObject);
}
}
/// <summary>
/// called when the game starts, sets state to the main menu
/// </summary>
// /// <exception cref="Exception">generic exception it couldn't verify a safe state when starting the game</exception>
private void Start()
{
SetDisplayState(DisplayState.ScreenMainMenu);
// _currentScene = SceneManager.GetSceneByName("S2 World");
// _previousScene = _currentScene;
// if (_currentScene == null)
// {
// throw new Exception("GameManager.Start: current scene is null");
// }
}
/// <summary>
/// helper function to hide any menu that is currently showing
/// </summary>
private void HideMenuHelper()
{
// get all child menus in the "Menus" parent object
foreach (var menu in GameObject.FindGameObjectsWithTag("Menus"))
foreach (Transform menuChild in menu.transform)
{
// disable the menu if it's currently active
if (!menuChild.gameObject.activeSelf) continue;
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
/// </summary>
/// <param name="incomingState">the to-be-set state of the game</param>
private void PauseGameHelper(DisplayState incomingState)
{
// if we're transitioning from a state of gameplay to a state of non-gameplay,
// then we should pause the game
if (_state == DisplayState.Game && incomingState != DisplayState.Game)
{
Debug.Log("GameManager.PauseGameHelper: actually pausing game");
Time.timeScale = 0f;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
// or if the incoming state is the main menu, we should probably free the cursor
else if (incomingState == DisplayState.ScreenMainMenu)
{
Debug.Log("GameManager.PauseGameHelper: freeing cursor for main menu");
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
// if the current and incoming states are the same, then we shouldn't do anything,
// so we return early
if (_state == incomingState)
{
Debug.Log("GameManager.PauseGameHelper: states are the same, returning early");
return;
}
// hide any menu that is currently showing
HideMenuHelper();
// get all child menus in the "Menus" parent object
foreach (var menuParent in GameObject.FindGameObjectsWithTag("Menus"))
foreach (Transform menu in menuParent.transform)
{
// show the menu based on the incoming state
// get the associated state of the menu
var possibleMenuObject = menu.gameObject.GetComponent<CommonMenu>();
if (possibleMenuObject == null) continue;
// guard clause if the menu isn't what we're looking for
if (possibleMenuObject.associatedState != incomingState)
continue;
// if the associated state is the same as the incoming state, then show the menu
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
/// </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)
{
Debug.Log(
"GameManager.ResumeGameHelper: returning prematurely as" +
$" _state={_state} and incomingState={incomingState}");
return;
}
// else, we should resume the game
Time.timeScale = 1f;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
Debug.Log("GameManager.ResumeGameHelper: resuming game");
// hide any menu that is currently showing
HideMenuHelper();
}
/// <summary>
/// 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)
{
// boolean check if we're transitioning to the same state
var transitioning = _state == displayState;
// check if the game is paused or not
if (displayState is DisplayState.Game or DisplayState.UnassociatedState)
{
Debug.Log($"GameManager.SetDisplayState({displayState}): pre-resume helper");
ResumeGameHelper(displayState);
}
else
{
Debug.Log($"GameManager.SetDisplayState({displayState}): pre-pause helper");
// if we're transitioning to the main menu, load the main menu scene
if (transitioning && displayState == DisplayState.ScreenMainMenu)
SceneManager.LoadScene("S2 World");
PauseGameHelper(displayState);
}
Debug.Log($"GameManager.SetDisplayState({displayState}): post-pause/resume helper");
// set the state of the game to the incoming state
_state = displayState;
Debug.Log($"GameManager.SetDisplayState({displayState}): state is now {displayState}");
// if we're transitioning into gameplay or into the main menu,
// we'll need a post-step to enable the correct camera
if (displayState is not (DisplayState.Game or DisplayState.ScreenMainMenu))
return;
// get all MainCamera-tagged objects and disable them
foreach (var mainCameraObject in GameObject.FindGameObjectsWithTag("MainCamera"))
{
// find the camera component
var potentialCamera = mainCameraObject.GetComponent<Camera>();
// if the object doesn't have a camera component, skip it
if (potentialCamera == null) continue;
// disable the camera
potentialCamera.enabled = false;
}
// declare a target camera to enable
GameObject targetCameraObject;
// switch on the state to enable the correct camera
// could be an if statement, but unity optimizes switch statements anyway
switch (displayState)
{
// if we're transitioning to the main menu state,
// change the camera to the main menu camera under the "Menus"-tagged parent object
case DisplayState.ScreenMainMenu:
Debug.Log("GameManager.SetDisplayState: targeting 'Menu Camera' camera");
targetCameraObject = GameObject.Find("Menu Camera");
break;
// else, we should enable the players' main camera
default:
Debug.Log("GameManager.SetDisplayState: targeting 'MainCamera' camera");
targetCameraObject = GameObject.Find("MainCamera");
break;
}
// find the camera component
if (targetCameraObject == null) return;
var targetCamera = targetCameraObject.GetComponent<Camera>();
if (targetCamera == null) return;
// enable the target camera
Debug.Log("GameManager.SetDisplayState: enabling target camera");
targetCamera.enabled = true;
}
/// <summary>
/// wrapper function to quit the game
/// in case of any cleanup needed
/// </summary>
public void Quit()
{
// quit the application
Application.Quit();
}
/// <summary>
/// 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

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9f6e55d96ef14d65979aabb99a0d5f1a
timeCreated: 1723266512

View file

@ -0,0 +1,35 @@
/*
* author: mark joshwel
* date: 27/5/2024
* description: script for handling player interactivity
*/
using UnityEngine;
/// <summary>
/// class for handling player interactivity
/// </summary>
public class MarkPlayer : MonoBehaviour
{
/// <summary>
/// game manager instance
/// </summary>
private GameManager _game;
/// <summary>
/// initialisation function
/// </summary>
private void Start()
{
_game = GameManager.Instance;
}
/// <summary>
/// function called by the input system when escape is paused
/// </summary>
public void OnPause()
{
Debug.Log("escape pressed");
_game.SetDisplayState(_game.Paused ? GameManager.DisplayState.Game : GameManager.DisplayState.ScreenPauseMenu);
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 995d26dc842b43269f0cfd51fe4e32ef
timeCreated: 1723266626