- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DialogController : MonoBehaviour
- {
- public List<DialogueMessage> dialogs = new List<DialogueMessage>();
- private Queue<DialogueMessage> dialogsQueue = new Queue<DialogueMessage>();
- public DialogueMessage currentDialogue { get; private set; }
- public bool dialogFinished { get { return dialogState == DialogState.Finished; }}
- public DialogState dialogState { get; private set; }
- public delegate void DialogCallback(DialogueMessage msg, DialogController controller);
- public static event DialogCallback dialogSkipped;
- public delegate void DialogStateCallback(DialogState state,DialogController controller);
- public static event DialogStateCallback dialogStateChanged;
- public enum DialogState
- {
- NotStated,
- Started,
- Finished
- }
-
- void Start()
- {
- Talk();
-
- }
- public void NextDialogue()
- {
- if (dialogsQueue.Count > 0)
- {
- DoDialogue(dialogsQueue.Dequeue());
- }
- }
- private void DoDialogue(DialogueMessage content)
- {
- TriggerStateChange(DialogState.Started);
- currentDialogue = content;
- TriggerNextDialog(content);
- if (dialogsQueue.Count == 0)
- {
- TriggerStateChange(DialogState.Finished);
- }
- }
-
- public void SayDialogue()
- {
- if (dialogsQueue.Count > 0)
- {
- DoDialogue(dialogsQueue.Peek());
- }
- }
- public void Talk()
- {
- dialogsQueue.Clear();
- if (dialogs.Count > 0)
- {
- for (int i = dialogs.Count - 1; i >= 0; i--)
- {
- dialogsQueue.Enqueue(dialogs[i]);
- }
- }
- }
- public static void Talk(DialogueMessage text)
- {
- if (dialogSkipped != null)
- {
- dialogSkipped(text, null);
- }
- }
- public void End()
- {
- dialogsQueue.Clear();
- TriggerStateChange(DialogState.Finished);
- currentDialogue = new DialogueMessage();
- if (dialogSkipped != null)
- {
- dialogSkipped(currentDialogue, this);
- }
- }
- void TriggerStateChange(DialogState newState)
- {
- if (dialogState != newState)
- {
- dialogState = newState;
- if (dialogStateChanged != null)
- {
- dialogStateChanged(newState, this);
- }
- }
- }
- void TriggerNextDialog(DialogueMessage txt)
- {
- if (dialogSkipped != null)
- {
- dialogSkipped(txt,this);
- }
- }
- }