using System.Collections.Generic; using PrimeTween; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.SceneManagement; using UnityEngine.UI; public class VNManager : MonoBehaviour { [SerializeField] TextAsset scriptFile; [SerializeField] TextMeshProUGUI speakerText; [SerializeField] GameObject speakerSprite; [SerializeField] TextMeshProUGUI dialogueText; [SerializeField] private GameObject choiceButtonPrefab; [SerializeField] private Transform choiceButtonContainer; [SerializeField] private Image choiceBackground; [SerializeField] float charsPerSecond = 45f; public VNDirector director; private bool isChoiceAvailable = false; private Tween dialogueTween; private Script _currentScript; public static string NextScriptPath = ""; void Start() { speakerText.SetText(" "); speakerText.ForceMeshUpdate(true); dialogueText.SetText(" "); dialogueText.ForceMeshUpdate(true); if (!string.IsNullOrEmpty(NextScriptPath)) { TextAsset loadedScript = Resources.Load($"NovelScripts/{NextScriptPath}"); if (loadedScript != null) { _currentScript = Parser.Parse(loadedScript.text); NextScriptPath = ""; } else { Debug.LogError($"ScriptManager :: Cannot find script: {NextScriptPath}"); _currentScript = Parser.Parse(scriptFile.text); } } else { _currentScript = Parser.Parse(scriptFile.text); } NextStep(); } void Update() { DisplayEffects(dialogueText); if (!isChoiceAvailable && !IsPointerOverInteractiveUI() && (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))) { if (dialogueTween.isAlive) { director.CompleteAllActions(); dialogueTween.Complete(); } else NextStep(); } } private void NextStep() { if (_currentScript.HasNextCommand()) { Command command = _currentScript.Continue(); Execute(command); return; } Debug.Log("ScriptManager :: End of Script"); } private void Execute(Command command) { switch (command.Type) { case "label": Debug.Log($"ScriptManager :: Change Label: {command.GetParam("content")}"); NextStep(); return; case "bg": Debug.Log($"ScriptManager :: Change Background: {command.GetParam("file")}"); NextStep(); return; case "char": director.AddCharacter(command.GetParam("img"), command.GetParam("enter").ToLower()); Debug.Log($"ScriptManager :: Character: {command.GetParam("img")}"); NextStep(); return; case "remove": director.RemoveCharacter(command.GetParam("target"), command.GetParam("exit").ToLower()); Debug.Log($"ScriptManager :: Remove Character: {command.GetParam("target")} to {command.GetParam("exit").ToLower()}"); NextStep(); return; case "action": director.PlayAction(command.GetParam("target"), command.GetParam("anim").ToLower()); Debug.Log($"ScriptManager :: Action: {command.GetParam("target")} {command.GetParam("anim").ToLower()}"); NextStep(); return; case "expr": director.ChangeExpression(command.GetParam("target"), command.GetParam("expr").ToLower()); Debug.Log($"ScriptManager :: Expression: {command.GetParam("target")} {command.GetParam("expr").ToLower()}"); NextStep(); return; case "spk": if (speakerSprite.activeSelf == false) speakerSprite.SetActive(true); if (command.GetParam("name") == "") speakerSprite.SetActive(false); string speaker = Store.Instance.ReplaceVariables(command.GetParam("name")); Debug.Log($"ScriptManager :: Speaker: {speaker}"); speakerText.SetText(speaker); speakerText.ForceMeshUpdate(true); NextStep(); return; case "msg": string dialogue = command.GetParam("content"); dialogue = Store.Instance.ReplaceVariables(dialogue); DisplayDialogue(dialogue); if (_currentScript.PeekNext()?.Type == "choices") { NextStep(); } return; case "goto": string targetLabel = command.GetParam("content"); _currentScript.JumpTo(targetLabel); NextStep(); return; case "choices": Debug.Log("ScriptManager :: Show Choices"); isChoiceAvailable = true; // WTF.. is this shit Color tempColor = choiceBackground.color; tempColor.a = 0.8f; choiceBackground.color = tempColor; foreach (var choice in command.Choices) { string text = Store.Instance.ReplaceVariables(choice["content"]); string target = choice["goto"]; GameObject buttonObj = Instantiate(choiceButtonPrefab, choiceButtonContainer); buttonObj.GetComponentInChildren().text = text; buttonObj .GetComponent