From 30c82d77f6d1df1654608f3a71d0624e8a4e1c07 Mon Sep 17 00:00:00 2001 From: Mark Joshwel Date: Sun, 17 Nov 2024 22:31:03 +0800 Subject: [PATCH] game: working PopulateFields --- ColourMeOKGame/Assets/Scripts/AccountUI.cs | 19 +- ColourMeOKGame/Assets/Scripts/GameManager.cs | 42 ++-- .../Assets/Scripts/LocalPlayerData.cs | 7 +- ColourMeOKGame/Assets/Scripts/UIManager.cs | 16 +- ColourMeOKGame/Assets/UI/GameUI.uxml | 190 ++++++++++++------ 5 files changed, 175 insertions(+), 99 deletions(-) diff --git a/ColourMeOKGame/Assets/Scripts/AccountUI.cs b/ColourMeOKGame/Assets/Scripts/AccountUI.cs index 7e28a50..bf8cb65 100644 --- a/ColourMeOKGame/Assets/Scripts/AccountUI.cs +++ b/ColourMeOKGame/Assets/Scripts/AccountUI.cs @@ -78,6 +78,11 @@ public class AccountUI : MonoBehaviour /// private Button _usernameUpdateButton; + private void Awake() + { + GameManager.Instance.Backend.RegisterOnSignInCallback(OnSignInCallback); + } + public void Start() { if (state == State.UnassociatedState) throw new Exception("unreachable state"); @@ -118,11 +123,6 @@ public void OnEnable() TransitionStateTo(State.NotSignedIn); } - private void Awake() - { - GameManager.Instance.Backend.RegisterOnSignInCallback(OnSignInCallback); - } - private void OnSignInCallback(FirebaseUser user) { Debug.Log("sign in account ui callback"); @@ -134,15 +134,6 @@ private void OnSignInCallback(FirebaseUser user) _header.text = $"Signed in as {username}"; } - /// - /// populate the fields with the given username and email, used by GameManager after local player data is loaded - /// - public void PopulateFields(string username, string email) - { - _usernameField.value = username; - _emailField.value = email; - } - private void TransitionStateTo(State newState, bool keepAccompanyingText = false) { // if we're transitioning to the same state, do nothing diff --git a/ColourMeOKGame/Assets/Scripts/GameManager.cs b/ColourMeOKGame/Assets/Scripts/GameManager.cs index c58f431..56be8e9 100644 --- a/ColourMeOKGame/Assets/Scripts/GameManager.cs +++ b/ColourMeOKGame/Assets/Scripts/GameManager.cs @@ -1,4 +1,5 @@ using UnityEngine; +using UnityEngine.UIElements; /// /// singleton for a single source of truth game state and flow management @@ -10,6 +11,11 @@ public class GameManager : MonoBehaviour /// public static GameManager Instance; + /// + /// ui manager object for handling ui state and flow + /// + public UIManager ui; + /// /// the local player data object for storing player data /// @@ -20,11 +26,6 @@ public class GameManager : MonoBehaviour /// public Backend Backend; - /// - /// ui manager object for handling ui state and flow - /// - public UIManager ui; - /// /// enforces singleton behaviour; sets doesn't destroy on load and checks for multiple instances /// @@ -43,7 +44,17 @@ private void Awake() Debug.Log("awake as non-singleton instance, destroying self"); Destroy(gameObject); } + } + private void Start() + { + // load the local player data and refresh the ui + _localPlayerData = new LocalPlayerData(); + _localPlayerData.LoadFromTheWorld(); + PopulateFields(); + + // register a callback to refresh the ui when the player signs in + Backend.RegisterOnSignInCallback(_ => { _localPlayerData.LoadFromTheWorld(); }); } /// @@ -56,21 +67,22 @@ private void OnEnable() ui = UIManager.Instance; } - private void Start() + /// + /// called when the application is quitting, saves the local player data + /// + private void OnApplicationQuit() { - // 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(); }); + Debug.Log("running deferred cleanup/save functions"); + Backend.Cleanup(); + _localPlayerData.SaveToTheWorld(); } /// - /// called when the game object is disabled + /// populate the fields with the given username and email, used by GameManager after local player data is loaded /// - private void OnDestroy() + public void PopulateFields() { - Backend.Cleanup(); + ui.UI.Q("UsernameField").value = _localPlayerData.LastKnownUsername; + ui.UI.Q("EmailField").value = _localPlayerData.LastKnownEmail; } } \ No newline at end of file diff --git a/ColourMeOKGame/Assets/Scripts/LocalPlayerData.cs b/ColourMeOKGame/Assets/Scripts/LocalPlayerData.cs index 7d44cca..f0ca908 100644 --- a/ColourMeOKGame/Assets/Scripts/LocalPlayerData.cs +++ b/ColourMeOKGame/Assets/Scripts/LocalPlayerData.cs @@ -90,7 +90,8 @@ public void LoadFromTheWorld() RecentOnlineScores.Enqueue(onlineScore); } - Debug.Log("loaded online scores from backend"); + Debug.Log( + $"loaded lpdata from the world ({LastKnownUsername} <{LastKnownEmail}> with RLS.Count={RecentLocalScores.Count}), ROS.Count={RecentOnlineScores.Count}"); }); } @@ -114,8 +115,7 @@ public void SaveToTheWorld() idx++; } - Debug.Log("saved lpdata to playerprefs"); - // online scores are already saved in the backend + Debug.Log("saved lpdata to playerprefs"); // online scores are already saved in the backend } /// @@ -128,7 +128,6 @@ public void RegisterLocalScore(Score score) RecentLocalScores.Enqueue(score); } - public struct Score { /// diff --git a/ColourMeOKGame/Assets/Scripts/UIManager.cs b/ColourMeOKGame/Assets/Scripts/UIManager.cs index a2e4d2b..3296492 100644 --- a/ColourMeOKGame/Assets/Scripts/UIManager.cs +++ b/ColourMeOKGame/Assets/Scripts/UIManager.cs @@ -4,11 +4,6 @@ public class UIManager : MonoBehaviour { - /// - /// singleton pattern: define instance field for accessing the singleton elsewhere - /// - public static UIManager Instance; - /// /// enum for available menus in the game, for use with ShowMenu() /// @@ -20,12 +15,17 @@ public enum DisplayState LeaderboardView, AccountView } - + + /// + /// singleton pattern: define instance field for accessing the singleton elsewhere + /// + public static UIManager Instance; + /// /// the current display state of the game /// [SerializeField] private DisplayState state = DisplayState.UnassociatedState; - + /// /// the visual element object for game ui /// @@ -56,7 +56,7 @@ private void OnEnable() UI = GetComponent().rootVisualElement; SetDisplayState(DisplayState.Nothing); } - + /// /// function to show a menu based on the enum passed, /// and any other necessary actions diff --git a/ColourMeOKGame/Assets/UI/GameUI.uxml b/ColourMeOKGame/Assets/UI/GameUI.uxml index 7245dbc..2ca73b7 100644 --- a/ColourMeOKGame/Assets/UI/GameUI.uxml +++ b/ColourMeOKGame/Assets/UI/GameUI.uxml @@ -1,92 +1,166 @@ - -