game(scripts): standardise StoryTyping

This commit is contained in:
Mark Joshwel 2025-02-15 00:36:25 +08:00
parent c6d0f73340
commit 8e1f74a92b

View file

@ -1,68 +1,65 @@
/* /*
Author: Reza and Wailam * Author: Reza and Wai Lam
Date: 14/2/25 * Date: 14/2/25
Description: For text to appear itself for storytelling * Description: For texts to appear themselves for storytelling
*/ */
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 StoryTyping : MonoBehaviour public class StoryTyping : MonoBehaviour
{ {
[Header("Message Settings")] [Header("Message Settings")]
// Custom message for this trigger // Custom message for this trigger
[TextArea(3, 5)] public string[] storyLines; [TextArea(3, 5)]
public string[] storyLines;
public TMP_Text storyText; public TMP_Text storyText;
// Speed at which text appears // Speed at which text appears
public float typingSpeed = 0.05f; public float typingSpeed = 0.05f;
public string nextSceneName = "NextScene"; public string nextSceneName = "NextScene";
private int currentLine = 0;
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; public float displayDuration = 5f;
private int _currentLine;
private void Start() private void Start()
{ {
// Start typing the first line if there are any lines // Start typing the first line if there are any lines
if (storyLines.Length > 0) if (storyLines.Length > 0) StartCoroutine(TypeText());
{
StartCoroutine(TypeText());
}
} }
IEnumerator TypeText() private IEnumerator TypeText()
{ {
// Loop through each line of text // Loop through each line of text
while (currentLine < storyLines.Length) while (_currentLine < storyLines.Length)
{ {
string fullText = storyLines[currentLine]; var fullText = storyLines[_currentLine];
string currentText = ""; var currentText = "";
// Type out the current line character by character // Type out the current line character by character
for (int i = 0; i < fullText.Length; i++) foreach (var t in fullText)
{ {
currentText += fullText[i]; currentText += t;
storyText.text = currentText; storyText.text = currentText;
yield return new WaitForSeconds(typingSpeed); yield return new WaitForSeconds(typingSpeed);
} }
currentLine++; // Move to the next line _currentLine++; // Move to the next line
yield return new WaitForSeconds(displayDuration); // Wait briefly before displaying the next line yield return new WaitForSeconds(displayDuration); // Wait briefly before displaying the next line
} }
// After all lines are typed, trigger fade and load the next scene // After all lines are typed, trigger fade and load the next scene
StartCoroutine(FadeToBlack()); StartCoroutine(FadeToBlack());
} }
IEnumerator FadeToBlack() private IEnumerator FadeToBlack()
{ {
// Fade to black // Fade to black
yield return StartCoroutine(Fade(0f, 1f, fadeDuration)); yield return StartCoroutine(Fade(0f, 1f, fadeDuration));
@ -71,16 +68,16 @@ public class StoryTyping : MonoBehaviour
SceneManager.LoadScene(nextSceneName); SceneManager.LoadScene(nextSceneName);
} }
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;
// Fade over the specified duration // Fade over the specified duration
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);
yield return null; yield return null;