Files
the-peoples-messenger/Assets/Scripts/DialogManager.cs
T
2026-08-29 21:20:04 +01:00

319 lines
9.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using DefaultNamespace;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using TMPro;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;
using Utils;
public class DialogManager : Singleton<DialogManager>
{
private GuardManager currentGuard;
public JObject dialogTree;
[CanBeNull] private JObject currentItem;
public GameObject QuestionOptionPrefab;
public GameObject QuestionInfillPrefab;
public GameObject AskButton;
public Transform QuestionOptionArea;
public Transform QuestionInfillArea;
private Stack<string> FullQuestionPrefixes = new Stack<string>();
private string FullQuestion;
private int recursiveDepth = 0;
public int MaxRecursiveDepth = 1;
private Stack<(string key, JObject parent, GameObject infil)> questionStack = new();
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
QuestionOptionArea = transform.FindChildRecursive("ADDQOPTIONSHERE");
QuestionInfillArea = transform.FindChildRecursive("ADDQINFILSHERE");
gameObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
}
//start from right click
[ContextMenu("Debug Questioning")]
private void DebugSpeech()
{
StartDialog(LevelLoader.Instance.Guards[0]);
}
//public interface to start interaction
public void StartDialog(GuardManager guardId)
{
gameObject.SetActive(true);
AskButton.SetActive(false);
QuestionOptionArea = transform.FindChildRecursive("ADDQOPTIONSHERE");
if (!GlobalStateManager.Instance.finalScene)
{
dialogTree = LevelLoader.Instance.SpeechTree;
}
currentGuard = guardId;
currentItem = dialogTree;
PopulateQuestionOptions();
}
private void PopulateQuestionOptions()
{
//clear options
foreach (Transform child in QuestionOptionArea)
{
Destroy(child.gameObject);
}
if (currentItem == null)
{
return;
}
foreach (var kvp in currentItem)
{
Debug.Log(kvp.ToString());
//special keywords
if (kvp.Key == "DOORS")
{
foreach (var man in LevelLoader.Instance.Doors)
{
PopulateSingleQuestionOption(new KeyValuePair<string, JToken>(man.GetUnknownDescription(), kvp.Value));
}
}
else if (kvp.Key.StartsWith("GUARDS"))
{
foreach (var man in LevelLoader.Instance.Guards)
{
string postfix = "";
if (man == currentGuard)
{
if (kvp.Key.EndsWith("YOU"))
{
postfix = " (you)";
}
else
{
//this lowkey breaks most puzzes cause double negative makes all tell truth
continue;
}
}
PopulateSingleQuestionOption(new KeyValuePair<string, JToken>(man.GetUnknownDescription() + postfix, kvp.Value));
}
}
else if (kvp.Key == "QUESTION")
{
recursiveDepth++;
FullQuestionPrefixes.Push((kvp.Value as JValue).ToString());
currentItem = dialogTree;
PopulateQuestionOptions();
}
else if (!kvp.Key.StartsWith("NOREC_") || recursiveDepth < MaxRecursiveDepth)
{
PopulateSingleQuestionOption(kvp);
}
}
}
private void PopulateSingleQuestionOption(KeyValuePair<string, JToken> kvp)
{
var option = Instantiate(QuestionOptionPrefab, QuestionOptionArea);
option.GetComponentInChildren<TextMeshProUGUI>().text = kvp.Key.Replace("NOREC_", "");
option.GetComponent<Button>().onClick.AddListener(() =>
{
AudioManager.Instance.PlaySound("WordAdd");
if (kvp.Value is JValue)
{
string addition = "?";
if (GlobalStateManager.Instance.finalScene)
{
addition = ".";
}
AddQuestionInfill(currentItem, kvp.Key + addition);
currentItem = null;
AskButton.SetActive(true);
FullQuestion = kvp.Value.ToString();
}
else if (kvp.Value is JObject value)
{
AddQuestionInfill(currentItem, kvp.Key);
currentItem = value;
}
PopulateQuestionOptions();
});
}
private void AddQuestionInfill(JObject parent, string item)
{
var infil = Instantiate(QuestionInfillPrefab, QuestionInfillArea);
infil.GetComponent<TextMeshProUGUI>().text = item.Replace("NOREC_", "");
questionStack.Push((item, parent, infil));
}
public void BackButtonPress()
{
AudioManager.Instance.PlaySound("WordRemove");
if (questionStack.Count == 0)
{
reset();
return;
}
AskButton.SetActive(false);
var (item, parent, infil) = questionStack.Pop();
currentItem = parent;
Destroy(infil);
//aaaaaaaaaaaaaaaaaaaaaaaa
//aaaaaaaaaaaaaaaaaaaaaaaa
if (parent.ContainsKey(item) && parent[item] is JObject && (parent[item] as JObject).ContainsKey("QUESTION"))
{
recursiveDepth--;
FullQuestionPrefixes.Pop();
}
PopulateQuestionOptions();
}
public void SubmitButtonPress()
{
AudioManager.Instance.PlaySound("Select");
var FullQuestionCopt = FullQuestion + "";
//formulate query string in format PERPSECTIVE%ITEM#PROPERTY
//slightly wack way of doing it, hoping it is future proof
while (FullQuestionPrefixes.Count > 0)
{
FullQuestion = FullQuestionPrefixes.Pop() + FullQuestion;
}
Debug.Log(FullQuestion);
string q = "";
string r = "";
while (questionStack.Count > 0)
{
var (item, _, go) = questionStack.Pop();
Destroy(go);
q += item;
r = item + " " + r;
int ig = LevelLoader.Instance.Guards.FindIndex(
guard => item.StartsWith(guard.GetUnknownDescription())
);
if (ig >= 0)
{
FullQuestion = FullQuestion.ReplaceLast("GS", "G" + ig);
}
int id = LevelLoader.Instance.Doors.FindIndex(
door => item.StartsWith(door.GetUnknownDescription())
);
if (id >= 0)
{
FullQuestion = FullQuestion.ReplaceLast("DS", "D" + id);
}
}
Debug.Log(FullQuestion);
r = r.Replace("NOREC_", "");
if (!r.Contains(":"))
{
r = Regex.Replace(
r,
@"is.*\(you\)",
"are you",
RegexOptions.IgnoreCase
);
}
var response = FullQuestionCopt;
if (FullQuestion == "RIDDLE")
{
response = currentGuard.puzzle;
}
else if (!FullQuestion.Contains("$"))
{
//now get a response from the guard being questioned
response = currentGuard.Answer(FullQuestion).ToRandomString();
}
var cdoor = LevelLoader.Instance.Doors.FirstOrDefault((d) => d.type == "LIFE");
if (cdoor != null)
{
response = response.Replace("CDOOR", cdoor.mod.Replace("#",""));
}
response = response.Replace("$", "");
string responsePrefix = "";
if (FullQuestion.Contains("%"))
{
responsePrefix = "They would answer: ";
}
var responder = "GUARD";
if (LevelLoader.Instance.GlobalModifier == "HORSE")
{
responder = "HORSE";
}
if (currentGuard.mod == "JOKER")
{
responder = "JOKER";
}
SpeechManager.Instance.StartText(new[]
{
new KeyValuePair<string, string>(r, "PLAYER"),
new KeyValuePair<string, string>(responsePrefix + response, responder)
});
reset();
}
private void reset()
{
//reset for next question
currentItem = dialogTree;
recursiveDepth = 0;
PopulateQuestionOptions();
gameObject.SetActive(false);
}
}