TurnSequence.ts 4.2 KB

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