Add speech management

This commit is contained in:
WillJeynes
2026-08-24 18:50:49 +01:00
parent 7fa82ce8ec
commit 3396234653
5 changed files with 899 additions and 3 deletions
+32 -2
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using DefaultNamespace;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
using TMPro;
@@ -37,7 +38,8 @@ public class DialogManager : Singleton<DialogManager>
{
QuestionOptionArea = transform.FindChildRecursive("ADDQOPTIONSHERE");
QuestionInfillArea = transform.FindChildRecursive("ADDQINFILSHERE");
gameObject.SetActive(false);
}
// Update is called once per frame
@@ -56,6 +58,7 @@ public class DialogManager : Singleton<DialogManager>
//public interface to start interaction
public void StartDialog(GuardManager guardId)
{
gameObject.SetActive(true);
dialogTree = LevelLoader.Instance.SpeechTree;
currentGuard = guardId;
@@ -143,6 +146,12 @@ public class DialogManager : Singleton<DialogManager>
public void BackButtonPress()
{
if (questionStack.Count == 0)
{
reset();
return;
}
AskButton.SetActive(false);
var (item, parent, infil) = questionStack.Pop();
currentItem = parent;
@@ -172,12 +181,14 @@ public class DialogManager : Singleton<DialogManager>
Debug.LogError(FullQuestion);
string q = "";
string r = "";
while (questionStack.Count > 0)
{
var (item, _, go) = questionStack.Pop();
Destroy(go);
q += item;
r = item + " " + r;
if (item.StartsWith("guard "))
{
FullQuestion = FullQuestion.ReplaceLast("GS", "G" + (int.Parse(item[^1].ToString())-1));
@@ -194,11 +205,30 @@ public class DialogManager : Singleton<DialogManager>
var response = currentGuard.Answer(FullQuestion);
Debug.Log(response);
r = r.Replace("NOREC_", "");
string responsePrefix = "";
if (FullQuestion.Contains("%"))
{
responsePrefix = "They would answer: ";
}
SpeechManager.Instance.StartText(new[]
{
new KeyValuePair<string, string>(r, "PLAYER"),
new KeyValuePair<string, string>(responsePrefix + response.ToRandomString(), "GUARD")
});
reset();
}
private void reset()
{
//reset for next question
currentItem = dialogTree;
recursiveDepth = 0;
PopulateQuestionOptions();
gameObject.SetActive(false);
}
}
+42
View File
@@ -1,3 +1,5 @@
using UnityEngine;
namespace DefaultNamespace
{
public enum QuestionAnswer
@@ -6,4 +8,44 @@ namespace DefaultNamespace
NO,
NO_YES,
}
public static class QuestionAnswerExtensions
{
public static string ToRandomString(this QuestionAnswer answer)
{
string[] options = answer switch
{
QuestionAnswer.YES => new[]
{
"Yes",
"Absolutely",
"Affirmative",
"Definitely",
"Of course",
"Sure",
},
QuestionAnswer.NO => new[]
{
"No",
"Absolutely not",
"Negative",
"Definitely not",
"Nope",
"Not at all",
},
QuestionAnswer.NO_YES => new[]
{
"I cannot answer that question",
"I'm not sure",
"I don't know",
},
_ => new[] { answer.ToString() }
};
return options[Random.Range(0, options.Length)];
}
}
}
+114
View File
@@ -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;
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 486ec28e163f4b0595905be63e55c07f
timeCreated: 1787590550