wirm/Game/Assets/Scripts/GoToSchool.cs

185 lines
5.6 KiB
C#
Raw Normal View History

2025-02-12 21:01:29 +08:00
/*
2025-02-15 00:14:14 +08:00
* Author: Wai Lam and Reza
* Date: 12/2/25
* Description: Go to school
*/
2025-02-12 21:01:29 +08:00
using System.Collections;
using TMPro;
2025-02-15 00:14:14 +08:00
using UnityEngine;
2025-02-12 21:01:29 +08:00
using UnityEngine.SceneManagement;
2025-02-14 10:38:31 +08:00
2025-02-12 21:01:29 +08:00
public class GoToSchool : MonoBehaviour
{
2025-02-15 00:14:14 +08:00
// ReSharper disable once GrammarMistakeInComment
// public PostProcessingManager PostProcessingManager;
2025-02-12 21:01:29 +08:00
public CanvasGroup fadeCanvasGroup; // Assign in Inspector
2025-02-15 00:14:14 +08:00
public float fadeDuration = 1f; // Duration for fade in/out
public float displayDuration = 5f; // Time the UI stays fully visible
2025-02-12 21:01:29 +08:00
public AudioSource[] audioSources;
public AudioLoop audioLoop;
2025-02-15 00:14:14 +08:00
2025-02-14 10:38:31 +08:00
public ParticleSystem[] particleEffects;
public AudioSource rainAudioSource;
2025-02-15 00:14:14 +08:00
2025-02-12 21:01:29 +08:00
// Defines UI references
2025-02-15 00:14:14 +08:00
[Header("UI References")] public GameObject storyPanelUI;
2025-02-12 21:01:29 +08:00
public TMP_Text storyText;
2025-02-15 00:14:14 +08:00
[Header("Triggers")] public Collider parkPondTrigger; // Assign in Inspector
public Collider schoolTrigger; // Assign in Inspector
public MemoriseInitialPosition xrRig;
private bool _atPond;
private GameManager _gameManager;
private bool _hasTriggered; // Prevent multiple triggers
private void Awake()
2025-02-12 21:01:29 +08:00
{
2025-02-13 22:10:19 +08:00
Debug.Log("IM AWAKE");
2025-02-15 00:14:14 +08:00
_gameManager = GameManager.Instance; // Reference to GameManager instance
Debug.Log("current day: " + _gameManager.CurrentDay);
2025-02-12 21:01:29 +08:00
if (storyPanelUI == null)
storyPanelUI = GameObject.Find("Story Panel"); // Use the exact name
if (storyText == null)
storyText = FindObjectOfType<TMP_Text>(); // Finds the first TMP_Text in the scene
2025-02-15 00:14:14 +08:00
2025-02-12 21:01:29 +08:00
if (storyPanelUI != null)
storyPanelUI.SetActive(true);
2025-02-15 00:14:14 +08:00
switch (_gameManager.CurrentDay)
2025-02-12 21:01:29 +08:00
{
2025-02-15 00:14:14 +08:00
case 1:
2025-02-13 21:22:19 +08:00
{
2025-02-15 00:14:14 +08:00
if (storyText != null)
{
storyText.text = "I guess I should head to school now...";
StartCoroutine(ClearMessageAfterSeconds(7f));
}
if (audioLoop != null) audioLoop.StartAudioLoop();
break;
2025-02-13 21:22:19 +08:00
}
2025-02-15 00:14:14 +08:00
case 2:
2025-02-13 21:22:19 +08:00
{
2025-02-15 00:14:14 +08:00
if (storyText != null)
{
storyText.text = "I need to calm down first... maybe going to the park pond would help...";
StartCoroutine(ClearMessageAfterSeconds(7f));
}
foreach (var effect in particleEffects)
{
effect.gameObject.SetActive(true); // Ensure the GameObject is active
effect.Play(); // Play each particle system
}
if (rainAudioSource != null)
{
rainAudioSource.volume = 0.8f;
rainAudioSource.Play();
}
2025-02-15 00:14:14 +08:00
// PostProcessingManager.Instance.TriggerEffect("Panic");
break;
2025-02-13 21:22:19 +08:00
}
}
2025-02-15 00:14:14 +08:00
}
2025-02-13 21:22:19 +08:00
2025-02-15 00:14:14 +08:00
private void OnTriggerEnter(Collider other)
{
Debug.Log("Triggered by: " + other.gameObject.name);
switch (_gameManager.CurrentDay)
2025-02-12 21:01:29 +08:00
{
2025-02-15 00:14:14 +08:00
// Player arrives at the pond first
// if (!atPond && other == parkPondTrigger)
case 2 when !_atPond && other == parkPondTrigger:
_atPond = true;
StartCoroutine(StayAtPond());
break;
// if (atPond && other == schoolTrigger)
// Normal case for Day 1
case 2 when _atPond && other == schoolTrigger:
case 1:
2025-02-14 10:38:31 +08:00
{
2025-02-15 00:14:14 +08:00
_hasTriggered = true;
StartCoroutine(FadeInAndLoadScene());
_gameManager.GoToSchoolTaskComplete();
_gameManager.IncrementDay();
break;
2025-02-14 10:38:31 +08:00
}
2025-02-12 21:01:29 +08:00
}
}
2025-02-12 21:01:29 +08:00
private IEnumerator ClearMessageAfterSeconds(float delay)
{
// Waits for delay to end and hides the UI
yield return new WaitForSeconds(delay);
storyText.text = "";
}
2025-02-13 21:22:19 +08:00
private IEnumerator StayAtPond()
{
storyText.text = "The sound of the water is soothing...";
yield return new WaitForSeconds(7f);
2025-02-15 00:14:14 +08:00
// PostProcessingManager.Instance.StopEffect();
2025-02-13 21:22:19 +08:00
storyText.text = "I feel a little better now. I should head to school now.";
StartCoroutine(ClearMessageAfterSeconds(7f));
}
2025-02-12 21:01:29 +08:00
2025-02-15 00:14:14 +08:00
private IEnumerator FadeInAndLoadScene()
2025-02-12 21:01:29 +08:00
{
yield return StartCoroutine(Fade(0f, 1f, fadeDuration));
yield return new WaitForSeconds(displayDuration);
2025-02-15 00:14:14 +08:00
var currentDay = _gameManager.CurrentDay;
var nextScene = currentDay switch
{
2 => "Day2",
3 => "Day3",
_ => "Start"
};
SceneManager.LoadScene(nextScene);
2025-02-13 20:33:40 +08:00
yield return new WaitForSeconds(1f); // Small delay to ensure scene transition
2025-02-15 00:14:14 +08:00
xrRig.ResetPosition();
2025-02-12 21:01:29 +08:00
}
2025-02-15 00:14:14 +08:00
private IEnumerator Fade(float startAlpha, float endAlpha, float duration)
2025-02-12 21:01:29 +08:00
{
2025-02-15 00:14:14 +08:00
var elapsed = 0f;
2025-02-12 21:01:29 +08:00
fadeCanvasGroup.alpha = startAlpha;
2025-02-15 00:14:14 +08:00
var startVolumes = new float[audioSources.Length];
for (var i = 0; i < audioSources.Length; i++)
startVolumes[i] = audioSources[i] ? audioSources[i].volume : 1f;
2025-02-12 21:01:29 +08:00
while (elapsed < duration)
{
elapsed += Time.deltaTime;
2025-02-15 00:14:14 +08:00
var t = elapsed / duration;
2025-02-12 21:01:29 +08:00
fadeCanvasGroup.alpha = Mathf.Lerp(startAlpha, endAlpha, t);
2025-02-15 00:14:14 +08:00
for (var i = 0; i < audioSources.Length; i++)
if (audioSources[i])
2025-02-12 21:01:29 +08:00
audioSources[i].volume = Mathf.Lerp(startVolumes[i], 0f, t);
yield return null;
}
fadeCanvasGroup.alpha = endAlpha;
2025-02-15 00:14:14 +08:00
foreach (var t in audioSources)
if (t)
t.volume = 0f;
2025-02-12 21:01:29 +08:00
}
}