game(scripts): standardise my stuff

This commit is contained in:
Mark Joshwel 2025-02-15 00:14:14 +08:00
parent dd29133152
commit 87f7a7ac6f
2 changed files with 149 additions and 134 deletions

View file

@ -1,88 +1,114 @@
/*
Author: Wai Lam and Reza
Date: 12/2/25
Description: Go to school
*/
* Author: Wai Lam and Reza
* Date: 12/2/25
* Description: Go to school
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GoToSchool : MonoBehaviour
{
private GameManager gameManager;
// public PostProcessingManager PostProcessingManager;
// ReSharper disable once GrammarMistakeInComment
// public PostProcessingManager PostProcessingManager;
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 float fadeDuration = 1f; // Duration for fade in/out
public float displayDuration = 5f; // Time the UI stays fully visible
public AudioSource[] audioSources;
private bool atPond = false;
private bool hasTriggered = false; // Prevent multiple triggers
public AudioLoop audioLoop;
public ParticleSystem[] particleEffects;
// Defines UI references
[Header("UI References")]
public GameObject storyPanelUI;
public TMP_Text storyText;
[Header("Triggers")]
public Collider parkPondTrigger; // Assign in Inspector
public Collider schoolTrigger; // Assign in Inspector
public ResetPosition xrRig;
void Awake()
public ParticleSystem[] particleEffects;
// Defines UI references
[Header("UI References")] public GameObject storyPanelUI;
public TMP_Text storyText;
[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()
{
Debug.Log("IM AWAKE");
// DontDestroyOnLoad(gameObject);
gameManager = GameManager.Instance; // Reference to GameManager instance
Debug.Log("currentday: " + gameManager.currentDay);
_gameManager = GameManager.Instance; // Reference to GameManager instance
Debug.Log("current day: " + _gameManager.CurrentDay);
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
if (storyPanelUI != null)
storyPanelUI.SetActive(true);
if (gameManager.currentDay == 1)
{
if (storyText != null)
{
storyText.text = "I guess I should head to school now...";
StartCoroutine(ClearMessageAfterSeconds(7f));
}
if (audioLoop != null)
{
audioLoop.StartAudioLoop();
}
}
if (gameManager.currentDay == 2)
switch (_gameManager.CurrentDay)
{
if (storyText != null)
case 1:
{
storyText.text = "I need to calm down first... maybe going to the park pond would help...";
StartCoroutine(ClearMessageAfterSeconds(7f));
if (storyText != null)
{
storyText.text = "I guess I should head to school now...";
StartCoroutine(ClearMessageAfterSeconds(7f));
}
if (audioLoop != null) audioLoop.StartAudioLoop();
break;
}
foreach (ParticleSystem effect in particleEffects)
case 2:
{
effect.gameObject.SetActive(true); // Ensure the GameObject is active
effect.Play(); // Play each particle system
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
}
// PostProcessingManager.Instance.TriggerEffect("Panic");
break;
}
}
}
private void OnTriggerEnter(Collider other)
{
Debug.Log("Triggered by: " + other.gameObject.name);
switch (_gameManager.CurrentDay)
{
// 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:
{
_hasTriggered = true;
StartCoroutine(FadeInAndLoadScene());
_gameManager.GoToSchoolTaskComplete();
_gameManager.IncrementDay();
break;
}
// PostProcessingManager.Instance.TriggerEffect("Panic");
}
}
private IEnumerator ClearMessageAfterSeconds(float delay)
@ -92,95 +118,61 @@ public class GoToSchool : MonoBehaviour
storyText.text = "";
}
private void OnTriggerEnter(Collider other)
{
Debug.Log("Triggered by: " + other.gameObject.name);
if (gameManager.currentDay == 2)
{
if (!atPond && other == parkPondTrigger) // Player arrives at pond first
{
atPond = true;
StartCoroutine(StayAtPond());
}
else if (atPond && other == schoolTrigger) // Player can go to school after pond
{
hasTriggered = true;
StartCoroutine(FadeInAndLoadScene());
gameManager.GoToSchoolTaskComplete();
gameManager.IncrementDay();
}
}
else if (gameManager.currentDay == 1) // Normal case for Day 1
{
hasTriggered = true;
StartCoroutine(FadeInAndLoadScene());
gameManager.GoToSchoolTaskComplete();
gameManager.IncrementDay();
}
}
private IEnumerator StayAtPond()
{
storyText.text = "The sound of the water is soothing...";
yield return new WaitForSeconds(7f);
// PostProcessingManager.Instance.StopEffect();
// PostProcessingManager.Instance.StopEffect();
storyText.text = "I feel a little better now. I should head to school now.";
StartCoroutine(ClearMessageAfterSeconds(7f));
}
IEnumerator FadeInAndLoadScene()
private IEnumerator FadeInAndLoadScene()
{
yield return StartCoroutine(Fade(0f, 1f, fadeDuration));
yield return new WaitForSeconds(displayDuration);
int currentDay = gameManager.currentDay;
string nextScene = currentDay == 2 ? "Day2" : (currentDay == 3 ? "Day3" : "Start");
var currentDay = _gameManager.CurrentDay;
var nextScene = currentDay switch
{
2 => "Day2",
3 => "Day3",
_ => "Start"
};
SceneManager.LoadScene(nextScene);
yield return new WaitForSeconds(1f); // Small delay to ensure scene transition
xrRig.ResetingPosition();
xrRig.ResetPosition();
}
IEnumerator Fade(float startAlpha, float endAlpha, float duration)
private IEnumerator Fade(float startAlpha, float endAlpha, float duration)
{
float elapsed = 0f;
var elapsed = 0f;
fadeCanvasGroup.alpha = startAlpha;
float[] startVolumes = new float[audioSources.Length];
for (int i = 0; i < audioSources.Length; i++)
{
startVolumes[i] = audioSources[i] != null ? audioSources[i].volume : 1f;
}
var startVolumes = new float[audioSources.Length];
for (var i = 0; i < audioSources.Length; i++)
startVolumes[i] = audioSources[i] ? audioSources[i].volume : 1f;
while (elapsed < duration)
{
elapsed += Time.deltaTime;
float t = elapsed / duration;
var t = elapsed / duration;
fadeCanvasGroup.alpha = Mathf.Lerp(startAlpha, endAlpha, t);
for (int i = 0; i < audioSources.Length; i++)
{
if (audioSources[i] != null)
{
for (var i = 0; i < audioSources.Length; i++)
if (audioSources[i])
audioSources[i].volume = Mathf.Lerp(startVolumes[i], 0f, t);
}
}
yield return null;
}
fadeCanvasGroup.alpha = endAlpha;
for (int i = 0; i < audioSources.Length; i++)
{
if (audioSources[i] != null)
{
audioSources[i].volume = 0f;
}
}
foreach (var t in audioSources)
if (t)
t.volume = 0f;
}
}

View file

@ -1,42 +1,65 @@
/*
* Author: Mark
* Date: 31/1/25
* Description: adds a help box to the inspector window
*/
// https://discussions.unity.com/t/helpattribute-allows-you-to-use-helpbox-in-the-unity-inspector-window/659414/22
using UnityEngine;
using System;
using UnityEditor;
using UnityEngine;
public enum HelpBoxMessageType { None, Info, Warning, Error }
public enum HelpBoxMessageType
{
None,
Info,
Warning,
Error
}
public class HelpBoxAttribute : PropertyAttribute {
public class HelpBoxAttribute : PropertyAttribute
{
public readonly HelpBoxMessageType MessageType;
public readonly string Text;
public string Text;
public HelpBoxMessageType MessageType;
public HelpBoxAttribute(string text, HelpBoxMessageType messageType = HelpBoxMessageType.None) {
this.Text = text;
this.MessageType = messageType;
public HelpBoxAttribute(string text, HelpBoxMessageType messageType = HelpBoxMessageType.None)
{
Text = text;
MessageType = messageType;
}
}
[CustomPropertyDrawer(typeof(HelpBoxAttribute))]
public class HelpBoxAttributeDrawer : DecoratorDrawer {
public override float GetHeight() {
try {
var helpBoxAttribute = attribute as HelpBoxAttribute;
if (helpBoxAttribute == null) return base.GetHeight();
var helpBoxStyle = (GUI.skin != null) ? GUI.skin.GetStyle("helpbox") : null;
return helpBoxStyle == null ? base.GetHeight() : Mathf.Max(40f, helpBoxStyle.CalcHeight(new GUIContent(helpBoxAttribute.Text), EditorGUIUtility.currentViewWidth) + 4);
public class HelpBoxAttributeDrawer : DecoratorDrawer
{
public override float GetHeight()
{
try
{
if (attribute is not HelpBoxAttribute helpBoxAttribute) return base.GetHeight();
var helpBoxStyle = GUI.skin != null ? GUI.skin.GetStyle("helpbox") : null;
return helpBoxStyle == null
? base.GetHeight()
: Mathf.Max(40f,
helpBoxStyle.CalcHeight(new GUIContent(helpBoxAttribute.Text), EditorGUIUtility.currentViewWidth) +
4);
}
catch (System.ArgumentException) {
catch (ArgumentException)
{
return 3 * EditorGUIUtility.singleLineHeight; // Handle Unity 2022.2 bug by returning default value.
}
}
public override void OnGUI(Rect position) {
public override void OnGUI(Rect position)
{
if (attribute is not HelpBoxAttribute helpBoxAttribute) return;
EditorGUI.HelpBox(position, helpBoxAttribute.Text, GetMessageType(helpBoxAttribute.MessageType));
}
private static MessageType GetMessageType(HelpBoxMessageType helpBoxMessageType) {
switch (helpBoxMessageType) {
private static MessageType GetMessageType(HelpBoxMessageType helpBoxMessageType)
{
switch (helpBoxMessageType)
{
default:
case HelpBoxMessageType.None: return MessageType.None;
case HelpBoxMessageType.Info: return MessageType.Info;
@ -44,4 +67,4 @@ public class HelpBoxAttributeDrawer : DecoratorDrawer {
case HelpBoxMessageType.Error: return MessageType.Error;
}
}
}
}