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,17 +1,17 @@
/* /*
Author: Wai Lam and Reza * Author: Wai Lam and Reza
Date: 12/2/25 * Date: 12/2/25
Description: Go to school * Description: Go to school
*/ */
using System.Collections; using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro; using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
public class GoToSchool : MonoBehaviour public class GoToSchool : MonoBehaviour
{ {
private GameManager gameManager; // ReSharper disable once GrammarMistakeInComment
// public PostProcessingManager PostProcessingManager; // public PostProcessingManager PostProcessingManager;
public CanvasGroup fadeCanvasGroup; // Assign in Inspector public CanvasGroup fadeCanvasGroup; // Assign in Inspector
@ -19,29 +19,31 @@ public class GoToSchool : MonoBehaviour
public float displayDuration = 5f; // Time the UI stays fully visible public float displayDuration = 5f; // Time the UI stays fully visible
public AudioSource[] audioSources; public AudioSource[] audioSources;
private bool atPond = false;
private bool hasTriggered = false; // Prevent multiple triggers
public AudioLoop audioLoop; public AudioLoop audioLoop;
public ParticleSystem[] particleEffects; public ParticleSystem[] particleEffects;
// 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;
[Header("Triggers")] [Header("Triggers")] public Collider parkPondTrigger; // Assign in Inspector
public Collider parkPondTrigger; // Assign in Inspector
public Collider schoolTrigger; // Assign in Inspector public Collider schoolTrigger; // Assign in Inspector
public ResetPosition xrRig; public MemoriseInitialPosition xrRig;
void Awake()
private bool _atPond;
private GameManager _gameManager;
private bool _hasTriggered; // Prevent multiple triggers
private void Awake()
{ {
Debug.Log("IM AWAKE"); Debug.Log("IM AWAKE");
// DontDestroyOnLoad(gameObject); _gameManager = GameManager.Instance; // Reference to GameManager instance
gameManager = GameManager.Instance; // Reference to GameManager instance Debug.Log("current day: " + _gameManager.CurrentDay);
Debug.Log("currentday: " + gameManager.currentDay);
if (storyPanelUI == null) if (storyPanelUI == null)
storyPanelUI = GameObject.Find("Story Panel"); // Use the exact name storyPanelUI = GameObject.Find("Story Panel"); // Use the exact name
@ -51,7 +53,9 @@ public class GoToSchool : MonoBehaviour
if (storyPanelUI != null) if (storyPanelUI != null)
storyPanelUI.SetActive(true); storyPanelUI.SetActive(true);
if (gameManager.currentDay == 1) switch (_gameManager.CurrentDay)
{
case 1:
{ {
if (storyText != null) if (storyText != null)
{ {
@ -59,13 +63,10 @@ public class GoToSchool : MonoBehaviour
StartCoroutine(ClearMessageAfterSeconds(7f)); StartCoroutine(ClearMessageAfterSeconds(7f));
} }
if (audioLoop != null) if (audioLoop != null) audioLoop.StartAudioLoop();
{ break;
audioLoop.StartAudioLoop();
} }
} case 2:
if (gameManager.currentDay == 2)
{ {
if (storyText != null) if (storyText != null)
{ {
@ -73,16 +74,41 @@ public class GoToSchool : MonoBehaviour
StartCoroutine(ClearMessageAfterSeconds(7f)); StartCoroutine(ClearMessageAfterSeconds(7f));
} }
foreach (ParticleSystem effect in particleEffects) foreach (var effect in particleEffects)
{ {
effect.gameObject.SetActive(true); // Ensure the GameObject is active effect.gameObject.SetActive(true); // Ensure the GameObject is active
effect.Play(); // Play each particle system effect.Play(); // Play each particle system
} }
// PostProcessingManager.Instance.TriggerEffect("Panic"); // 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;
}
}
} }
private IEnumerator ClearMessageAfterSeconds(float delay) private IEnumerator ClearMessageAfterSeconds(float delay)
@ -92,34 +118,6 @@ public class GoToSchool : MonoBehaviour
storyText.text = ""; 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() private IEnumerator StayAtPond()
{ {
storyText.text = "The sound of the water is soothing..."; storyText.text = "The sound of the water is soothing...";
@ -129,58 +127,52 @@ public class GoToSchool : MonoBehaviour
StartCoroutine(ClearMessageAfterSeconds(7f)); StartCoroutine(ClearMessageAfterSeconds(7f));
} }
IEnumerator FadeInAndLoadScene() private IEnumerator FadeInAndLoadScene()
{ {
yield return StartCoroutine(Fade(0f, 1f, fadeDuration)); yield return StartCoroutine(Fade(0f, 1f, fadeDuration));
yield return new WaitForSeconds(displayDuration); yield return new WaitForSeconds(displayDuration);
int currentDay = gameManager.currentDay; var currentDay = _gameManager.CurrentDay;
string nextScene = currentDay == 2 ? "Day2" : (currentDay == 3 ? "Day3" : "Start"); var nextScene = currentDay switch
{
2 => "Day2",
3 => "Day3",
_ => "Start"
};
SceneManager.LoadScene(nextScene); SceneManager.LoadScene(nextScene);
yield return new WaitForSeconds(1f); // Small delay to ensure scene transition 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; fadeCanvasGroup.alpha = startAlpha;
float[] startVolumes = new float[audioSources.Length]; var startVolumes = new float[audioSources.Length];
for (int i = 0; i < audioSources.Length; i++) for (var i = 0; i < audioSources.Length; i++)
{ startVolumes[i] = audioSources[i] ? audioSources[i].volume : 1f;
startVolumes[i] = audioSources[i] != null ? audioSources[i].volume : 1f;
}
while (elapsed < duration) while (elapsed < duration)
{ {
elapsed += Time.deltaTime; elapsed += Time.deltaTime;
float t = elapsed / duration; var t = elapsed / duration;
fadeCanvasGroup.alpha = Mathf.Lerp(startAlpha, endAlpha, t); fadeCanvasGroup.alpha = Mathf.Lerp(startAlpha, endAlpha, t);
for (int i = 0; i < audioSources.Length; i++) for (var i = 0; i < audioSources.Length; i++)
{ if (audioSources[i])
if (audioSources[i] != null)
{
audioSources[i].volume = Mathf.Lerp(startVolumes[i], 0f, t); audioSources[i].volume = Mathf.Lerp(startVolumes[i], 0f, t);
}
}
yield return null; yield return null;
} }
fadeCanvasGroup.alpha = endAlpha; fadeCanvasGroup.alpha = endAlpha;
foreach (var t in audioSources)
for (int i = 0; i < audioSources.Length; i++) if (t)
{ t.volume = 0f;
if (audioSources[i] != null)
{
audioSources[i].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 // 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 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 HelpBoxAttribute(string text, HelpBoxMessageType messageType = HelpBoxMessageType.None)
public HelpBoxMessageType MessageType; {
Text = text;
public HelpBoxAttribute(string text, HelpBoxMessageType messageType = HelpBoxMessageType.None) { MessageType = messageType;
this.Text = text;
this.MessageType = messageType;
} }
} }
[CustomPropertyDrawer(typeof(HelpBoxAttribute))] [CustomPropertyDrawer(typeof(HelpBoxAttribute))]
public class HelpBoxAttributeDrawer : DecoratorDrawer { public class HelpBoxAttributeDrawer : DecoratorDrawer
{
public override float GetHeight() { public override float GetHeight()
try { {
var helpBoxAttribute = attribute as HelpBoxAttribute; try
if (helpBoxAttribute == null) return base.GetHeight(); {
var helpBoxStyle = (GUI.skin != null) ? GUI.skin.GetStyle("helpbox") : null; if (attribute is not HelpBoxAttribute helpBoxAttribute) return base.GetHeight();
return helpBoxStyle == null ? base.GetHeight() : Mathf.Max(40f, helpBoxStyle.CalcHeight(new GUIContent(helpBoxAttribute.Text), EditorGUIUtility.currentViewWidth) + 4); 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. 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; if (attribute is not HelpBoxAttribute helpBoxAttribute) return;
EditorGUI.HelpBox(position, helpBoxAttribute.Text, GetMessageType(helpBoxAttribute.MessageType)); EditorGUI.HelpBox(position, helpBoxAttribute.Text, GetMessageType(helpBoxAttribute.MessageType));
} }
private static MessageType GetMessageType(HelpBoxMessageType helpBoxMessageType) { private static MessageType GetMessageType(HelpBoxMessageType helpBoxMessageType)
switch (helpBoxMessageType) { {
switch (helpBoxMessageType)
{
default: default:
case HelpBoxMessageType.None: return MessageType.None; case HelpBoxMessageType.None: return MessageType.None;
case HelpBoxMessageType.Info: return MessageType.Info; case HelpBoxMessageType.Info: return MessageType.Info;