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 { private GameObject playerPanel; private GameObject otherPanel; private TextMeshProUGUI mainText; public Queue> textBuffer = new(); public float charactersPerSecond = 90; [CanBeNull] private string fulltext; private Coroutine ccoroutine; public string Status = "TALKING"; void Start() { playerPanel = transform.FindChildRecursive("PLAYERPANEL").gameObject; otherPanel = transform.FindChildRecursive("OTHERPANEL").gameObject; mainText = transform.FindChildRecursive("TEXTPANEL").GetComponentInChildren(); gameObject.SetActive(false); } public void HandlePress() { if (fulltext != null) { StopCoroutine(ccoroutine); mainText.text = fulltext; fulltext = null; } else if (textBuffer.Count > 0) { NextText(); } else { gameObject.SetActive(false); } } public void StartText(KeyValuePair[] buffer) { Status = "TALKING"; gameObject.SetActive(true); foreach (var kvp in buffer) { textBuffer.Enqueue(kvp); } NextText(); } private void NextText() { var item = textBuffer.Dequeue(); ccoroutine = StartCoroutine(TypeTextUncapped(item.Key)); var speaker = item.Value; 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) { textBuffer += chars[i]; mainText.text = textBuffer; timer += interval; i++; } else { timer -= Time.deltaTime; yield return null; } } Status = fulltext.Length > 30 ? "DONELONG" : "DONE"; fulltext = null; } }