using System; using System.Collections.Generic; using System.IO; using TMPro; using UnityEngine; using UnityEngine.UI; public class DialogueManager : MonoBehaviour { #region Statication public static DialogueManager instance; private void Awake() { if (instance != null && instance != this) { Destroy(gameObject); return; } instance = this; } #endregion private class Choice { public string choiceName; public DialogueScript choiceScript; } [Header("Player")] public Player player; [Header("Dialogue Script")] public DialogueScript currentDialogueScript; public DialogueScript[] allDialogue; public bool clickingDisabled = false; private int textProgress; public string[] scriptLines; [Header("UI")] public GameObject uiCanvas; public TextMeshProUGUI nameText; public TextMeshProUGUI dialogueText; public Button choiceButton; public Transform choicePanel; public void StartDialogue(DialogueScript scriptToShow) { uiCanvas.SetActive(true); textProgress = 0; currentDialogueScript = scriptToShow; scriptLines = currentDialogueScript.script.text.Split("\n"); player.abilitiesDisabled = true; player.stalled = true; UpdateDialogue(); } private void ContinueDialogue() { textProgress++; clickingDisabled = false; UpdateDialogue(); } private void UpdateDialogue() { string[] words = scriptLines[textProgress].Split(" "); switch (words[0].ToUpper()) { case "END": EndDialogue(); break; case "TEXT": foreach (DialogueCharacter character in currentDialogueScript.characters) { if (string.Equals(character.name, words[1], StringComparison.CurrentCultureIgnoreCase)) { nameText.text = character.name; string text = string.Join(" ", words[2..]); text = text.Replace("\"", ""); dialogueText.text = text; break; } } break; case "TASK": //must use 3 words on these. string taskModifier = words[1].ToUpper(); if (taskModifier == "ADD") { foreach (Task task in TaskManager.instance.taskList) { if (string.Equals(task.taskID, words[2], StringComparison.CurrentCultureIgnoreCase)) { TaskManager.instance.AddTask(task); break; } } } ContinueDialogue(); break; case "ITEM": string itemModifier = words[1].ToUpper(); if (itemModifier == "ADD") { foreach (Item item in InventoryManager.instance.itemList) { if (string.Equals(item.itemName, words[2], StringComparison.CurrentCultureIgnoreCase)) { InventoryManager.instance.AddItem(item, Int32.Parse(words[3])); break; } } } ContinueDialogue(); break; case "#": ContinueDialogue(); break; case "CHOICE": clickingDisabled = true; List choices = new(); for (int i = 1; i < words.Length; i++) { Choice newChoice = new Choice(); string[] c = words[i].Split(":"); newChoice.choiceName = c[0]; foreach (DialogueScript dialogueScript in allDialogue) { if (string.Equals(c[1], dialogueScript.dialogueID, StringComparison.CurrentCultureIgnoreCase)) { newChoice.choiceScript = dialogueScript; //this is the worst way ever to find a matching id but WHATEVER break; } } if (!newChoice.choiceScript) { Debug.LogWarning("BRO WTF THERE IS NO MATCHING ID!!!"); } else { choices.Add(newChoice); } } foreach (Choice possibleChoice in choices) { Button newChoiceButton = Instantiate(choiceButton, choicePanel); newChoiceButton.onClick.AddListener(() => StartDialogue(possibleChoice.choiceScript)); newChoiceButton.GetComponentInChildren().text = possibleChoice.choiceName; } break; } } public void EndDialogue() { //TaskManager.instance.AddTask(currentDialogueScript.taskToStart); uiCanvas.SetActive(false); currentDialogueScript = null; scriptLines = null; player.abilitiesDisabled = false; player.stalled = false; } private void Update() { if (currentDialogueScript && Input.GetMouseButtonDown(0)) { ContinueDialogue(); } } }