Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Ink / InkLibs / InkRuntime / Flow.cs
  1. using System.Collections.Generic;
  2. namespace Ink.Runtime
  3. {
  4. public class Flow {
  5. public string name;
  6. public CallStack callStack;
  7. public List<Runtime.Object> outputStream;
  8. public List<Choice> currentChoices;
  9. public Flow(string name, Story story) {
  10. this.name = name;
  11. this.callStack = new CallStack(story);
  12. this.outputStream = new List<Object>();
  13. this.currentChoices = new List<Choice>();
  14. }
  15. public Flow(string name, Story story, Dictionary<string, object> jObject) {
  16. this.name = name;
  17. this.callStack = new CallStack(story);
  18. this.callStack.SetJsonToken ((Dictionary < string, object > )jObject ["callstack"], story);
  19. this.outputStream = Json.JArrayToRuntimeObjList ((List<object>)jObject ["outputStream"]);
  20. this.currentChoices = Json.JArrayToRuntimeObjList<Choice>((List<object>)jObject ["currentChoices"]);
  21. // choiceThreads is optional
  22. object jChoiceThreadsObj;
  23. jObject.TryGetValue("choiceThreads", out jChoiceThreadsObj);
  24. LoadFlowChoiceThreads((Dictionary<string, object>)jChoiceThreadsObj, story);
  25. }
  26. public void WriteJson(SimpleJson.Writer writer)
  27. {
  28. writer.WriteObjectStart();
  29. writer.WriteProperty("callstack", callStack.WriteJson);
  30. writer.WriteProperty("outputStream", w => Json.WriteListRuntimeObjs(w, outputStream));
  31. // choiceThreads: optional
  32. // Has to come BEFORE the choices themselves are written out
  33. // since the originalThreadIndex of each choice needs to be set
  34. bool hasChoiceThreads = false;
  35. foreach (Choice c in currentChoices)
  36. {
  37. c.originalThreadIndex = c.threadAtGeneration.threadIndex;
  38. if (callStack.ThreadWithIndex(c.originalThreadIndex) == null)
  39. {
  40. if (!hasChoiceThreads)
  41. {
  42. hasChoiceThreads = true;
  43. writer.WritePropertyStart("choiceThreads");
  44. writer.WriteObjectStart();
  45. }
  46. writer.WritePropertyStart(c.originalThreadIndex);
  47. c.threadAtGeneration.WriteJson(writer);
  48. writer.WritePropertyEnd();
  49. }
  50. }
  51. if (hasChoiceThreads)
  52. {
  53. writer.WriteObjectEnd();
  54. writer.WritePropertyEnd();
  55. }
  56. writer.WriteProperty("currentChoices", w => {
  57. w.WriteArrayStart();
  58. foreach (var c in currentChoices)
  59. Json.WriteChoice(w, c);
  60. w.WriteArrayEnd();
  61. });
  62. writer.WriteObjectEnd();
  63. }
  64. // Used both to load old format and current
  65. public void LoadFlowChoiceThreads(Dictionary<string, object> jChoiceThreads, Story story)
  66. {
  67. foreach (var choice in currentChoices) {
  68. var foundActiveThread = callStack.ThreadWithIndex(choice.originalThreadIndex);
  69. if( foundActiveThread != null ) {
  70. choice.threadAtGeneration = foundActiveThread.Copy ();
  71. } else {
  72. var jSavedChoiceThread = (Dictionary <string, object>) jChoiceThreads[choice.originalThreadIndex.ToString()];
  73. choice.threadAtGeneration = new CallStack.Thread(jSavedChoiceThread, story);
  74. }
  75. }
  76. }
  77. }
  78. }