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 readonly float shakeAmount = 1.1f; private bool isChoiceAvailable = false; private Tween dialogueTween; private Command _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.HasNextAction()) { CommandSet command = _currentScript.Continue(); Execute(command); return; } Debug.Log("ScriptManager :: End of Script"); } private void Execute(CommandSet command) { if (command.Type == "label") { string labelName = command.GetParam("content"); Debug.Log($"ScriptManager :: Change Label: {labelName}"); NextStep(); return; } if (command.Type == "bg") { string bgFile = command.GetParam("file"); Debug.Log($"ScriptManager :: Change Background: {bgFile}"); NextStep(); return; } if (command.Type == "char") { string charFile = command.GetParam("img"); if (string.IsNullOrEmpty(charFile)) { charFile = command.GetParam("target"); } string charEntrance = command.GetParam("enter"); if (charEntrance == "") charEntrance = "center"; if (charEntrance.ToLower() == "center") director.AddCharacter(charFile, VNDirector.EntranceType.Center); if (charEntrance.ToLower() == "top") director.AddCharacter(charFile, VNDirector.EntranceType.Top); if (charEntrance.ToLower() == "left") director.AddCharacter(charFile, VNDirector.EntranceType.Left); if (charEntrance.ToLower() == "right") director.AddCharacter(charFile, VNDirector.EntranceType.Right); if (charEntrance.ToLower() == "bottomleft") director.AddCharacter(charFile, VNDirector.EntranceType.BottomLeft); if (charEntrance.ToLower() == "bottomright") director.AddCharacter(charFile, VNDirector.EntranceType.BottomRight); if (charEntrance.ToLower() == "leftrun") director.AddCharacter(charFile, VNDirector.EntranceType.LeftRun); if (charEntrance.ToLower() == "rightrun") director.AddCharacter(charFile, VNDirector.EntranceType.RightRun); Debug.Log($"ScriptManager :: Character: {charFile}"); NextStep(); return; } if (command.Type == "remove") { string charName = command.GetParam("target"); string exitType = command.GetParam("exit"); if (exitType == "") exitType = "center"; VNDirector.EntranceType type = new(); if (exitType.ToLower() == "center") type = VNDirector.EntranceType.Center; if (exitType.ToLower() == "left") type = VNDirector.EntranceType.Left; if (exitType.ToLower() == "right") type = VNDirector.EntranceType.Right; if (exitType.ToLower() == "bottomleft") type = VNDirector.EntranceType.BottomLeft; if (exitType.ToLower() == "bottomright") type = VNDirector.EntranceType.BottomRight; if (exitType.ToLower() == "top") type = VNDirector.EntranceType.Top; if (exitType.ToLower() == "leftrun") type = VNDirector.EntranceType.LeftRun; if (exitType.ToLower() == "rightrun") type = VNDirector.EntranceType.RightRun; director.RemoveCharacter(charName, type); Debug.Log($"ScriptManager :: Remove Character: {charName} to {exitType}"); NextStep(); return; } if (command.Type == "action") { string charName = command.GetParam("target"); string charAnim = command.GetParam("anim"); if (charAnim == "") charAnim = "center"; if (charAnim.ToLower() == "jump") director.PlayAction(charName, VNDirector.ActionType.Jump); if (charAnim.ToLower() == "shake") director.PlayAction(charName, VNDirector.ActionType.Shake); if (charAnim.ToLower() == "run") director.PlayAction(charName, VNDirector.ActionType.Run); if (charAnim.ToLower() == "nod") director.PlayAction(charName, VNDirector.ActionType.Nod); if (charAnim.ToLower() == "punch") director.PlayAction(charName, VNDirector.ActionType.Punch); Debug.Log($"ScriptManager :: Action: {charName} {charAnim}"); NextStep(); return; } if (command.Type == "expr") { string charName = command.GetParam("target"); string charExpr = command.GetParam("expr"); director.ChangeExpression(charName, charExpr); Debug.Log($"ScriptManager :: Expression: {charName} {charExpr}"); NextStep(); return; } if (command.Type == "spk") { string speaker = command.GetParam("name"); if (speakerSprite.activeSelf == false) speakerSprite.SetActive(true); if (speaker == "") speakerSprite.SetActive(false); speaker = Store.Instance.ReplaceVariables(speaker); Debug.Log($"ScriptManager :: Speaker: {speaker}"); speakerText.SetText(speaker); speakerText.ForceMeshUpdate(true); NextStep(); return; } if (command.Type == "msg") { string dialogue = command.GetParam("content"); dialogue = Store.Instance.ReplaceVariables(dialogue); DisplayDialogue(dialogue); if (_currentScript.PeekNext()?.Type == "choices") { NextStep(); } return; } if (command.Type == "goto") { string targetLabel = command.GetParam("content"); _currentScript.JumpTo(targetLabel); NextStep(); return; } if (command.Type == "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