Import TMPro. Add init vers of question formulation

This commit is contained in:
WillJeynes
2026-08-24 16:09:49 +01:00
parent 641564b461
commit f4e5de25c5
91 changed files with 64672 additions and 16 deletions
+136 -2
View File
@@ -1,16 +1,150 @@
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 : MonoBehaviour
public class DialogManager : Singleton<DialogManager>
{
private int currentGuard = 0;
private JObject dialogTree;
[CanBeNull] private JObject currentItem;
public GameObject QuestionOptionPrefab;
public GameObject QuestionInfillPrefab;
public GameObject AskButton;
private Transform QuestionOptionArea;
private Transform QuestionInfillArea;
private string FullQuestion;
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()
{
}
[ContextMenu("Debug Something")]
private void DebugSpeech()
{
StartDialog(0);
}
public void StartDialog(int 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")
{
currentItem = dialogTree;
PopulateQuestionOptions();
}
else
{
PopulateSingleQuestionOption(kvp);
}
}
}
private void PopulateSingleQuestionOption(KeyValuePair<string, JToken> kvp)
{
var option = Instantiate(QuestionOptionPrefab, QuestionOptionArea);
option.GetComponentInChildren<TextMeshProUGUI>().text = kvp.Key;
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;
questionStack.Push((item, parent, infil));
}
public void BackButtonPress()
{
AskButton.SetActive(false);
var (_, parent, infil) = questionStack.Pop();
currentItem = parent;
Destroy(infil);
PopulateQuestionOptions();
}
public void SubmitButtonPress()
{
Debug.LogError(FullQuestion);
}
}
+4 -3
View File
@@ -19,6 +19,8 @@ public class LevelLoader : Singleton<LevelLoader>
public List<DoorManager> Doors = new List<DoorManager>();
public List<GuardManager> Guards = new List<GuardManager>();
public JObject SpeechTree { private set; get; }
void Start()
{
@@ -56,9 +58,8 @@ public class LevelLoader : Singleton<LevelLoader>
}
//dynamic dialog dree
var speechTree = JObject.Parse(levels[0].text)["dialog"] as JObject;
//dynamic dialog tree
SpeechTree = JObject.Parse(levels[0].text)["dialog"] as JObject;
}
@@ -0,0 +1,23 @@
using UnityEngine;
namespace Utils
{
public static class GameObjectExtensions
{
public static Transform FindChildRecursive(this Transform parent, string name)
{
foreach (Transform child in parent)
{
if (child.name == name)
return child;
var result = FindChildRecursive(child, name);
if (result != null)
return result;
}
return null;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8d96797c677e440ea5272e3362ae9c30
timeCreated: 1787578078