1
1

TurnSequence.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /// <reference path="./Classes/Rulebook.ts" />
  2. /// <reference path="./Classes/Rule.ts" />
  3. module TurnSequence {
  4. export var rulebook = new Rulebook<Action>("Turn Sequence");
  5. export var playerActions = [];
  6. export let lastTurnTime : number = 0;
  7. export let currentTurnActions : Array<Action> = [];
  8. export let currentTurnActionsTargets : Array<any> = [];
  9. export let currentTurnActionsActors : Array<Thing> = [];
  10. export let repeatedAction : Action;
  11. export function clearActions () {
  12. console.debug(Rulebook.getIndentation() + "Clearing Turn Sequence Actions");
  13. currentTurnActions = [];
  14. currentTurnActionsTargets = [];
  15. currentTurnActionsActors = [];
  16. }
  17. export function addAction (action : Action) {
  18. currentTurnActions.push(action);
  19. currentTurnActionsTargets.push(action.getNoun(0));
  20. currentTurnActionsActors.push(action.actor);
  21. action.actor.lastActionTurn = WorldState.getCurrentTurn();
  22. }
  23. export async function execute (action? : Action) {
  24. // Only one action at a time
  25. if (playerActions.push(action) == 1) {
  26. let t0 = performance.now();
  27. console.debug(Rulebook.getIndentation() + " Player Action: " + (action ? action.getCommandText() : "none"));
  28. await rulebook.execute({
  29. noun: action
  30. });
  31. playerActions = [];
  32. let t1 = performance.now();
  33. lastTurnTime = t1 - t0;
  34. console.debug("Total: " + (t1 - t0) + " milliseconds.");
  35. if (Settings.sayTurnTime) {
  36. Elements.CurrentTurnHandler.printAsContent(new Say(new SayBold("Time taken for turn: "), (t1 - t0), " milliseconds."));
  37. }
  38. }
  39. if (repeatedAction != undefined) {
  40. setTimeout(() => {
  41. let action = TurnSequence.repeatedAction;
  42. TurnSequence.repeatedAction = undefined;
  43. TurnSequence.execute(action);
  44. }, 1);
  45. }
  46. }
  47. /**
  48. * This is the Prepare Elements for the turn rule.
  49. * @type {Rule}
  50. */
  51. export var PrepareElementsRule = new Rule({
  52. firstPriority: Rule.PRIORITY_HIGHEST, // This will be about the first Rule to be executed
  53. priority: Rule.PRIORITY_MEDIUM, // This needs to be done before the majority of the rules, but we don't want it to be the very first no matter what
  54. name: "Begin Turn (Elements Side)",
  55. code: function (runner : RulebookRunner<Action>) {
  56. Elements.CurrentTurnHandler.startTurn(runner.noun);
  57. }
  58. });
  59. TurnSequence.rulebook.addRule(PrepareElementsRule);
  60. /**
  61. * This is the Do Player Action Rule.
  62. * @type {Rule}
  63. */
  64. export let PlayerActionRule = new Rule({
  65. firstPriority: Rule.PRIORITY_HIGH,
  66. priority: Rule.PRIORITY_MEDIUM,
  67. name: "Do Player Action",
  68. code: async (rulebook : RulebookRunner<Action>) => {
  69. let playerAction = <Action> rulebook.noun;
  70. if (playerAction != undefined) {
  71. let promise = playerAction.execute();
  72. await promise;
  73. Elements.CurrentTurnHandler.printAsContent(playerAction.say);
  74. if (playerAction.requiresTurn) {
  75. WorldState.incrementPlayerTurn();
  76. }
  77. }
  78. }
  79. });
  80. rulebook.addRule(PlayerActionRule);
  81. /**
  82. * This is the Run Every Turn Rules.
  83. * @type {Rule}
  84. */
  85. export var RunEveryTurnRulesRule = new Rule({
  86. firstPriority: PlayerActionRule.firstPriority,
  87. priority: PlayerActionRule.priority - 1,
  88. name: "Run Every Turn Rules",
  89. code: async function () {
  90. while (WorldState.isTurnWaiting()) {
  91. await EveryTurn.EveryTurn.execute({});
  92. }
  93. }
  94. });
  95. rulebook.addRule(RunEveryTurnRulesRule);
  96. /**
  97. * This is the Clean up Turn Actions Rule
  98. */
  99. /**
  100. * This is the Run Every Turn Rules.
  101. * @type {Rule}
  102. */
  103. export var CleanTurnActionsRule = new Rule({
  104. firstPriority: RunEveryTurnRulesRule.firstPriority,
  105. priority: RunEveryTurnRulesRule.priority - 1,
  106. name: "Clear Turn Actions Rule",
  107. code: async function () {
  108. TurnSequence.clearActions();
  109. }
  110. });
  111. rulebook.addRule(CleanTurnActionsRule);
  112. /**
  113. * This is the Inform Elements the turn has ended rule.
  114. * @type {Rule}
  115. */
  116. export var RemoveTurnFromElementsRule = new Rule({
  117. firstPriority: Rule.PRIORITY_LOWEST, // This will be about the last rule to be executed
  118. priority: Rule.PRIORITY_MEDIUM, // This needs to be done after the majority of the rules, but we don't care if it's the very last or not
  119. name: "End Turn (Elements/Controls Side)",
  120. code: async function (rulebook : RulebookRunner<Action>) {
  121. Elements.CurrentTurnHandler.endTurn();
  122. Controls.KeyHandler.reset();
  123. await Elements.HyperlinkHandler.hyperlinkCommonActions();
  124. await Elements.RoomHandler.updateRoom();
  125. await Elements.RememberedHandler.updateMap();
  126. let playerAction = <Action> rulebook.noun;
  127. if (playerAction) {
  128. await Elements.HyperlinkHandler.hyperlinkObject(playerAction.getNoun(0));
  129. } else {
  130. await Elements.HyperlinkHandler.hyperlinkObject();
  131. }
  132. await Elements.InventoryHandler.updateInventory();
  133. await Elements.AppearanceHandler.updateAppearance();
  134. //await Elements.StatusLineHandler.updateStatusLine();
  135. }
  136. });
  137. TurnSequence.rulebook.addRule(RemoveTurnFromElementsRule);
  138. }