game(scripts): trying to do the game loop

This commit is contained in:
rezazfn 2025-02-13 17:16:55 +08:00
parent a0ba4c9bd8
commit e5e33bbf87
2 changed files with 59 additions and 38 deletions

View file

@ -8,6 +8,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using TMPro; using TMPro;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour public class GameManager : MonoBehaviour
{ {
@ -16,8 +17,15 @@ public class GameManager : MonoBehaviour
/// </summary> /// </summary>
public static GameManager Instance; public static GameManager Instance;
// Starts from Day 1
public int currentDay = 1; public int currentDay = 1;
// Tracks GoToSchool task status
private bool goToSchool = false;
private bool bedroomCleaned = false;
private bool teethBrushed = false;
private bool floorSweeped = false;
private string lastSceneName; private string lastSceneName;
// Defines UI references // Defines UI references
@ -25,12 +33,6 @@ public class GameManager : MonoBehaviour
public GameObject storyPanelUI; public GameObject storyPanelUI;
public TMP_Text storyText; public TMP_Text storyText;
// Trackable Task Completions
private bool bedroomCleaned = false;
private bool teethBrushed = false;
private bool floorSweeped = false;
private bool goToSchool = false;
// Queue for managing messages // Queue for managing messages
private Queue<string> messageQueue = new Queue<string>(); private Queue<string> messageQueue = new Queue<string>();
private bool isMessageActive = false; private bool isMessageActive = false;
@ -41,11 +43,10 @@ public class GameManager : MonoBehaviour
public bool IsBedroomCleaned() { return bedroomCleaned; } public bool IsBedroomCleaned() { return bedroomCleaned; }
public bool IsTeethBrushed() { return teethBrushed; } public bool IsTeethBrushed() { return teethBrushed; }
public bool IsFloorSweeped() { return floorSweeped; } public bool IsFloorSweeped() { return floorSweeped; }
public bool IsGoToSchool() { return goToSchool; } public bool IsGoToSchool() { return goToSchool; }
/// <summary> /// <summary>
/// Enforces singleton behaviour; sets doesn't destroy on load and checks for multiple instances /// Enforces singleton behavior; sets doesn't destroy on load and checks for multiple instances
/// </summary> /// </summary>
private void Awake() private void Awake()
{ {
@ -116,6 +117,7 @@ public class GameManager : MonoBehaviour
Debug.Log("Go To School: " + goToSchool); Debug.Log("Go To School: " + goToSchool);
} }
// Checks if all tasks are done before player can go to school
public void AreTasksDone() public void AreTasksDone()
{ {
if (bedroomCleaned && teethBrushed && floorSweeped) if (bedroomCleaned && teethBrushed && floorSweeped)
@ -150,18 +152,23 @@ public class GameManager : MonoBehaviour
goToSchool = true; goToSchool = true;
} }
// Increments the current day by 1
public void IncrementDay() public void IncrementDay()
{ {
currentDay++; currentDay++;
Debug.Log("Day incremented to: " + currentDay); // Debug log for tracking day increment
// Checks if it's Day 4, then loads the callingChoice scene
if (currentDay > 3)
{
LoadCallingScene();
}
} }
public void SetLastScene(string sceneName) // Loads the callingChoice scene when Day 3 is completed
private void LoadCallingScene()
{ {
lastSceneName = sceneName; Debug.Log("Loading Calling Scene: callingChoice");
} SceneManager.LoadScene("callingChoice");
public string GetLastScene()
{
return lastSceneName;
} }
} }

View file

@ -12,25 +12,24 @@ public class GoToSchool : MonoBehaviour
{ {
private GameManager gameManager; private GameManager gameManager;
public CanvasGroup fadeCanvasGroup; // Assign in Inspector public CanvasGroup fadeCanvasGroup; // Assign in Inspector
public float fadeDuration = 1f; // Duration for fade in/out public float fadeDuration = 1f; // Duration for fade in/out
public float displayDuration = 5f; // Time the UI stays fully visible public float displayDuration = 5f; // Time the UI stays fully visible
public string nextSceneName; // Name of the scene to load
public AudioSource[] audioSources; public AudioSource[] audioSources;
private bool hasTriggered = false; // Prevent multiple triggers private bool hasTriggered = false; // Prevent multiple triggers
public AudioLoop audioLoop; public AudioLoop audioLoop;
// Defines UI references // Defines UI references
[Header("UI References")] [Header("UI References")]
public GameObject storyPanelUI; public GameObject storyPanelUI;
public TMP_Text storyText; public TMP_Text storyText;
void Start() void Start()
{ {
gameManager = GameManager.Instance; // Reference to GameManager instance
if (storyPanelUI == null) if (storyPanelUI == null)
storyPanelUI = GameObject.Find("Story Panel"); // Use the exact name storyPanelUI = GameObject.Find("Story Panel"); // Use the exact name
@ -50,15 +49,15 @@ public class GoToSchool : MonoBehaviour
{ {
audioLoop.StartAudioLoop(); audioLoop.StartAudioLoop();
} }
} }
private IEnumerator ClearMessageAfterSeconds(float delay) private IEnumerator ClearMessageAfterSeconds(float delay)
{ {
// Waits for delay to end and hides the UI // Waits for delay to end and hides the UI
yield return new WaitForSeconds(delay); yield return new WaitForSeconds(delay);
storyText.text = ""; storyText.text = "";
} }
private void OnTriggerEnter(Collider other) private void OnTriggerEnter(Collider other)
{ {
// Check if the player entered the trigger // Check if the player entered the trigger
@ -66,12 +65,9 @@ public class GoToSchool : MonoBehaviour
{ {
hasTriggered = true; hasTriggered = true;
StartCoroutine(FadeInAndLoadScene()); StartCoroutine(FadeInAndLoadScene());
GameManager.Instance.GoToSchoolTaskComplete();
} // Task completion is noted here
if (GameManager.Instance != null) gameManager.GoToSchoolTaskComplete();
{
GameManager.Instance.IncrementDay();
} }
} }
@ -83,10 +79,28 @@ public class GoToSchool : MonoBehaviour
// Display UI for 5 seconds // Display UI for 5 seconds
yield return new WaitForSeconds(displayDuration); yield return new WaitForSeconds(displayDuration);
GameManager.Instance.SetLastScene(SceneManager.GetActiveScene().name); // Determine the next scene based on the current day
int currentDay = gameManager.currentDay;
string nextScene;
// Load the next scene switch (currentDay)
SceneManager.LoadScene(nextSceneName); {
case 2:
nextScene = "Day2";
break;
case 3:
nextScene = "Day3"; // need another way to go to day 3 tho bcos they arent going to sch on day 3
break;
default:
nextScene = "Start"; // Fallback in case of unexpected day value
break;
}
// Load the determined next scene
SceneManager.LoadScene(nextScene);
// Increment the day AFTER transitioning to avoid multiple increments
gameManager.IncrementDay();
} }
IEnumerator Fade(float startAlpha, float endAlpha, float duration) IEnumerator Fade(float startAlpha, float endAlpha, float duration)