2024-11-15 07:54:43 +08:00
|
|
|
using UnityEngine;
|
2024-11-17 22:31:03 +08:00
|
|
|
using UnityEngine.UIElements;
|
2024-11-15 07:54:43 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// singleton for a single source of truth game state and flow management
|
|
|
|
/// </summary>
|
|
|
|
public class GameManager : MonoBehaviour
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// singleton pattern: define instance field for accessing the singleton elsewhere
|
|
|
|
/// </summary>
|
|
|
|
public static GameManager Instance;
|
|
|
|
|
2024-11-17 22:31:03 +08:00
|
|
|
/// <summary>
|
|
|
|
/// ui manager object for handling ui state and flow
|
|
|
|
/// </summary>
|
|
|
|
public UIManager ui;
|
|
|
|
|
2024-11-17 07:29:22 +08:00
|
|
|
/// <summary>
|
2024-11-17 22:05:04 +08:00
|
|
|
/// the local player data object for storing player data
|
2024-11-17 21:32:14 +08:00
|
|
|
/// </summary>
|
2024-11-17 22:05:04 +08:00
|
|
|
private LocalPlayerData _localPlayerData;
|
2024-11-17 21:32:14 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// backend object for handling communication with the firebase backend
|
|
|
|
/// </summary>
|
|
|
|
public Backend Backend;
|
2024-11-17 22:05:04 +08:00
|
|
|
|
2024-11-15 07:54:43 +08:00
|
|
|
/// <summary>
|
|
|
|
/// enforces singleton behaviour; sets 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)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2024-11-17 22:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
// load the local player data and refresh the ui
|
|
|
|
_localPlayerData = new LocalPlayerData();
|
|
|
|
_localPlayerData.LoadFromTheWorld();
|
|
|
|
PopulateFields();
|
2024-11-17 07:29:22 +08:00
|
|
|
|
2024-11-17 22:31:03 +08:00
|
|
|
// register a callback to refresh the ui when the player signs in
|
|
|
|
Backend.RegisterOnSignInCallback(_ => { _localPlayerData.LoadFromTheWorld(); });
|
2024-11-17 19:10:01 +08:00
|
|
|
}
|
|
|
|
|
2024-11-17 07:29:22 +08:00
|
|
|
/// <summary>
|
2024-11-17 22:05:04 +08:00
|
|
|
/// called when the game object is enabled, initialises variables
|
2024-11-17 07:29:22 +08:00
|
|
|
/// </summary>
|
2024-11-17 21:32:26 +08:00
|
|
|
private void OnEnable()
|
2024-11-17 07:29:22 +08:00
|
|
|
{
|
2024-11-17 21:32:26 +08:00
|
|
|
Backend = new Backend();
|
|
|
|
Backend.Initialise();
|
2024-11-17 22:05:04 +08:00
|
|
|
ui = UIManager.Instance;
|
2024-11-17 07:29:22 +08:00
|
|
|
}
|
|
|
|
|
2024-11-17 22:31:03 +08:00
|
|
|
/// <summary>
|
|
|
|
/// called when the application is quitting, saves the local player data
|
|
|
|
/// </summary>
|
|
|
|
private void OnApplicationQuit()
|
2024-11-17 07:29:22 +08:00
|
|
|
{
|
2024-11-17 22:31:03 +08:00
|
|
|
Debug.Log("running deferred cleanup/save functions");
|
|
|
|
Backend.Cleanup();
|
|
|
|
_localPlayerData.SaveToTheWorld();
|
2024-11-17 07:29:22 +08:00
|
|
|
}
|
2024-11-17 19:10:01 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2024-11-17 22:31:03 +08:00
|
|
|
/// populate the fields with the given username and email, used by GameManager after local player data is loaded
|
2024-11-17 19:10:01 +08:00
|
|
|
/// </summary>
|
2024-11-17 22:31:03 +08:00
|
|
|
public void PopulateFields()
|
2024-11-17 19:10:01 +08:00
|
|
|
{
|
2024-11-17 22:31:03 +08:00
|
|
|
ui.UI.Q<TextField>("UsernameField").value = _localPlayerData.LastKnownUsername;
|
|
|
|
ui.UI.Q<TextField>("EmailField").value = _localPlayerData.LastKnownEmail;
|
2024-11-17 19:10:01 +08:00
|
|
|
}
|
2024-11-15 07:54:43 +08:00
|
|
|
}
|