wirm/Game/Assets/Scripts/GameManager.cs

219 lines
6.6 KiB
C#
Raw Permalink Normal View History

/*
2025-02-14 22:25:01 +08:00
* Author: Reza, Wai Lam, Mark
2025-02-14 22:24:45 +08:00
* Date: 3/2/25
* Description: To keep track of tasks, which level the player is at, and game mechanics
*/
using System.Collections;
using System.Collections.Generic;
using TMPro;
2025-02-14 22:24:45 +08:00
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
/// <summary>
/// Define instance field for accessing the singleton elsewhere
/// </summary>
public static GameManager Instance;
2025-02-11 15:01:17 +08:00
// Defines UI references
[Header("UI References")] public GameObject storyPanelUI;
2025-02-11 15:01:17 +08:00
public TMP_Text storyText;
// Queue for managing messages
2025-02-14 22:25:01 +08:00
private readonly Queue<string> _messageQueue = new();
private bool _bedroomCleaned;
private bool _floorSwept;
2025-02-14 22:24:45 +08:00
// Tracks GoToSchool task status
2025-02-14 22:25:01 +08:00
private bool _goToSchool;
2025-02-14 22:25:01 +08:00
private bool _hasIncrementedToday;
private bool _isMessageActive;
2025-02-14 22:25:01 +08:00
private string _lastSceneName;
private bool _teethBrushed;
2025-02-20 19:53:08 +08:00
public AuthManager authManager; // Add this line to the GameManager
2025-02-14 22:24:45 +08:00
// current day, publicly readable, privately settable
// for public access, setting is a no-op
2025-02-14 22:25:01 +08:00
public int CurrentDay { get; private set; } = 1;
/// <summary>
/// Enforces singleton behavior; sets doesn't destroy on load and checks for multiple instances
/// </summary>
private void Awake()
{
// check if instance hasn't been set yet
if (Instance == null)
{
Debug.Log(
$"game manager ({GetInstanceID()}) is awake as singleton instance, setting self as the forever-alive instance");
Instance = this;
DontDestroyOnLoad(gameObject);
}
// check if instance is already set and it's not this instance
else if (Instance != null && Instance != this)
{
Debug.Log($"game manager ({GetInstanceID()}) is awake as non-singleton instance, destroying self");
Destroy(gameObject);
return;
}
Debug.Log($"game manager ({GetInstanceID()}) is initialising itself!");
2025-02-14 22:25:01 +08:00
CurrentDay = 1;
2025-02-12 22:16:45 +08:00
// Try to find UI elements if not set
2025-02-14 22:24:45 +08:00
if (storyPanelUI == null) storyPanelUI = GameObject.Find("StoryPanelUI");
2025-02-14 22:24:45 +08:00
if (storyText == null) storyText = GameObject.Find("StoryText").GetComponent<TMP_Text>();
2025-02-13 20:33:40 +08:00
SceneManager.sceneLoaded += OnSceneLoaded;
}
// Update is called once per frame
2025-02-14 22:24:45 +08:00
private void Update()
{
// Continuously check and display queued messages
2025-02-14 22:25:01 +08:00
if (!_isMessageActive && _messageQueue.Count > 0)
{
2025-02-14 22:25:01 +08:00
var nextMessage = _messageQueue.Dequeue();
StartCoroutine(DisplayMessage(nextMessage));
}
}
2025-02-14 22:24:45 +08:00
/// <summary>
/// Checks if tasks are completed
/// </summary>
public bool IsBedroomCleaned()
{
2025-02-14 22:25:01 +08:00
return _bedroomCleaned;
2025-02-14 22:24:45 +08:00
}
public bool IsTeethBrushed()
{
2025-02-14 22:25:01 +08:00
return _teethBrushed;
2025-02-14 22:24:45 +08:00
}
2025-02-14 22:25:01 +08:00
public bool IsFloorSwept()
2025-02-14 22:24:45 +08:00
{
2025-02-14 22:25:01 +08:00
return _floorSwept;
2025-02-14 22:24:45 +08:00
}
public bool IsGoToSchool()
{
2025-02-14 22:25:01 +08:00
return _goToSchool;
2025-02-14 22:24:45 +08:00
}
/// <summary>
/// Queues a message to be displayed
/// </summary>
2025-02-14 22:25:01 +08:00
// ReSharper disable once MemberCanBePrivate.Global
public void QueueMessage(string message)
{
2025-02-14 22:25:01 +08:00
_messageQueue.Enqueue(message);
}
/// <summary>
/// Displays a message and waits for it to disappear
/// </summary>
private IEnumerator DisplayMessage(string message)
{
2025-02-14 22:25:01 +08:00
_isMessageActive = true;
storyPanelUI.SetActive(true);
storyText.text = message;
yield return new WaitForSeconds(7f); // Wait for 7 seconds before hiding
storyPanelUI.SetActive(false);
2025-02-14 22:25:01 +08:00
_isMessageActive = false;
}
2025-02-14 22:25:01 +08:00
// log the players choices before leaving the house (for future Firebase tracking)
public void LogPlayerChoices()
{
Debug.Log("Player is trying to leave the house. Task Completion Status:");
2025-02-14 22:25:01 +08:00
Debug.Log("Bedroom Cleaned: " + _bedroomCleaned);
Debug.Log("Teeth Brushed: " + _teethBrushed);
Debug.Log("Floor Swept: " + _floorSwept);
Debug.Log("Go To School: " + _goToSchool);
}
2025-02-11 15:01:17 +08:00
// Checks if all tasks are done before player can go to school
2025-02-14 22:25:01 +08:00
// ReSharper disable once MemberCanBePrivate.Global
2025-02-11 15:01:17 +08:00
public void AreTasksDone()
{
2025-02-14 22:25:01 +08:00
if (_bedroomCleaned && _teethBrushed && _floorSwept)
QueueMessage("I think I did everything... I think I can leave for school now");
2025-02-11 15:01:17 +08:00
}
2025-02-14 22:24:45 +08:00
// for mark (backend): u can track whether they want to do their tasks, some people may be unmotivated to
2025-02-14 22:25:01 +08:00
// do the tasks in game, then we can ask them irl why they didn't do the task
2025-02-20 19:53:08 +08:00
// Tracks if the bedroom is cleaned or not
2025-02-14 22:25:01 +08:00
// Tracks if the bedroom is cleaned or not
public void BedroomTaskComplete()
{
2025-02-14 22:25:01 +08:00
_bedroomCleaned = true;
2025-02-11 15:01:17 +08:00
AreTasksDone();
2025-02-20 19:53:08 +08:00
// Call AuthManager to update the Firebase database
2025-02-20 20:10:47 +08:00
authManager.UpdateTaskStatus("Day1", "clean room", true); // Example: Update "clean room" task to completed
}
2025-02-14 22:25:01 +08:00
// Tracks if teeth are brushed or not
public void BrushTeethTaskComplete()
{
2025-02-14 22:25:01 +08:00
_teethBrushed = true;
2025-02-11 15:01:17 +08:00
AreTasksDone();
2025-02-20 19:53:08 +08:00
// Call AuthManager to update the Firebase database
2025-02-20 20:10:47 +08:00
authManager.UpdateTaskStatus("Day1", "Brush teeth", true); // Update "Brush teeth" task to completed
}
2025-02-14 22:25:01 +08:00
// Tracks if the floor has been swept or not
public void FloorSweptTaskComplete()
{
2025-02-14 22:25:01 +08:00
_floorSwept = true;
2025-02-11 15:01:17 +08:00
AreTasksDone();
2025-02-20 19:53:08 +08:00
// Call AuthManager to update the Firebase database
authManager.UpdateTaskStatus("Day1", "Sweep floor", true); // Update "Sweep floor" task to completed
2025-02-11 15:01:17 +08:00
}
2025-02-12 21:01:29 +08:00
2025-02-20 19:53:08 +08:00
2025-02-12 21:01:29 +08:00
public void GoToSchoolTaskComplete()
{
2025-02-14 22:25:01 +08:00
_goToSchool = true;
2025-02-20 19:53:08 +08:00
// Call AuthManager to update the Firebase database
authManager.UpdateTaskStatus("Day2", "go to school", true); // Update "go to school" task to completed
2025-02-12 21:01:29 +08:00
}
// Increments the current day by 1
2025-02-13 15:11:41 +08:00
public void IncrementDay()
{
2025-02-14 22:25:01 +08:00
if (_hasIncrementedToday) return; // Prevents multiple increments
_hasIncrementedToday = true;
2025-02-14 22:24:45 +08:00
2025-02-14 22:25:01 +08:00
CurrentDay++;
Debug.Log("Day incremented to: " + CurrentDay);
if (CurrentDay > 3) LoadCallingScene();
2025-02-13 15:48:11 +08:00
}
2025-02-14 22:24:45 +08:00
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
2025-02-13 20:33:40 +08:00
{
2025-02-14 22:25:01 +08:00
_hasIncrementedToday = false; // Allows the day to be incremented again in the next transition
2025-02-13 20:33:40 +08:00
}
2025-02-13 15:48:11 +08:00
// Loads the callingChoice scene when Day 3 is completed
private void LoadCallingScene()
2025-02-13 15:48:11 +08:00
{
Debug.Log("Loading Calling Scene: callingChoice");
2025-02-16 23:04:51 +08:00
// Debug.LogError("not ready yet");
2025-02-14 22:25:01 +08:00
// FIXME: SceneManager.LoadScene("CallingChoice");
2025-02-16 23:04:51 +08:00
SceneManager.LoadScene("EndingChoice");
2025-02-13 15:48:11 +08:00
}
}