game: clean up + comments

This commit is contained in:
ryan 2025-02-17 12:12:01 +08:00
parent dae091faca
commit 23da2fd76d
8 changed files with 135 additions and 97 deletions

View file

@ -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 System.Threading.Tasks;
using UnityEngine; using UnityEngine;
using Supabase; using Supabase;
@ -11,16 +18,39 @@ using UnityEngine.UI;
public class Backend : MonoBehaviour public class Backend : MonoBehaviour
{ {
/// <summary>
/// singleton instance
/// </summary>
public static Backend instance; public static Backend instance;
/// <summary>
/// url to supabase
/// </summary>
[SerializeField] private string url; [SerializeField] private string url;
/// <summary>
/// supabase api anon key
/// </summary>
[SerializeField] private string anonKey; [SerializeField] private string anonKey;
/// <summary>
/// supabase client
/// </summary>
public Client Client; public Client Client;
/// <summary>
/// supabase auth session
/// </summary>
public Session Session; public Session Session;
/// <summary>
/// user data
/// </summary>
public Users User; public Users User;
public Image profilePicture; /// <summary>
/// setting up supabase client
/// </summary>
private async void Start() private async void Start()
{ {
var options = new SupabaseOptions var options = new SupabaseOptions
@ -32,6 +62,7 @@ public class Backend : MonoBehaviour
Client = new Supabase.Client(url, anonKey, options); Client = new Supabase.Client(url, anonKey, options);
await Client.InitializeAsync().ContinueWith(task => await Client.InitializeAsync().ContinueWith(task =>
{ {
// Check if the task is successful
if (!task.IsCompletedSuccessfully) if (!task.IsCompletedSuccessfully)
{ {
Debug.LogError(task.Exception); Debug.LogError(task.Exception);
@ -41,9 +72,11 @@ public class Backend : MonoBehaviour
Debug.Log("Supabase Initialized"); Debug.Log("Supabase Initialized");
} }
}); });
} }
/// <summary>
/// signing out of supabase auth
/// </summary>
public async void SignOut() public async void SignOut()
{ {
User = null; User = null;
@ -51,6 +84,16 @@ public class Backend : MonoBehaviour
await Client.Auth.SignOut(); await Client.Auth.SignOut();
} }
/// <summary>
/// sending user data to supabase
/// </summary>
/// <param name="uid">the user id of the current player</param>
/// <param name="profilePictureUrl">the url for the profile picture</param>
/// <param name="score">total score</param>
/// <param name="displayName">name of this player</param>
/// <param name="daysPlayed">the number of days that this player has gone through</param>
/// <param name="customersHelpedCorrectly"></param>
/// <param name="customersHelpedWrongly"></param>
public async void SendData(string uid, string profilePictureUrl, int score, string displayName, int daysPlayed, public async void SendData(string uid, string profilePictureUrl, int score, string displayName, int daysPlayed,
int customersHelpedCorrectly, int customersHelpedCorrectly,
int customersHelpedWrongly) int customersHelpedWrongly)
@ -65,6 +108,7 @@ public class Backend : MonoBehaviour
customersHelpedCorrectly = customersHelpedCorrectly, customersHelpedCorrectly = customersHelpedCorrectly,
customersHelpedWrongly = customersHelpedWrongly, customersHelpedWrongly = customersHelpedWrongly,
}; };
// Send the data to the database
await Client.From<Users>().OnConflict(x => x.uid) await Client.From<Users>().OnConflict(x => x.uid)
.Upsert(user).ContinueWith(SendTask => .Upsert(user).ContinueWith(SendTask =>
{ {
@ -79,7 +123,11 @@ public class Backend : MonoBehaviour
}); });
} }
/// <summary>
/// sign in to supabase auth
/// </summary>
/// <param name="email"></param>
/// <param name="password"></param>
public async void SignIn(string email, string password) public async void SignIn(string email, string password)
{ {
Session = await Client.Auth.SignIn(email, password); Session = await Client.Auth.SignIn(email, password);
@ -87,8 +135,13 @@ public class Backend : MonoBehaviour
GetData(Session.User.Id); GetData(Session.User.Id);
} }
/// <summary>
/// gettting user data from supabase
/// </summary>
/// <param name="uid">the uid for the player from auth</param>
public async void GetData(string uid) public async void GetData(string uid)
{ {
// Get the data from the database
var result = await Client.From<Users>().Where(x => x.uid == uid).Get(); var result = await Client.From<Users>().Where(x => x.uid == uid).Get();
User = result.Model; User = result.Model;
@ -113,10 +166,15 @@ public class Backend : MonoBehaviour
} }
} }
/// <summary>
/// getting the npc data from firebase
/// </summary>
/// <param name="target">the npc script for desired npc</param>
public void FirebaseGet(NpcMovementRework target) public void FirebaseGet(NpcMovementRework target)
{ {
NpcData data = new NpcData(); 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() .GetValueAsync()
.ContinueWithOnMainThread(task => .ContinueWithOnMainThread(task =>
{ {
@ -151,6 +209,11 @@ public class Backend : MonoBehaviour
}); });
} }
/// <summary>
/// getting the profile picture from the url
/// </summary>
/// <param name="url"></param>
/// <param name="targetRenderer">the targeted ui image</param>
public async void GetProfile(string url, Image targetRenderer) public async void GetProfile(string url, Image targetRenderer)
{ {
if (string.IsNullOrEmpty(url)) if (string.IsNullOrEmpty(url))
@ -187,6 +250,11 @@ public class Backend : MonoBehaviour
} }
} }
/// <summary>
/// getting the texture from the url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private async Task<Texture2D> GetTextureFromURL(string url) private async Task<Texture2D> GetTextureFromURL(string url)
{ {
using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url)) using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
@ -210,6 +278,9 @@ public class Backend : MonoBehaviour
} }
} }
/// <summary>
/// assigning the singleton instance
/// </summary>
private void Awake() private void Awake()
{ {
if (instance == null) if (instance == null)

View file

@ -53,5 +53,4 @@ public class DayManager : MonoBehaviour
doneAShift = false; doneAShift = false;
} }
} }
} }

View file

@ -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;
*/
/// <summary>
/// Destroy NPC object on trigger enter
/// </summary>
/// <param name="other"></param>
public void OnTriggerEnter(Collider other)
{
if (other.CompareTag("NPC"))
{
GameManager.instance.currentNPC = null;
/*
NPCSpawn.instance.npcSpawned = false;
*/
Destroy(other.gameObject);
}
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: a9d335c7da7e4ea49aac3c7a08b135d2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,5 +1,5 @@
/* /*
* Author: Lin Hengrui Ryan * Author: Lin Hengrui Ryan, Livinia Poo
* Date: 3/2/25 * Date: 3/2/25
* Description: * Description:
* Customer walking handling using NavMesh * Customer walking handling using NavMesh

View file

@ -1,5 +1,5 @@
/* /*
* Author: Lin Hengrui Ryan and Livinia Poo * Author: Lin Hengrui Ryan, Livinia Poo
* Date: 1/2/25 * Date: 1/2/25
* Description: * Description:
* Npc Manager * Npc Manager
@ -9,7 +9,6 @@ using System;
using UnityEngine; using UnityEngine;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections; using System.Collections;
using UnityEngine.Serialization; using UnityEngine.Serialization;
public class NpcManager : MonoBehaviour public class NpcManager : MonoBehaviour
@ -51,10 +50,6 @@ public class NpcManager : MonoBehaviour
public Seat[] Seats; public Seat[] Seats;
/// <summary>
/// a bool to check if the player is free
/// </summary>
public bool playerFree = false;
/// <summary> /// <summary>
/// collection of all exiting npcs /// collection of all exiting npcs
@ -76,6 +71,9 @@ public class NpcManager : MonoBehaviour
/// </summary> /// </summary>
public Transform desk; public Transform desk;
/// <summary>
/// singleton pattern
/// </summary>
void Awake() void Awake()
{ {
if (instance == null) if (instance == null)
@ -91,6 +89,9 @@ public class NpcManager : MonoBehaviour
gm = GameObject.Find("Game Manager").GetComponent<GameManager>(); gm = GameObject.Find("Game Manager").GetComponent<GameManager>();
} }
/// <summary>
/// called once per frame to check if the shift has started and if there are less than 4 npcs
/// </summary>
private void Update() private void Update()
{ {
if (gm.shiftStarted) if (gm.shiftStarted)
@ -106,9 +107,12 @@ public class NpcManager : MonoBehaviour
} }
} }
/// <summary>
/// to spawn a new npc
/// </summary>
public void SpawnNPC() public void SpawnNPC()
{ {
var randomNpc=0; var randomNpc = 0;
bool isFemale = UnityEngine.Random.value > 0.5f; bool isFemale = UnityEngine.Random.value > 0.5f;
var spawnPoint = spawnPoints[UnityEngine.Random.Range(0, spawnPoints.Length)]; var spawnPoint = spawnPoints[UnityEngine.Random.Range(0, spawnPoints.Length)];
if (isFemale) if (isFemale)
@ -119,23 +123,32 @@ public class NpcManager : MonoBehaviour
{ {
randomNpc = UnityEngine.Random.Range(0, maleNpcs.Length); 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); currentNpcs.Add(npc);
Debug.Log($"NPC Spawned! Total NPCS: {currentNpcs.Count}"); Debug.Log($"NPC Spawned! Total NPCS: {currentNpcs.Count}");
} }
/// <summary>
/// to spawn a new npc after a delay
/// </summary>
IEnumerator SpawnNPCAfterWait() IEnumerator SpawnNPCAfterWait()
{ {
isSpawning = true; isSpawning = true;
yield return new WaitForSeconds(npcBufferTime); yield return new WaitForSeconds(npcBufferTime);
if(currentNpcs.Count < 4) if (currentNpcs.Count < 4)
{ {
SpawnNPC(); SpawnNPC();
} }
isSpawning = false; isSpawning = false;
} }
/// <summary>
/// to end the day and despawn all npcs
/// </summary>
public void EndDay() public void EndDay()
{ {
foreach (var npc in currentNpcs) foreach (var npc in currentNpcs)
@ -144,6 +157,9 @@ public class NpcManager : MonoBehaviour
} }
} }
/// <summary>
/// a struct to store the seat object and availability
/// </summary>
[Serializable] [Serializable]
public struct Seat public struct Seat
{ {

View file

@ -11,11 +11,9 @@ using UnityEngine;
public class ShiftManager : MonoBehaviour public class ShiftManager : MonoBehaviour
{ {
[SerializeField] [SerializeField] private float shiftDuration;
private float shiftDuration;
private float remainingTime; private float remainingTime;
[SerializeField] [SerializeField] private GameObject npcSpawnArea;
private GameObject npcSpawnArea;
private Collider shiftTrigger; private Collider shiftTrigger;
private GameManager gm; private GameManager gm;
@ -56,6 +54,7 @@ public class ShiftManager : MonoBehaviour
Debug.Log("Too many mistakes! Shift ended!"); Debug.Log("Too many mistakes! Shift ended!");
break; break;
} }
yield return null; yield return null;
} }