Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Scripts / Dialog / DialogController.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class DialogController : MonoBehaviour
  5. {
  6. public List<DialogueMessage> dialogs = new List<DialogueMessage>();
  7. private Queue<DialogueMessage> dialogsQueue = new Queue<DialogueMessage>();
  8. public DialogueMessage currentDialogue { get; private set; }
  9. public bool dialogFinished { get { return dialogState == DialogState.Finished; }}
  10. public DialogState dialogState { get; private set; }
  11. public delegate void DialogCallback(DialogueMessage msg, DialogController controller);
  12. public static event DialogCallback dialogSkipped;
  13. public delegate void DialogStateCallback(DialogState state,DialogController controller);
  14. public static event DialogStateCallback dialogStateChanged;
  15. public enum DialogState
  16. {
  17. NotStated,
  18. Started,
  19. Finished
  20. }
  21. void Start()
  22. {
  23. Talk();
  24. }
  25. public void NextDialogue()
  26. {
  27. if (dialogsQueue.Count > 0)
  28. {
  29. DoDialogue(dialogsQueue.Dequeue());
  30. }
  31. }
  32. private void DoDialogue(DialogueMessage content)
  33. {
  34. TriggerStateChange(DialogState.Started);
  35. currentDialogue = content;
  36. TriggerNextDialog(content);
  37. if (dialogsQueue.Count == 0)
  38. {
  39. TriggerStateChange(DialogState.Finished);
  40. }
  41. }
  42. public void SayDialogue()
  43. {
  44. if (dialogsQueue.Count > 0)
  45. {
  46. DoDialogue(dialogsQueue.Peek());
  47. }
  48. }
  49. public void Talk()
  50. {
  51. dialogsQueue.Clear();
  52. if (dialogs.Count > 0)
  53. {
  54. for (int i = dialogs.Count - 1; i >= 0; i--)
  55. {
  56. dialogsQueue.Enqueue(dialogs[i]);
  57. }
  58. }
  59. }
  60. public static void Talk(DialogueMessage text)
  61. {
  62. if (dialogSkipped != null)
  63. {
  64. dialogSkipped(text, null);
  65. }
  66. }
  67. public void End()
  68. {
  69. dialogsQueue.Clear();
  70. TriggerStateChange(DialogState.Finished);
  71. currentDialogue = new DialogueMessage();
  72. if (dialogSkipped != null)
  73. {
  74. dialogSkipped(currentDialogue, this);
  75. }
  76. }
  77. void TriggerStateChange(DialogState newState)
  78. {
  79. if (dialogState != newState)
  80. {
  81. dialogState = newState;
  82. if (dialogStateChanged != null)
  83. {
  84. dialogStateChanged(newState, this);
  85. }
  86. }
  87. }
  88. void TriggerNextDialog(DialogueMessage txt)
  89. {
  90. if (dialogSkipped != null)
  91. {
  92. dialogSkipped(txt,this);
  93. }
  94. }
  95. }