Files
the-peoples-messenger/Assets/Scripts/SpeechManager.cs
T
2026-08-27 12:54:16 +01:00

148 lines
3.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Utils;
public class SpeechManager : Singleton<SpeechManager>
{
private GameObject playerPanel;
private GameObject otherPanel;
private TextMeshProUGUI mainText;
public Queue<KeyValuePair<string,string>> textBuffer = new();
public float charactersPerSecond = 90;
[CanBeNull] private string fulltext;
private Coroutine ccoroutine;
private string speaker;
public string Status = "TALKING";
private int counter = 0;
void Start()
{
playerPanel = transform.FindChildRecursive("PLAYERPANEL").gameObject;
otherPanel = transform.FindChildRecursive("OTHERPANEL").gameObject;
mainText = transform.FindChildRecursive("TEXTPANEL").GetComponentInChildren<TextMeshProUGUI>();
gameObject.SetActive(false);
}
public void HandlePress()
{
if (fulltext != null)
{
StopCoroutine(ccoroutine);
mainText.text = fulltext;
Status = fulltext.Length > 30 ? "DONELONG" : "DONE";
fulltext = null;
}
else if (textBuffer.Count > 0)
{
NextText();
}
else
{
AudioManager.Instance.PlaySound("Select");
gameObject.SetActive(false);
if (counter++ > 0 && LevelLoader.Instance.GlobalModifier == "CUP")
{
CupShuffle.Instance.StartShuffle();
}
}
}
public void StartText(KeyValuePair<string,string>[] buffer)
{
gameObject.SetActive(true);
foreach (var kvp in buffer)
{
textBuffer.Enqueue(kvp);
}
NextText();
}
private void NextText()
{
var item = textBuffer.Dequeue();
speaker = item.Value;
if (LevelLoader.Instance.GlobalModifier == "HORSE" && speaker == "GUARD")
{
Debug.LogError("HORSE");
mainText.text = item.Key;
playerPanel.SetActive(false);
otherPanel.SetActive(false);
AudioManager.Instance.PlaySound("Horse");
return;
}
Status = "TALKING";
AudioManager.Instance.PlaySound("Select");
ccoroutine = StartCoroutine(TypeTextUncapped(item.Key));
if (speaker == "PLAYER")
{
playerPanel.SetActive(true);
otherPanel.SetActive(false);
}
else if (speaker == "GUARD")
{
playerPanel.SetActive(false);
otherPanel.SetActive(true);
}
else
{
throw new NotImplementedException();
}
}
IEnumerator TypeTextUncapped(string line)
{
fulltext = line;
float timer = 0;
float interval = 1 / charactersPerSecond;
string textBuffer = null;
char[] chars = line.ToCharArray();
int i = 0;
while (i < chars.Length)
{
if (timer < Time.deltaTime)
{
AudioManager.Instance.PlaySpeechSound(speaker != "PLAYER");
textBuffer += chars[i];
mainText.text = textBuffer;
timer += interval;
i++;
}
else
{
timer -= Time.deltaTime;
yield return null;
}
}
Status = fulltext.Length > 30 ? "DONELONG" : "DONE";
fulltext = null;
}
}