Add speech management
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
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;
|
||||
|
||||
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;
|
||||
fulltext = null;
|
||||
}
|
||||
else if (textBuffer.Count > 0)
|
||||
{
|
||||
NextText();
|
||||
}
|
||||
else
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
fulltext = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user