From 23da2fd76d41a75ac4b5c0963ac8407c615c868d Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 17 Feb 2025 12:12:01 +0800 Subject: [PATCH] game: clean up + comments --- SSLR/Assets/Scripts/Backend.cs | 81 ++++++++++++++++++++++-- SSLR/Assets/Scripts/Bin.cs | 4 +- SSLR/Assets/Scripts/DayManager.cs | 15 ++--- SSLR/Assets/Scripts/Despawn.cs | 36 ----------- SSLR/Assets/Scripts/Despawn.cs.meta | 11 ---- SSLR/Assets/Scripts/NPCMovementRework.cs | 2 +- SSLR/Assets/Scripts/NpcManager.cs | 62 +++++++++++------- SSLR/Assets/Scripts/ShiftManager.cs | 21 +++--- 8 files changed, 135 insertions(+), 97 deletions(-) delete mode 100644 SSLR/Assets/Scripts/Despawn.cs delete mode 100644 SSLR/Assets/Scripts/Despawn.cs.meta diff --git a/SSLR/Assets/Scripts/Backend.cs b/SSLR/Assets/Scripts/Backend.cs index c43fab3..793b74a 100644 --- a/SSLR/Assets/Scripts/Backend.cs +++ b/SSLR/Assets/Scripts/Backend.cs @@ -1,3 +1,10 @@ +/* + * Author:Lin Hengrui Ryan, Livinia Poo + * Date: 20/1/25 + * Description: + * backend to talk to supabase and firebase + */ + using System.Threading.Tasks; using UnityEngine; using Supabase; @@ -11,16 +18,39 @@ using UnityEngine.UI; public class Backend : MonoBehaviour { + /// + /// singleton instance + /// public static Backend instance; + /// + /// url to supabase + /// [SerializeField] private string url; + + /// + /// supabase api anon key + /// [SerializeField] private string anonKey; + + /// + /// supabase client + /// public Client Client; + /// + /// supabase auth session + /// public Session Session; + + /// + /// user data + /// public Users User; - public Image profilePicture; + /// + /// setting up supabase client + /// private async void Start() { var options = new SupabaseOptions @@ -32,6 +62,7 @@ public class Backend : MonoBehaviour Client = new Supabase.Client(url, anonKey, options); await Client.InitializeAsync().ContinueWith(task => { + // Check if the task is successful if (!task.IsCompletedSuccessfully) { Debug.LogError(task.Exception); @@ -41,9 +72,11 @@ public class Backend : MonoBehaviour Debug.Log("Supabase Initialized"); } }); - } + /// + /// signing out of supabase auth + /// public async void SignOut() { User = null; @@ -51,6 +84,16 @@ public class Backend : MonoBehaviour await Client.Auth.SignOut(); } + /// + /// sending user data to supabase + /// + /// the user id of the current player + /// the url for the profile picture + /// total score + /// name of this player + /// the number of days that this player has gone through + /// + /// public async void SendData(string uid, string profilePictureUrl, int score, string displayName, int daysPlayed, int customersHelpedCorrectly, int customersHelpedWrongly) @@ -65,6 +108,7 @@ public class Backend : MonoBehaviour customersHelpedCorrectly = customersHelpedCorrectly, customersHelpedWrongly = customersHelpedWrongly, }; + // Send the data to the database await Client.From().OnConflict(x => x.uid) .Upsert(user).ContinueWith(SendTask => { @@ -79,7 +123,11 @@ public class Backend : MonoBehaviour }); } - + /// + /// sign in to supabase auth + /// + /// + /// public async void SignIn(string email, string password) { Session = await Client.Auth.SignIn(email, password); @@ -87,8 +135,13 @@ public class Backend : MonoBehaviour GetData(Session.User.Id); } + /// + /// gettting user data from supabase + /// + /// the uid for the player from auth public async void GetData(string uid) { + // Get the data from the database var result = await Client.From().Where(x => x.uid == uid).Get(); User = result.Model; @@ -113,10 +166,15 @@ public class Backend : MonoBehaviour } } + /// + /// getting the npc data from firebase + /// + /// the npc script for desired npc public void FirebaseGet(NpcMovementRework target) { NpcData data = new NpcData(); - FirebaseDatabase.DefaultInstance.RootReference.Child("scenarios").Child(UnityEngine.Random.Range(1, 8).ToString()) + FirebaseDatabase.DefaultInstance.RootReference.Child("scenarios") + .Child(UnityEngine.Random.Range(1, 8).ToString()) .GetValueAsync() .ContinueWithOnMainThread(task => { @@ -151,6 +209,11 @@ public class Backend : MonoBehaviour }); } + /// + /// getting the profile picture from the url + /// + /// + /// the targeted ui image public async void GetProfile(string url, Image targetRenderer) { if (string.IsNullOrEmpty(url)) @@ -158,7 +221,7 @@ public class Backend : MonoBehaviour Debug.LogError("Profile picture URL is empty"); return; } - + try { Texture2D texture = await GetTextureFromURL(url); @@ -187,6 +250,11 @@ public class Backend : MonoBehaviour } } + /// + /// getting the texture from the url + /// + /// + /// private async Task GetTextureFromURL(string url) { using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url)) @@ -210,6 +278,9 @@ public class Backend : MonoBehaviour } } + /// + /// assigning the singleton instance + /// private void Awake() { if (instance == null) diff --git a/SSLR/Assets/Scripts/Bin.cs b/SSLR/Assets/Scripts/Bin.cs index b5937f5..9f59fe9 100644 --- a/SSLR/Assets/Scripts/Bin.cs +++ b/SSLR/Assets/Scripts/Bin.cs @@ -1,7 +1,7 @@ /* * Author: Livinia Poo * Date: 12/2/25 - * Description: + * Description: * Bin */ @@ -23,4 +23,4 @@ public class Bin : MonoBehaviour Debug.Log("you can't throw that!"); } } -} +} \ No newline at end of file diff --git a/SSLR/Assets/Scripts/DayManager.cs b/SSLR/Assets/Scripts/DayManager.cs index 15f32ee..da2c35a 100644 --- a/SSLR/Assets/Scripts/DayManager.cs +++ b/SSLR/Assets/Scripts/DayManager.cs @@ -1,7 +1,7 @@ /* * Author: Livinia Poo * Date: 4/2/25 - * Description: + * Description: * Managing start and end days */ @@ -27,7 +27,7 @@ public class DayManager : MonoBehaviour endDayTrigger.enabled = false; } - + void Update() { if (doneAShift && !endDayTrigger.enabled) @@ -39,19 +39,18 @@ public class DayManager : MonoBehaviour endDayTrigger.enabled = false; } } - + void OnTriggerEnter(Collider other) { if (other.CompareTag("Player")) - { - Debug.Log("Day completed!"); + { + Debug.Log("Day completed!"); Player.daysPlayed += 1; Debug.Log(Player.daysPlayed); shiftManagerScript.AllowShiftStart(); Debug.Log("You can start another shift!"); - + doneAShift = false; } } - -} +} \ No newline at end of file diff --git a/SSLR/Assets/Scripts/Despawn.cs b/SSLR/Assets/Scripts/Despawn.cs deleted file mode 100644 index 7de2c71..0000000 --- a/SSLR/Assets/Scripts/Despawn.cs +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Author: Livinia Poo - * Date: 22/1/25 - * Description: - * Customer despawns in certain area - */ - -using System; -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Despawn : MonoBehaviour -{ - private Player playerScript; - /* - NPCSpawn npcSpawnScript; - */ - - /// - /// Destroy NPC object on trigger enter - /// - /// - public void OnTriggerEnter(Collider other) - { - if (other.CompareTag("NPC")) - { - GameManager.instance.currentNPC = null; - /* - NPCSpawn.instance.npcSpawned = false; - */ - - Destroy(other.gameObject); - } - } -} diff --git a/SSLR/Assets/Scripts/Despawn.cs.meta b/SSLR/Assets/Scripts/Despawn.cs.meta deleted file mode 100644 index 23f8554..0000000 --- a/SSLR/Assets/Scripts/Despawn.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: a9d335c7da7e4ea49aac3c7a08b135d2 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/SSLR/Assets/Scripts/NPCMovementRework.cs b/SSLR/Assets/Scripts/NPCMovementRework.cs index 465a214..8a63a09 100644 --- a/SSLR/Assets/Scripts/NPCMovementRework.cs +++ b/SSLR/Assets/Scripts/NPCMovementRework.cs @@ -1,5 +1,5 @@ /* - * Author: Lin Hengrui Ryan + * Author: Lin Hengrui Ryan, Livinia Poo * Date: 3/2/25 * Description: * Customer walking handling using NavMesh diff --git a/SSLR/Assets/Scripts/NpcManager.cs b/SSLR/Assets/Scripts/NpcManager.cs index ec5d3f8..8cae3a1 100644 --- a/SSLR/Assets/Scripts/NpcManager.cs +++ b/SSLR/Assets/Scripts/NpcManager.cs @@ -1,5 +1,5 @@ /* - * Author: Lin Hengrui Ryan and Livinia Poo + * Author: Lin Hengrui Ryan, Livinia Poo * Date: 1/2/25 * Description: * Npc Manager @@ -9,7 +9,6 @@ using System; using UnityEngine; using System.Collections.Generic; using System.Collections; - using UnityEngine.Serialization; public class NpcManager : MonoBehaviour @@ -18,7 +17,7 @@ public class NpcManager : MonoBehaviour /// Assign Npc Manager instance /// public static NpcManager instance; - + /// /// Assign GameManager script /// @@ -43,24 +42,20 @@ public class NpcManager : MonoBehaviour /// list of all the spawn points /// public Transform[] spawnPoints; - + /// /// float for time between npc spawns /// [SerializeField] private float npcBufferTime; - + public Seat[] Seats; - - /// - /// a bool to check if the player is free - /// - public bool playerFree = false; + /// /// collection of all exiting npcs /// - public List currentNpcs; - + public List currentNpcs; + /// /// a collection of positions for the npcs to despawn /// @@ -70,12 +65,15 @@ public class NpcManager : MonoBehaviour /// flag to prevent multiple coroutines /// private bool isSpawning = false; - + /// /// the position of the desk /// public Transform desk; - + + /// + /// singleton pattern + /// void Awake() { if (instance == null) @@ -87,10 +85,13 @@ public class NpcManager : MonoBehaviour { Destroy(gameObject); } - + gm = GameObject.Find("Game Manager").GetComponent(); } - + + /// + /// called once per frame to check if the shift has started and if there are less than 4 npcs + /// private void Update() { if (gm.shiftStarted) @@ -106,9 +107,12 @@ public class NpcManager : MonoBehaviour } } + /// + /// to spawn a new npc + /// public void SpawnNPC() { - var randomNpc=0; + var randomNpc = 0; bool isFemale = UnityEngine.Random.value > 0.5f; var spawnPoint = spawnPoints[UnityEngine.Random.Range(0, spawnPoints.Length)]; if (isFemale) @@ -119,23 +123,32 @@ public class NpcManager : MonoBehaviour { randomNpc = UnityEngine.Random.Range(0, maleNpcs.Length); } - var npc = Instantiate(isFemale ? femaleNpcs[randomNpc] : maleNpcs[randomNpc], spawnPoint.position, Quaternion.identity); + + var npc = Instantiate(isFemale ? femaleNpcs[randomNpc] : maleNpcs[randomNpc], spawnPoint.position, + Quaternion.identity); currentNpcs.Add(npc); Debug.Log($"NPC Spawned! Total NPCS: {currentNpcs.Count}"); } + /// + /// to spawn a new npc after a delay + /// IEnumerator SpawnNPCAfterWait() { isSpawning = true; yield return new WaitForSeconds(npcBufferTime); - - if(currentNpcs.Count < 4) + + if (currentNpcs.Count < 4) { SpawnNPC(); } + isSpawning = false; } - + + /// + /// to end the day and despawn all npcs + /// public void EndDay() { foreach (var npc in currentNpcs) @@ -143,11 +156,14 @@ public class NpcManager : MonoBehaviour npc.GetComponent().Despawn(true); } } - + + /// + /// a struct to store the seat object and availability + /// [Serializable] public struct Seat { public GameObject SeatObject; public bool Available; } -} +} \ No newline at end of file diff --git a/SSLR/Assets/Scripts/ShiftManager.cs b/SSLR/Assets/Scripts/ShiftManager.cs index b9afc5d..d2c3cae 100644 --- a/SSLR/Assets/Scripts/ShiftManager.cs +++ b/SSLR/Assets/Scripts/ShiftManager.cs @@ -1,7 +1,7 @@ /* * Author: Livinia Poo * Date: 4/2/25 - * Description: + * Description: * Starting/Ending Shifts */ @@ -11,11 +11,9 @@ using UnityEngine; public class ShiftManager : MonoBehaviour { - [SerializeField] - private float shiftDuration; + [SerializeField] private float shiftDuration; private float remainingTime; - [SerializeField] - private GameObject npcSpawnArea; + [SerializeField] private GameObject npcSpawnArea; private Collider shiftTrigger; private GameManager gm; @@ -25,10 +23,10 @@ public class ShiftManager : MonoBehaviour { gm = GameObject.Find("Game Manager").GetComponent(); dayManager = GameObject.Find("Day Manager").GetComponent(); - + gm.shiftStarted = false; remainingTime = shiftDuration; - + shiftTrigger = GetComponent(); } @@ -37,7 +35,7 @@ public class ShiftManager : MonoBehaviour if (other.CompareTag("Player") && !gm.shiftStarted) { Debug.Log("Shift started"); - + npcSpawnArea.SetActive(true); gm.shiftStarted = true; shiftTrigger.enabled = false; @@ -56,16 +54,17 @@ public class ShiftManager : MonoBehaviour Debug.Log("Too many mistakes! Shift ended!"); break; } + yield return null; } - + EndShift(); } void EndShift() { Debug.Log("Shift ended!"); - + npcSpawnArea.SetActive(false); remainingTime = shiftDuration; gm.shiftStarted = false; @@ -77,4 +76,4 @@ public class ShiftManager : MonoBehaviour { shiftTrigger.enabled = true; } -} +} \ No newline at end of file