StoryController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using KairoEngine.Core;
  6. using Ink.Runtime;
  7. using UniRx;
  8. namespace KairoEngine.StorySystem
  9. {
  10. // Todo: Request events for getting and setting story variables
  11. [System.Serializable]
  12. public class StoryController : System.Object
  13. {
  14. private TextAsset inkJSONAsset = null;
  15. private Story story;
  16. private string name;
  17. private bool logText = false;
  18. private bool started = false;
  19. private int lineInverval = 250;
  20. public bool wait = false;
  21. private CompositeDisposable disposables = new CompositeDisposable();
  22. private List<StoryObject> storyObjects;
  23. public StoryController(TextAsset inkJSONAsset, string storyName, bool logText = false)
  24. {
  25. this.inkJSONAsset = inkJSONAsset;
  26. this.name = storyName;
  27. this.story = new Story (inkJSONAsset.text);
  28. this.logText = logText;
  29. }
  30. public StoryController(TextAsset inkJSONAsset, string storyName, List<StoryObject> storyObjects, Transform parent = null, bool logText = false, int lineInverval = 250)
  31. {
  32. this.inkJSONAsset = inkJSONAsset;
  33. this.name = storyName;
  34. this.story = new Story (inkJSONAsset.text);
  35. this.logText = logText;
  36. this.lineInverval = lineInverval;
  37. this.storyObjects = storyObjects;
  38. foreach (var item in storyObjects)
  39. {
  40. item.instance = GameObject.Instantiate(item.prefab, parent);
  41. item.instance.SetActive(false);
  42. item.instance.name = item.instance.name.Replace("(Clone)", "");
  43. }
  44. }
  45. public void Start()
  46. {
  47. if(started) return;
  48. started = true;
  49. GenericEvents.StartListening(name, JumpToPath);
  50. EventManager.broadcast.StartListening(name, SelectStoryBranch);
  51. EventManager.broadcast.StartListening(name, PublishStorylineToObservers);
  52. if(logText) Debug.Log($"Starting {name}");
  53. story.BindExternalFunction ("WaitForTime", (int time) => { Wait(time); if(logText) Debug.Log($"Waiting {time}"); }, false);
  54. story.BindExternalFunction ("Enable", (string name) => { EnableGameObject(name); if(logText) Debug.Log($"Enabling {name}"); }, false);
  55. story.BindExternalFunction ("Disable", (string name) => { DisableGameObject(name); if(logText) Debug.Log($"Disabling {name}"); }, false);
  56. story.BindExternalFunction ("TriggerEvent", (string name) => { GenericEvents.Trigger(name); if(logText) Debug.Log($"Triggering event {name}"); }, false);
  57. var storyStream = Observable.Interval(TimeSpan.FromMilliseconds(250))
  58. //.Where(_ => wait == false)
  59. .Subscribe(_ => StoryLoop())
  60. .AddTo(disposables);
  61. }
  62. public void Stop()
  63. {
  64. GenericEvents.StopListening(name, JumpToPath);
  65. EventManager.broadcast.StopListening(name, SelectStoryBranch);
  66. EventManager.broadcast.StopListening(name, PublishStorylineToObservers);
  67. disposables.Clear();
  68. started = false;
  69. if(logText) Debug.Log($"{name} has stoped.");
  70. }
  71. private void StoryLoop()
  72. {
  73. //Debug.Log(wait);
  74. if(wait) return;
  75. // Read all the content until we can't continue any more
  76. if (story.canContinue) {
  77. // Continue gets the next line of the story
  78. string text = story.Continue ();
  79. // This removes any white space from the text.
  80. text = text.Trim();
  81. // Create StoryStepData
  82. StoryStepData storyStep = new StoryStepData(StoryStepType.Line, text, story.currentTags, null);
  83. EventManager.broadcast.Trigger(name, storyStep);
  84. // Display the text if log is enabled
  85. if(logText) Debug.Log(storyStep.text);
  86. }
  87. else if( story.currentChoices.Count > 0 )
  88. {
  89. List<string> choices = new List<string>();
  90. for (int i = 0; i < story.currentChoices.Count; ++i)
  91. {
  92. Choice choice = story.currentChoices [i];
  93. choices.Add(choice.text);
  94. if(logText) Debug.Log($"Choice {i + 1}. {choice.text}");
  95. }
  96. StoryStepData storyStep = new StoryStepData(StoryStepType.Branch, "", story.currentTags, choices);
  97. EventManager.broadcast.Trigger(name, storyStep);
  98. wait = true;
  99. }
  100. else Stop();
  101. }
  102. private void SelectStoryBranch(StoryStepData storyStep)
  103. {
  104. if(storyStep.category == StoryStepType.Path)
  105. {
  106. story.ChooseChoiceIndex (storyStep.path);
  107. wait = false;
  108. StoryLoop();
  109. }
  110. }
  111. static Subject<StoryStepData> subject;
  112. public static IObservable<StoryStepData> Lines()
  113. {
  114. if(subject == null) subject = new Subject<StoryStepData>();
  115. return subject.AsObservable();
  116. }
  117. private void PublishStorylineToObservers(StoryStepData storyStep)
  118. {
  119. if(subject != null) subject.OnNext(storyStep);
  120. }
  121. public void JumpToPath(string path)
  122. {
  123. if(story != null)
  124. {
  125. if(logText) Debug.Log($"Jumping to story knot \"{path}\"");
  126. story.ChoosePathString(path);
  127. wait = false;
  128. }
  129. }
  130. #region ExternalStoryFunctions
  131. private void Wait(int time)
  132. {
  133. wait = true;
  134. Timer.Execute(time, () => wait = false);
  135. }
  136. private void EnableGameObject(string name)
  137. {
  138. for (int i = 0; i < storyObjects.Count; i++)
  139. {
  140. if(storyObjects[i].name == name) storyObjects[i].instance.SetActive(true);
  141. }
  142. }
  143. private void DisableGameObject(string name)
  144. {
  145. for (int i = 0; i < storyObjects.Count; i++)
  146. {
  147. if(storyObjects[i].name == name) storyObjects[i].instance.SetActive(false);
  148. }
  149. }
  150. #endregion
  151. }
  152. }