129 lines
3.0 KiB
C#
129 lines
3.0 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";
|
|
|
|
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
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
public void StartText(KeyValuePair<string,string>[] buffer)
|
|
{
|
|
Status = "TALKING";
|
|
gameObject.SetActive(true);
|
|
foreach (var kvp in buffer)
|
|
{
|
|
textBuffer.Enqueue(kvp);
|
|
}
|
|
|
|
NextText();
|
|
}
|
|
|
|
private void NextText()
|
|
{
|
|
AudioManager.Instance.PlaySound("Select");
|
|
var item = textBuffer.Dequeue();
|
|
ccoroutine = StartCoroutine(TypeTextUncapped(item.Key));
|
|
|
|
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)
|
|
{
|
|
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;
|
|
|
|
}
|
|
} |