1
1

TurnSequence.ts 5.3 KB

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