using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Timeline; using UnityEngine.Localization; using UnityEditor.Localization; using UnityEditor; using UnityEngine.Localization.Settings; using UnityEngine.Localization.Metadata; public enum DialogueType { StartDialogue, CompletionDialogue, IncompletionDialogue, DefaultDialogue, } public enum ChoiceActionType { DoNothing, ContinueWithStep, WinningChoice, LosingChoice, IncompleteStep } /// /// A Dialogue is a list of consecutive DialogueLines. They play in sequence using the input of the player to skip forward. /// In future versions it might contain support for branching conversations. /// [CreateAssetMenu(fileName = "new Dialogue", menuName = "Dialogues/Dialogue Data")] public class DialogueDataSO : ScriptableObject { [SerializeField] private List _lines = default; [SerializeField] private DialogueType _dialogueType = default; [SerializeField] private VoidEventChannelSO _endOfDialogueEvent = default; public VoidEventChannelSO EndOfDialogueEvent => _endOfDialogueEvent; public List _Lines => _lines; public DialogueType DialogueType { get { return _dialogueType; } set { _dialogueType = value; } } public void SetActor(ActorSO newActor) { //_actor = newActor; } public DialogueDataSO() { } public DialogueDataSO(string dialogueName) { SetDialogueLines(dialogueName); } public DialogueDataSO(DialogueDataSO dialogue) { /* _actor = dialogue.Actor; _dialogueLines = new List(dialogue.DialogueLines); _choices = new List(); _endOfDialogueEvent = dialogue.EndOfDialogueEvent; if (dialogue.Choices != null) for (int i = 0; i < dialogue.Choices.Count; i++) { _choices.Add(new Choice(dialogue.Choices[i])); } _dialogueType = dialogue.DialogueType; */ } public void FinishDialogue() { if (EndOfDialogueEvent != null) EndOfDialogueEvent.RaiseEvent(); } #if UNITY_EDITOR private void OnEnable() { SetDialogueLines(this.name); } void SetDialogueLines(string dialogueName) { if (_lines == null) _lines = new List(); _lines.Clear(); int dialogueIndex = 0; Line _dialogueLine = new Line(); do { dialogueIndex++; _dialogueLine = new Line("D" + dialogueIndex + "-" + dialogueName); if (_dialogueLine.TextList != null) _lines.Add(_dialogueLine); } while (_dialogueLine.TextList != null); } /// /// This function is only useful for the Questline Tool in Editor to remove a Questline /// /// The local path public string GetPath() { return AssetDatabase.GetAssetPath(this); } #endif } [Serializable] public class Choice { [SerializeField] private LocalizedString _response = default; [SerializeField] private DialogueDataSO _nextDialogue = default; [SerializeField] private ChoiceActionType _actionType = default; public LocalizedString Response => _response; public DialogueDataSO NextDialogue => _nextDialogue; public ChoiceActionType ActionType => _actionType; public void SetNextDialogue(DialogueDataSO dialogue) { _nextDialogue = dialogue; } public Choice() { } public Choice(Choice choice) { _response = choice.Response; _nextDialogue = choice.NextDialogue; _actionType = ActionType; } public Choice(LocalizedString response) { _response = response; } public void SetChoiceAction(Comment comment) { _actionType = (ChoiceActionType)Enum.Parse(typeof(ChoiceActionType), comment.CommentText); } } [Serializable] public class Line { [SerializeField] private ActorID _actorID = default; [SerializeField] private List _textList = default; [SerializeField] private List _choices = default; public ActorID Actor => _actorID; public List TextList => _textList; public List Choices => _choices; public Line() { _textList = null; } public void SetActor(Comment comment) { _actorID = (ActorID)Enum.Parse(typeof(ActorID), comment.CommentText); } public Line(string _name) { StringTableCollection collection = LocalizationEditorSettings.GetStringTableCollection("Questline Dialogue"); _textList = null; if (collection != null) { int lineIndex = 0; LocalizedString _dialogueLine = null; do { lineIndex++; string key = "L" + lineIndex + "-" + _name; if (collection.SharedData.Contains(key)) { SetActor(collection.SharedData.GetEntry(key).Metadata.GetMetadata()); _dialogueLine = new LocalizedString() { TableReference = "Questline Dialogue", TableEntryReference = key }; if (_textList == null) _textList = new List(); _textList.Add(_dialogueLine); } else { _dialogueLine = null; } } while (_dialogueLine != null); int choiceIndex = 0; Choice choice = null; do { choiceIndex++; string key = "C" + choiceIndex + "-" + _name; if (collection.SharedData.Contains(key)) { LocalizedString _choiceLine = new LocalizedString() { TableReference = "Questline Dialogue", TableEntryReference = key }; choice = new Choice(_choiceLine); choice.SetChoiceAction(collection.SharedData.GetEntry(key).Metadata.GetMetadata()); if (_choices == null) { _choices = new List(); } _choices.Add(choice); } else { choice = null; } } while (choice != null); } else { _textList = null; } } }