1
1

ActionGoThrough.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /// <reference path="../Action.ts" />
  2. /// <reference path="../Rule.ts" />
  3. /// <reference path="../Rulebook.ts" />
  4. class ActionGoThrough extends Action {
  5. public static check = new Rulebook<ActionGoThrough>("Check Going Through");
  6. public static carry = new Rulebook<ActionGoThrough>("Carry out Going Through");
  7. public constructor (actor : Thing, ...nouns : Array<any>) {
  8. super(actor, ...nouns);
  9. this.requiresNoun = true;
  10. this.requiresVisibility = true;
  11. }
  12. public getCommandText () {
  13. return "go through " + (<Door> this.getDoor()).getPrintedName();
  14. }
  15. public getDoor () : Door {
  16. return this.getNoun(0);
  17. }
  18. public static checkLocked = ActionGoThrough.check.createAndAddRule({
  19. firstPriority : Rule.PRIORITY_HIGHEST,
  20. priority : Rule.PRIORITY_HIGHEST,
  21. name : "Check Going Through - Is it locked?",
  22. code : async (runner: RulebookRunner<ActionGoThrough>) => {
  23. let actor = runner.noun.actor;
  24. let door = <Door> runner.noun.getDoor();
  25. let isPlayer = WorldState.isPlayer(actor);
  26. if (door.locked) {
  27. let actorThings = actor.getEnclosed();
  28. if (actorThings.includes(door.key)) {
  29. if (isPlayer) {
  30. Elements.CurrentTurnHandler.printAsContent(new Say(Say.Mention(door), " is locked, but you can unlock it with ", Say.Mention(door.key), ". Do you want to unlock it?"));
  31. if (!(await Controls.askForConsent())) {
  32. return false;
  33. }
  34. }
  35. door.locked = false;
  36. } else {
  37. if (isPlayer) {
  38. runner.noun.say.add("It's locked, and you don't have the key to unlock it...");
  39. }
  40. // TODO: Reconsider whether taking a turn on failed open checks is good.
  41. return false;
  42. }
  43. }
  44. }
  45. });
  46. public static carryGoThrough = ActionGoThrough.check.createAndAddRule({
  47. name: "Carry Going Through - Go Through!",
  48. code: async (runner: RulebookRunner<ActionGoThrough>) => {
  49. let actor = runner.noun.actor;
  50. let door = <Door> runner.noun.getDoor();
  51. let isPlayer = WorldState.isPlayer(actor);
  52. if (isPlayer) {
  53. runner.noun.say.add("You go through ", Say.Mention(door), ".");
  54. if (actor.isPlayer()) {
  55. WorldState.rememberRoom(actor.getRoom(), door.destination);
  56. }
  57. } else {
  58. let oldRoom = actor.getRoom();
  59. let playerRoom = WorldState.player.getRoom();
  60. if (oldRoom == playerRoom) {
  61. runner.noun.say.add(Say.Mention(actor), " goes through ", Say.Mention(door), ".");
  62. } else {
  63. runner.noun.say.add(Say.Mention(actor), " arrives through ", Say.Mention(door), ".");
  64. }
  65. }
  66. door.destination.place(actor);
  67. }
  68. });
  69. }
  70. Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
  71. {
  72. name : "Hyperlink - Go through",
  73. firstPriority : Rule.PRIORITY_HIGHEST,
  74. code : (rulebook : RulebookRunner<Thing>) => {
  75. let thing = <Thing> rulebook.noun;
  76. if (thing instanceof Door && thing.isVisibleTo(WorldState.player)) {
  77. Elements.HyperlinkHandler.addAvailableAction("Go through", new ActionGoThrough(WorldState.player, thing));
  78. }
  79. }
  80. }
  81. ));