using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UIElements;
///
/// singleton for a single source of truth game state and flow management
///
public class GameManager : MonoBehaviour
{
///
/// enum for available menus in the game, for use with ShowMenu()
///
public enum DisplayState
{
Nothing,
PlayView,
LeaderboardView,
AccountView,
}
///
/// singleton pattern: define instance field for accessing the singleton elsewhere
///
public static GameManager Instance;
///
/// the current display state of the game
///
[SerializeField] private DisplayState state = DisplayState.Nothing;
///
/// the visual element object for game ui (hud/prompts/tooltips)
///
private VisualElement _ui;
///
/// backend object for handling communication with the firebase backend
///
public Backend Backend;
///
/// the local player data object for storing player data
///
private LocalPlayerData _localPlayerData;
///
/// enforces singleton behaviour; sets doesn't destroy on load and checks for multiple instances
///
private void Awake()
{
// check if instance hasn't been set yet
if (Instance == null)
{
Debug.Log("awake as singleton instance, setting self as the forever-alive instance");
Instance = this;
DontDestroyOnLoad(gameObject);
}
// check if instance is already set and it's not this instance
else if (Instance != null && Instance != this)
{
Debug.Log("awake as non-singleton instance, destroying self");
Destroy(gameObject);
}
}
private void Start()
{
SetDisplayState(DisplayState.PlayView);
// load the local player data and refresh the ui
_localPlayerData = new LocalPlayerData();
_localPlayerData.LoadFromTheWorld();
// register a callback to refresh the ui when the player signs in
Backend.RegisterOnSignInCallback(_ =>
{
_localPlayerData.LoadFromTheWorld();
});
}
///
/// called when the game object is enabled
///
private void OnEnable()
{
Backend = new Backend();
Backend.Initialise();
}
///
/// called when the game object is disabled
///
private void OnDestroy()
{
Backend.Cleanup();
}
///
/// function to show a menu based on the enum passed,
/// and any other necessary actions
///
/// the game menu to show
public void SetDisplayState(DisplayState newDisplayState)
{
var currentDisplayState = St;
switch (newDisplayState)
}
}