DialogueTree.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /// <reference path="../Save/StoredVariable.ts" />
  2. class DialogueTree {
  3. public id : string;
  4. private nodes : {[id : string] : DialogueNode} = {};
  5. public startNode : DialogueNode = undefined;
  6. private repeatChoices : boolean = true;
  7. private lastPrintedChoice : Array<HTMLElement>;
  8. private executedCount : StoredVariable<number>;
  9. public constructor (id : string) {
  10. this.id = id;
  11. this.executedCount = new StoredVariable({id: "DialogueTree " + id, value : 0});
  12. }
  13. public hasRan () {
  14. return this.executedCount.value > 0;
  15. }
  16. public ranTimes () {
  17. return this.executedCount.value;
  18. }
  19. public incrementRanCount () {
  20. this.executedCount.value = (this.ranTimes() + 1);
  21. }
  22. public addNode (node : DialogueNode) {
  23. this.nodes[node.id] = node;
  24. if (node.type == NodeType.Node) {
  25. this.nodes[node.name] = node; // This is a label!
  26. }
  27. }
  28. public addStartNode (node : DialogueNode) {
  29. this.addNode(node);
  30. this.startNode = node;
  31. }
  32. public getNode (id : string) {
  33. return this.nodes[id];
  34. }
  35. public getNext (node : DialogueNode) {
  36. let next = node.getNext();
  37. if (next != undefined) {
  38. return this.getNode(next);
  39. }
  40. }
  41. public setRepeatChoices (doIt : boolean) {
  42. this.repeatChoices = doIt;
  43. }
  44. public async execute (startId? : string) {
  45. this.incrementRanCount();
  46. console.debug(Rulebook.getIndentation() + "[DialogueTree] Running " + this.id);
  47. Rulebook.increaseIndentation(this);
  48. let node : DialogueNode;
  49. if (startId == undefined) {
  50. node = this.startNode;
  51. } else {
  52. node = this.getNode(startId);
  53. }
  54. if (node == undefined) {
  55. Elements.CurrentTurnHandler.printAsError("Unable to start dialogue " + this.id + ": A starting node could not be found.");
  56. return;
  57. }
  58. let previousNode : DialogueNode;
  59. while (node != undefined) {
  60. let nextNode = await this.processNode(node, previousNode);
  61. previousNode = node;
  62. node = nextNode;
  63. }
  64. Rulebook.decreaseIndentation();
  65. }
  66. public async processNode (node : DialogueNode, previousNode : DialogueNode) {
  67. console.debug(Rulebook.getIndentation() + "[" + node.type + "] " + node.id);
  68. Rulebook.increaseIndentation(node);
  69. let doChoices = node.hasChoices();
  70. if (node.type == NodeType.Tree) {
  71. await (<DialogueNodeTree> node).tree().execute();
  72. } else if (node.type == NodeType.Text) {
  73. let say = (<DialogueText> node).getSay();
  74. Elements.CurrentTurnHandler.printAsContent(say);
  75. } else if (node.type == NodeType.Set) {
  76. let runningSet = (<DialogueSet> node).run();
  77. if (runningSet instanceof Promise) {
  78. await runningSet;
  79. }
  80. }
  81. if (doChoices) {
  82. // Next node is decided by the choices
  83. let branchingDialogue = new BranchingDialogue();
  84. let choices = node.choices;
  85. let options = [];
  86. for (let i = 0; i < choices.length; i++) {
  87. let choice = <DialogueChoice> this.getNode(choices[i]);
  88. let branchingOption = new BranchingOption(choice.getSay(), choice.isAvailable());
  89. options.push(branchingOption);
  90. branchingDialogue.addOptions(branchingOption);
  91. }
  92. let chosenOption : BranchingOption = await branchingDialogue.getChosenOption();
  93. let chosenNodeId = choices[options.indexOf(chosenOption)];
  94. let chosenNode = <DialogueChoice> this.getNode(chosenNodeId);
  95. if (this.repeatChoices) {
  96. let say = new Say(new SayBold(" > ", chosenNode.getSay()));
  97. this.lastPrintedChoice = await Elements.CurrentTurnHandler.getSayElementsAsContent(say);
  98. await Elements.CurrentTurnHandler.print(...this.lastPrintedChoice);
  99. }
  100. console.debug(Rulebook.getIndentation() + "[Choice] Picked " + chosenNodeId);
  101. console.debug(Rulebook.getIndentation() + "[Choice] Going to " + chosenNode.getNext());
  102. Rulebook.increaseIndentation(chosenNodeId);
  103. Rulebook.decreaseIndentation();
  104. Rulebook.decreaseIndentation();
  105. return this.getNext(chosenNode);
  106. } else {
  107. Rulebook.decreaseIndentation();
  108. return this.getNext(node);
  109. }
  110. }
  111. public unprintLastChoice () {
  112. Elements.CurrentTurnHandler.unprint(...this.lastPrintedChoice);
  113. }
  114. }