105 lines
2.7 KiB
C#
105 lines
2.7 KiB
C#
using System.Collections;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using Event = AK.Wwise.Event;
|
|
|
|
public class DialogManager : MonoBehaviour
|
|
{
|
|
public static DialogManager Instance;
|
|
|
|
public GameObject dialogPanel;
|
|
public TextMeshProUGUI dialogText; // Use TextMeshProUGUI instead of Text
|
|
public PlayerMovement playerMovement;
|
|
[SerializeField] private Event TypeSound;
|
|
|
|
private Coroutine dialogCoroutine;
|
|
private Animator player;
|
|
private Animator quester;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject); // Ensure only one instance exists
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
dialogPanel.SetActive(false); // Hide on start
|
|
}
|
|
|
|
public void ShowDialog(string speaker, string message, Animator player, Animator quester)
|
|
{
|
|
if (player != null)
|
|
{
|
|
player.SetBool("Talking", true);
|
|
this.player = player;
|
|
}
|
|
|
|
if (quester != null)
|
|
{
|
|
quester.SetBool("Talking", true);
|
|
this.quester = quester;
|
|
}
|
|
|
|
print("should be showing dialog");
|
|
if (dialogCoroutine != null) StopCoroutine(dialogCoroutine);
|
|
dialogCoroutine = StartCoroutine(ShowDialogCoroutine(speaker, 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 speaker, string message)
|
|
{
|
|
dialogText.text = speaker; // 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()
|
|
{
|
|
if (player != null)
|
|
{
|
|
player.SetBool("Talking", false);
|
|
player = null;
|
|
}
|
|
|
|
if (quester != null)
|
|
{
|
|
quester.SetBool("Talking", false);
|
|
quester = null;
|
|
}
|
|
|
|
dialogPanel.SetActive(false);
|
|
playerMovement.moveAllowed = true;
|
|
}
|
|
} |