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 UnityEngine;
using Supabase;
@ -11,16 +18,39 @@ using UnityEngine.UI;
public class Backend : MonoBehaviour
{
/// <summary>
/// singleton instance
/// </summary>
public static Backend instance;
/// <summary>
/// url to supabase
/// </summary>
[SerializeField] private string url;
/// <summary>
/// supabase api anon key
/// </summary>
[SerializeField] private string anonKey;
/// <summary>
/// supabase client
/// </summary>
public Client Client;
/// <summary>
/// supabase auth session
/// </summary>
public Session Session;
/// <summary>
/// user data
/// </summary>
public Users User;
public Image profilePicture;
/// <summary>
/// setting up supabase client
/// </summary>
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");
}
});
}
/// <summary>
/// signing out of supabase auth
/// </summary>
public async void SignOut()
{
User = null;
@ -51,6 +84,16 @@ public class Backend : MonoBehaviour
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,
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<Users>().OnConflict(x => x.uid)
.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)
{
Session = await Client.Auth.SignIn(email, password);
@ -87,8 +135,13 @@ public class Backend : MonoBehaviour
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)
{
// Get the data from the database
var result = await Client.From<Users>().Where(x => x.uid == uid).Get();
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)
{
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
});
}
/// <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)
{
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
}
}
/// <summary>
/// getting the texture from the url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private async Task<Texture2D> GetTextureFromURL(string url)
{
using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
@ -210,6 +278,9 @@ public class Backend : MonoBehaviour
}
}
/// <summary>
/// assigning the singleton instance
/// </summary>
private void Awake()
{
if (instance == null)

View file

@ -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!");
}
}
}
}

View file

@ -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;
}
}
}
}

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
* Description:
* 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
* 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
/// </summary>
public static NpcManager instance;
/// <summary>
/// Assign GameManager script
/// </summary>
@ -43,24 +42,20 @@ public class NpcManager : MonoBehaviour
/// list of all the spawn points
/// </summary>
public Transform[] spawnPoints;
/// <summary>
/// float for time between npc spawns
/// </summary>
[SerializeField] private float npcBufferTime;
public Seat[] Seats;
/// <summary>
/// a bool to check if the player is free
/// </summary>
public bool playerFree = false;
/// <summary>
/// collection of all exiting npcs
/// </summary>
public List<GameObject> currentNpcs;
public List<GameObject> currentNpcs;
/// <summary>
/// a collection of positions for the npcs to despawn
/// </summary>
@ -70,12 +65,15 @@ public class NpcManager : MonoBehaviour
/// flag to prevent multiple coroutines
/// </summary>
private bool isSpawning = false;
/// <summary>
/// the position of the desk
/// </summary>
public Transform desk;
/// <summary>
/// singleton pattern
/// </summary>
void Awake()
{
if (instance == null)
@ -87,10 +85,13 @@ public class NpcManager : MonoBehaviour
{
Destroy(gameObject);
}
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()
{
if (gm.shiftStarted)
@ -106,9 +107,12 @@ public class NpcManager : MonoBehaviour
}
}
/// <summary>
/// to spawn a new npc
/// </summary>
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}");
}
/// <summary>
/// to spawn a new npc after a delay
/// </summary>
IEnumerator SpawnNPCAfterWait()
{
isSpawning = true;
yield return new WaitForSeconds(npcBufferTime);
if(currentNpcs.Count < 4)
if (currentNpcs.Count < 4)
{
SpawnNPC();
}
isSpawning = false;
}
/// <summary>
/// to end the day and despawn all npcs
/// </summary>
public void EndDay()
{
foreach (var npc in currentNpcs)
@ -143,11 +156,14 @@ public class NpcManager : MonoBehaviour
npc.GetComponent<NpcMovementRework>().Despawn(true);
}
}
/// <summary>
/// a struct to store the seat object and availability
/// </summary>
[Serializable]
public struct Seat
{
public GameObject SeatObject;
public bool Available;
}
}
}

View file

@ -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<GameManager>();
dayManager = GameObject.Find("Day Manager").GetComponent<DayManager>();
gm.shiftStarted = false;
remainingTime = shiftDuration;
shiftTrigger = GetComponent<Collider>();
}
@ -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;
}
}
}