ccl4/blueberryPeak/Assets/Scripts/Player/DialogManager.cs
2025-06-18 10:40:56 +02:00

64 lines
1.8 KiB
C#

using System.Collections;
using TMPro;
using UnityEngine;
using Event = AK.Wwise.Event;
public class DialogManager : MonoBehaviour
{
public GameObject dialogPanel;
public TextMeshProUGUI dialogText; // Use TextMeshProUGUI instead of Text
public PlayerMovement playerMovement;
[SerializeField] private Event TypeSound;
private Coroutine dialogCoroutine;
private void Start()
{
dialogPanel.SetActive(false); // Hide on start
}
public void ShowDialog(string message)
{
print("should be showing dialog");
if (dialogCoroutine != null) StopCoroutine(dialogCoroutine);
dialogCoroutine = StartCoroutine(ShowDialogCoroutine(message));
dialogPanel.SetActive(true);
playerMovement.moveAllowed = false;
}
// Coroutine to show dialog. Each letter is displayed one by one
// Coroutine to show dialog. Each letter is displayed one by one
private IEnumerator ShowDialogCoroutine(string message)
{
dialogText.text = ""; // Clear previous text
dialogPanel.SetActive(true);
playerMovement.moveAllowed = false;
var letterCount = 0;
var nextSoundTrigger = Random.Range(3, 8); // Random between 2 and 5 inclusive
foreach (var letter in message)
{
dialogText.text += letter;
letterCount++;
if (letterCount >= nextSoundTrigger)
{
TypeSound.Post(gameObject);
letterCount = 0;
nextSoundTrigger = Random.Range(3, 8); // Choose new interval
}
yield return new WaitForSeconds(0.02f);
}
dialogCoroutine = null;
}
public void HideDialog()
{
dialogPanel.SetActive(false);
playerMovement.moveAllowed = true;
}
}