game: stdise whats done in Awake, OnEnable and Start
- Awake -> singleton stuff or anything that needs to start really early - OnEnable -> init + register callbacks - Start -> do anything that might call a callback
This commit is contained in:
parent
534e96a150
commit
fe44ced9f8
|
@ -1,6 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Mail;
|
using System.Net.Mail;
|
||||||
using Firebase.Auth;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Assertions;
|
using UnityEngine.Assertions;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
|
@ -376,6 +375,8 @@ private void OnUsernameUpdateButtonClick()
|
||||||
_ => "An error occurred updating the username. Please try again."
|
_ => "An error occurred updating the username. Please try again."
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO: update lpdata
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -408,6 +409,8 @@ private void OnEmailUpdateButtonClick()
|
||||||
_ => "An error occurred updating the email. Please try again."
|
_ => "An error occurred updating the email. Please try again."
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO: update lpdata
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -57,6 +57,12 @@ public enum UserAccountDetailTargetEnum
|
||||||
Password
|
Password
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// callback functions to be invoked when the user signs in
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
private readonly List<Action<FirebaseConnectionStatus>> _onConnectionStatusChangedCallbacks = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// callback functions to be invoked when the user signs in
|
/// callback functions to be invoked when the user signs in
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -67,12 +73,6 @@ public enum UserAccountDetailTargetEnum
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly List<Action> _onSignOutCallbacks = new();
|
private readonly List<Action> _onSignOutCallbacks = new();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// callback functions to be invoked when the user signs in
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
private readonly List<Action<FirebaseConnectionStatus>> _onConnectionStatusChangedCallbacks = new ();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// the firebase authentication object
|
/// the firebase authentication object
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -88,16 +88,16 @@ public enum UserAccountDetailTargetEnum
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private FirebaseUser _user;
|
private FirebaseUser _user;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// whether the user is signed in
|
|
||||||
/// </summary>
|
|
||||||
public bool IsSignedIn;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// the current user's username, if authenticated
|
/// the current user's username, if authenticated
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private string _username;
|
private string _username;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// whether the user is signed in
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSignedIn;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// whether the backend is connected to the firebase backend
|
/// whether the backend is connected to the firebase backend
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -159,7 +159,6 @@ private void FireOnSignInCallbacks()
|
||||||
{
|
{
|
||||||
Debug.Log($"firing OnSignInCallbacks ({_onSignInCallbacks.Count})");
|
Debug.Log($"firing OnSignInCallbacks ({_onSignInCallbacks.Count})");
|
||||||
foreach (var callback in _onSignInCallbacks)
|
foreach (var callback in _onSignInCallbacks)
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
callback.Invoke(_user);
|
callback.Invoke(_user);
|
||||||
|
@ -168,7 +167,6 @@ private void FireOnSignInCallbacks()
|
||||||
{
|
{
|
||||||
Debug.LogError($"error invoking OnSignInCallback: {e.Message}");
|
Debug.LogError($"error invoking OnSignInCallback: {e.Message}");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -178,7 +176,6 @@ private void FireOnSignOutCallbacks()
|
||||||
{
|
{
|
||||||
Debug.Log($"firing OnSignOutCallbacks ({_onSignOutCallbacks.Count})");
|
Debug.Log($"firing OnSignOutCallbacks ({_onSignOutCallbacks.Count})");
|
||||||
foreach (var callback in _onSignOutCallbacks)
|
foreach (var callback in _onSignOutCallbacks)
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
callback.Invoke();
|
callback.Invoke();
|
||||||
|
@ -187,7 +184,6 @@ private void FireOnSignOutCallbacks()
|
||||||
{
|
{
|
||||||
Debug.LogError($"error invoking OnSignOutCallback: {e.Message}");
|
Debug.LogError($"error invoking OnSignOutCallback: {e.Message}");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -234,10 +230,7 @@ private void AuthStateChanged(object sender, EventArgs eventArgs)
|
||||||
if (!IsSignedIn) return;
|
if (!IsSignedIn) return;
|
||||||
|
|
||||||
Debug.Log($"signed in successfully as {_user?.UserId}");
|
Debug.Log($"signed in successfully as {_user?.UserId}");
|
||||||
RetrieveUsernameWithCallback((_, _) =>
|
RetrieveUsernameWithCallback((_, _) => { FireOnSignInCallbacks(); });
|
||||||
{
|
|
||||||
FireOnSignInCallbacks();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -482,11 +475,12 @@ public void ForgotPassword(string email, Action<bool> callback)
|
||||||
/// abstraction function to get the user's recent scores from the database
|
/// abstraction function to get the user's recent scores from the database
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="callback">
|
/// <param name="callback">
|
||||||
/// callback function that takes in a <c>DatabaseTransactionResult</c> enum and a <c>List<LocalPlayerData.Score></c>
|
/// callback function that takes in a <c>DatabaseTransactionResult</c> enum and a
|
||||||
|
/// <c>List<LocalPlayerData.Score></c>
|
||||||
/// </param>
|
/// </param>
|
||||||
public void GetRecentScores(Action<DatabaseTransactionResult, List<LocalPlayerData.Score>> callback)
|
public void GetRecentScores(Action<DatabaseTransactionResult, List<LocalPlayerData.Score>> callback)
|
||||||
{
|
{
|
||||||
// TODO
|
// TODO: implement this
|
||||||
callback(DatabaseTransactionResult.Error, new List<LocalPlayerData.Score>(0));
|
callback(DatabaseTransactionResult.Error, new List<LocalPlayerData.Score>(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -506,7 +500,10 @@ public void GetRecentScores(Action<DatabaseTransactionResult, List<LocalPlayerDa
|
||||||
/// abstraction function to get and calculate the user's rating from the database
|
/// abstraction function to get and calculate the user's rating from the database
|
||||||
/// calculation is done locally, call UpdateUserRating to update the user's rating in the database
|
/// calculation is done locally, call UpdateUserRating to update the user's rating in the database
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="callback">callback function that takes in a <c>DatabaseTransactionResult</c> enum and a user rating <c>float</c></param>
|
/// <param name="callback">
|
||||||
|
/// callback function that takes in a <c>DatabaseTransactionResult</c> enum and a user rating
|
||||||
|
/// <c>float</c>
|
||||||
|
/// </param>
|
||||||
public void CalculateUserRating(
|
public void CalculateUserRating(
|
||||||
Action<DatabaseTransactionResult, float> callback)
|
Action<DatabaseTransactionResult, float> callback)
|
||||||
{
|
{
|
||||||
|
|
|
@ -18,26 +18,26 @@ public class GameManager : MonoBehaviour
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public UIManager ui;
|
public UIManager ui;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// list of callbacks to call when the local player data changes
|
||||||
|
/// </summary>
|
||||||
|
private readonly List<Action<LocalPlayerData>> _onLocalPlayerDataChangeCallbacks = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// the local player data object for storing player data
|
/// the local player data object for storing player data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private LocalPlayerData _data;
|
private LocalPlayerData _data;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// read-only property for accessing the local player data outside of this class
|
|
||||||
/// </summary>
|
|
||||||
public LocalPlayerData Data => _data;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// list of callbacks to call when the local player data changes
|
|
||||||
/// </summary>
|
|
||||||
private readonly List<Action<LocalPlayerData>> _onLocalPlayerDataChangeCallbacks = new List<Action<LocalPlayerData>>();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// backend object for handling communication with the firebase backend
|
/// backend object for handling communication with the firebase backend
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Backend Backend;
|
public Backend Backend;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// read-only property for accessing the local player data outside of this class
|
||||||
|
/// </summary>
|
||||||
|
public LocalPlayerData Data => _data;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// enforces singleton behaviour; sets doesn't destroy on load and checks for multiple instances
|
/// enforces singleton behaviour; sets doesn't destroy on load and checks for multiple instances
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -59,7 +59,16 @@ private void Awake()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// called when the game object is enabled, initialises variables
|
/// start modifying state
|
||||||
|
/// </summary>
|
||||||
|
private void Start()
|
||||||
|
{
|
||||||
|
Debug.Log("GameManager starts here");
|
||||||
|
_data.LoadFromTheWorld(FireLocalPlayerDataChangeCallbacks);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// initialise variables and ui elements
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void OnEnable()
|
private void OnEnable()
|
||||||
{
|
{
|
||||||
|
@ -100,15 +109,6 @@ private void OnEnable()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// load in stuff
|
|
||||||
/// </summary>
|
|
||||||
private void Start()
|
|
||||||
{
|
|
||||||
Debug.Log("GameManager starts here");
|
|
||||||
_data.LoadFromTheWorld(FireLocalPlayerDataChangeCallbacks);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// called when the application is quitting, saves the local player data
|
/// called when the application is quitting, saves the local player data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -136,7 +136,6 @@ private void FireLocalPlayerDataChangeCallbacks(LocalPlayerData data)
|
||||||
{
|
{
|
||||||
Debug.Log($"firing LocalPlayerDataChangeCallbacks ({_onLocalPlayerDataChangeCallbacks.Count})");
|
Debug.Log($"firing LocalPlayerDataChangeCallbacks ({_onLocalPlayerDataChangeCallbacks.Count})");
|
||||||
foreach (var callback in _onLocalPlayerDataChangeCallbacks)
|
foreach (var callback in _onLocalPlayerDataChangeCallbacks)
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
callback.Invoke(data);
|
callback.Invoke(data);
|
||||||
|
@ -145,6 +144,5 @@ private void FireLocalPlayerDataChangeCallbacks(LocalPlayerData data)
|
||||||
{
|
{
|
||||||
Debug.LogError($"error invoking LocalPlayerDataChangeCallback: {e.Message}");
|
Debug.LogError($"error invoking LocalPlayerDataChangeCallback: {e.Message}");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -43,7 +43,7 @@ public class OklchColourPickerUI : MonoBehaviour
|
||||||
private VisualElement _responseColour;
|
private VisualElement _responseColour;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// function to set the initial values of the sliders
|
/// modify state of initialised variables
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
|
@ -53,7 +53,7 @@ private void Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// function to subscribe slider events to their respective functions
|
/// initialise the ui elements and register change event callbacks functions
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void OnEnable()
|
public void OnEnable()
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
using System;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
|
@ -13,20 +12,35 @@ public class SideViewUI : MonoBehaviour
|
||||||
private Button _accountButton;
|
private Button _accountButton;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// leaderboard button for showing the leaderboard
|
/// text label for showing the player's stable-ish chroma accuracy
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private Button _leaderboardButton;
|
private Label _chromaAccuracyText;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// play button for starting the game
|
|
||||||
/// </summary>
|
|
||||||
private Button _playButton;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// connection status label for showing the connection status
|
/// connection status label for showing the connection status
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private Label _connectionStatusLabel;
|
private Label _connectionStatusLabel;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// text label for showing the player's stable-ish hue accuracy
|
||||||
|
/// </summary>
|
||||||
|
private Label _hueAccuracyText;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// leaderboard button for showing the leaderboard
|
||||||
|
/// </summary>
|
||||||
|
private Button _leaderboardButton;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// text label for showing the player's stable-ish lightness accuracy
|
||||||
|
/// </summary>
|
||||||
|
private Label _lightnessAccuracyText;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// play button for starting the game
|
||||||
|
/// </summary>
|
||||||
|
private Button _playButton;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// text label for showing the player's known name
|
/// text label for showing the player's known name
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -38,22 +52,7 @@ public class SideViewUI : MonoBehaviour
|
||||||
private Label _ratingText;
|
private Label _ratingText;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// text label for showing the player's stable-ish lightness accuracy
|
/// initialise the ui elements and register the button click event functions
|
||||||
/// </summary>
|
|
||||||
private Label _lightnessAccuracyText;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// text label for showing the player's stable-ish chroma accuracy
|
|
||||||
/// </summary>
|
|
||||||
private Label _chromaAccuracyText;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// text label for showing the player's stable-ish hue accuracy
|
|
||||||
/// </summary>
|
|
||||||
private Label _hueAccuracyText;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// function to subscribe button events to their respective functions
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void OnEnable()
|
private void OnEnable()
|
||||||
{
|
{
|
||||||
|
|
|
@ -51,10 +51,20 @@ private void Awake()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// modify state of initial variables
|
||||||
|
/// </summary>
|
||||||
|
private void Start()
|
||||||
|
{
|
||||||
|
SetDisplayState(DisplayState.Nothing);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// initialise variables
|
||||||
|
/// </summary>
|
||||||
private void OnEnable()
|
private void OnEnable()
|
||||||
{
|
{
|
||||||
UI = GetComponent<UIDocument>().rootVisualElement;
|
UI = GetComponent<UIDocument>().rootVisualElement;
|
||||||
SetDisplayState(DisplayState.Nothing);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Reference in a new issue