ActionRemove.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /// <reference path="../Things/Clothing.ts" />
  2. /// <reference path="../Action.ts" />
  3. /// <reference path="../Rule.ts" />
  4. /// <reference path="../Rulebook.ts" />
  5. /// <reference path="../../../Elements/Classes/Say/SayBold.ts" />
  6. /// <reference path="../../../Elements/Modules/HyperlinkHandler.ts" />
  7. class ActionRemove extends Action {
  8. public static check: Rulebook<ActionRemove> = new Rulebook("Check Removing");
  9. public static carry: Rulebook<ActionRemove> = new Rulebook("Carry out Removing");
  10. /**
  11. * Needs to return a string explaining what the player will do if he does this action.
  12. * For instance, ActionTaking should return something like return "take " + this.nouns[0].getName(),
  13. * which would read as "take thing".
  14. * remember that things implement PRINTABLE interface, so you can get their names.
  15. * @returns {Say}
  16. */
  17. public getCommandText () {
  18. return "take off " + (this.getNoun(0) != undefined ? this.getNoun(0).getPrintedName() : "");
  19. }
  20. public getClothing () : Clothing{
  21. return this.getNoun(0);
  22. }
  23. public static checkIsWearable = ActionRemove.check.createAndAddRule({
  24. name : "Is noun a clothing",
  25. firstPriority : Rule.PRIORITY_HIGHEST,
  26. code : (runner : RulebookRunner<ActionRemove>) => {
  27. let action = runner.noun;
  28. let noun = action.getClothing();
  29. if (!(noun instanceof Clothing)) {
  30. if (action.actor == WorldState.player) {
  31. action.say.add("You can only remove Clothing.");
  32. }
  33. return false;
  34. }
  35. }
  36. });
  37. public static checkIsHeld = ActionRemove.check.createAndAddRule({
  38. name : "Is noun worn",
  39. firstPriority : Rule.PRIORITY_HIGH,
  40. code : async (runner : RulebookRunner<ActionRemove>) => {
  41. let action = runner.noun;
  42. let noun = action.getClothing();
  43. if (!Thing.WearRelation.isRight(action.actor, noun)) {
  44. if (action.actor == WorldState.player) {
  45. action.say.add("You are not wearing it.");
  46. }
  47. return false;
  48. }
  49. }
  50. });
  51. public static carryDefault = ActionRemove.carry.createAndAddRule({
  52. name : "Set Clothing as Carried",
  53. firstPriority : Rule.PRIORITY_MEDIUM,
  54. code : (runner : RulebookRunner<ActionRemove>) => {
  55. let action = runner.noun;
  56. let noun = action.getClothing();
  57. Thing.CarryRelation.setRelation(action.actor, noun);
  58. let actor = action.actor;
  59. let thing = (<Thing>action.getNoun(0));
  60. if (actor == WorldState.player) {
  61. action.say.add(new SayBold(thing, ": "), "Removed.");
  62. } else {
  63. action.say.add(new SayThe(), actor, " takes off ", new SayThe(), thing, ".");
  64. }
  65. }
  66. });
  67. }
  68. /**
  69. * Hyperlinking
  70. */
  71. Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
  72. {
  73. name : "Hyperlink - Remove",
  74. firstPriority : Rule.PRIORITY_HIGHEST,
  75. code : (rulebook : RulebookRunner<Thing>) => {
  76. let thing = <Thing> rulebook.noun;
  77. if (thing instanceof Clothing && (Thing.WearRelation.isRight(WorldState.player, thing))) {
  78. Elements.HyperlinkHandler.addAvailableAction("Remove", new ActionRemove(WorldState.player, thing));
  79. }
  80. }
  81. }
  82. ));
  83. /**
  84. Inventory
  85. */
  86. Elements.InventoryHandler.LinkingThing.addRule(new Rule(
  87. {
  88. name : "Inventory - Remove",
  89. firstPriority : Rule.PRIORITY_LOWEST,
  90. code : (rulebook : RulebookRunner<Thing>) => {
  91. let thing = <Thing> rulebook.noun;
  92. if (thing instanceof Clothing && (Thing.WearRelation.isRight(WorldState.player, thing))) {
  93. Elements.InventoryHandler.printThingLink("R", new ActionRemove(WorldState.player, thing));
  94. }
  95. }
  96. }
  97. ));