Files
the-peoples-messenger/Assets/Scripts/DialogManager.cs
T

205 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;
using Utils;
public class DialogManager : Singleton<DialogManager>
{
private GuardManager currentGuard;
private JObject dialogTree;
[CanBeNull] private JObject currentItem;
public GameObject QuestionOptionPrefab;
public GameObject QuestionInfillPrefab;
public GameObject AskButton;
private Transform QuestionOptionArea;
private 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");
}
// 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)
{
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)
{
//special keywords
if (kvp.Key == "DOORS")
{
for (int i = 0; i < LevelLoader.Instance.Doors.Count; i++)
{
PopulateSingleQuestionOption(new KeyValuePair<string, JToken>("door " + (i+1), kvp.Value));
}
}
else if (kvp.Key == "GUARDS")
{
for (int i = 0; i < LevelLoader.Instance.Guards.Count; i++)
{
PopulateSingleQuestionOption(new KeyValuePair<string, JToken>("guard " + (i+1), 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(() =>
{
if (kvp.Value is JValue)
{
AddQuestionInfill(currentItem, kvp.Key + "?");
Debug.LogError("ready");
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()
{
AskButton.SetActive(false);
var (item, parent, infil) = questionStack.Pop();
currentItem = parent;
Destroy(infil);
//this might be buggy, or might miraculuisly work
//if options start dissapearing randomly, look here
if (item.StartsWith("NOREC_"))
{
recursiveDepth--;
FullQuestionPrefixes.Pop();
}
PopulateQuestionOptions();
}
public void SubmitButtonPress()
{
//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.LogError(FullQuestion);
string q = "";
while (questionStack.Count > 0)
{
var (item, _, go) = questionStack.Pop();
Destroy(go);
q += item;
if (item.StartsWith("guard "))
{
FullQuestion = FullQuestion.ReplaceLast("GS", "G" + (int.Parse(item[^1].ToString())-1));
}
if (item.StartsWith("door "))
{
FullQuestion = FullQuestion.ReplaceLast("DS", "D" + (int.Parse(item[^1].ToString())-1));
}
}
Debug.LogError(q);
Debug.LogError(FullQuestion);
//now get a response from the guard being questioned
var response = currentGuard.Answer(FullQuestion);
Debug.Log(response);
//reset for next question
currentItem = dialogTree;
recursiveDepth = 0;
PopulateQuestionOptions();
}
}