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 UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
@ -15,9 +16,16 @@ public class GameManager : MonoBehaviour
/// Define instance field for accessing the singleton elsewhere
/// </summary>
public static GameManager Instance;
// Starts from Day 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;
// Defines UI references
@ -25,27 +33,20 @@ public class GameManager : MonoBehaviour
public GameObject storyPanelUI;
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
private Queue<string> messageQueue = new Queue<string>();
private bool isMessageActive = false;
/// <summary>
/// Checks if tasks are completed
/// </summary>
public bool IsBedroomCleaned() { return bedroomCleaned; }
public bool IsTeethBrushed() { return teethBrushed; }
public bool IsFloorSweeped() { return floorSweeped; }
public bool IsGoToSchool() { return goToSchool; }
/// <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>
private void Awake()
{
@ -62,7 +63,7 @@ public class GameManager : MonoBehaviour
Debug.Log("awake as non-singleton instance, destroying self");
Destroy(gameObject);
}
// Try to find UI elements if not set
if (storyPanelUI == null)
{
@ -116,6 +117,7 @@ public class GameManager : MonoBehaviour
Debug.Log("Go To School: " + goToSchool);
}
// Checks if all tasks are done before player can go to school
public void AreTasksDone()
{
if (bedroomCleaned && teethBrushed && floorSweeped)
@ -149,19 +151,24 @@ public class GameManager : MonoBehaviour
{
goToSchool = true;
}
// Increments the current day by 1
public void IncrementDay()
{
currentDay++;
}
public void SetLastScene(string sceneName)
{
lastSceneName = sceneName;
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 string GetLastScene()
// Loads the callingChoice scene when Day 3 is completed
private void LoadCallingScene()
{
return lastSceneName;
Debug.Log("Loading Calling Scene: callingChoice");
SceneManager.LoadScene("callingChoice");
}
}
}

View file

@ -11,26 +11,25 @@ using UnityEngine.SceneManagement;
public class GoToSchool : MonoBehaviour
{
private GameManager gameManager;
public CanvasGroup fadeCanvasGroup; // Assign in Inspector
public float fadeDuration = 1f; // Duration for fade in/out
public float displayDuration = 5f; // Time the UI stays fully visible
public string nextSceneName; // Name of the scene to load
public AudioSource[] audioSources;
private bool hasTriggered = false; // Prevent multiple triggers
public AudioLoop audioLoop;
// Defines UI references
[Header("UI References")]
public GameObject storyPanelUI;
public TMP_Text storyText;
void Start()
{
gameManager = GameManager.Instance; // Reference to GameManager instance
if (storyPanelUI == null)
storyPanelUI = GameObject.Find("Story Panel"); // Use the exact name
@ -50,15 +49,15 @@ public class GoToSchool : MonoBehaviour
{
audioLoop.StartAudioLoop();
}
}
private IEnumerator ClearMessageAfterSeconds(float delay)
{
// Waits for delay to end and hides the UI
yield return new WaitForSeconds(delay);
storyText.text = "";
}
private void OnTriggerEnter(Collider other)
{
// Check if the player entered the trigger
@ -66,12 +65,9 @@ public class GoToSchool : MonoBehaviour
{
hasTriggered = true;
StartCoroutine(FadeInAndLoadScene());
GameManager.Instance.GoToSchoolTaskComplete();
}
if (GameManager.Instance != null)
{
GameManager.Instance.IncrementDay();
// Task completion is noted here
gameManager.GoToSchoolTaskComplete();
}
}
@ -82,11 +78,29 @@ public class GoToSchool : MonoBehaviour
// Display UI for 5 seconds
yield return new WaitForSeconds(displayDuration);
GameManager.Instance.SetLastScene(SceneManager.GetActiveScene().name);
// Load the next scene
SceneManager.LoadScene(nextSceneName);
// Determine the next scene based on the current day
int currentDay = gameManager.currentDay;
string nextScene;
switch (currentDay)
{
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)
@ -128,4 +142,4 @@ public class GoToSchool : MonoBehaviour
}
}
}
}
}