1
1

ActionWear.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 ActionWear extends Action {
  8. public static check: Rulebook<ActionWear> = new Rulebook("Check Wearing");
  9. public static carry: Rulebook<ActionWear> = new Rulebook("Carry out Wearing");
  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 "wear " + (this.getNoun(0) != undefined ? this.getNoun(0).getPrintedName() : "");
  19. }
  20. public getClothing () : Clothing{
  21. return this.getNoun(0);
  22. }
  23. public static checkIsWearable = ActionWear.check.createAndAddRule({
  24. name : "Is noun a clothing",
  25. firstPriority : Rule.PRIORITY_HIGHEST,
  26. code : (runner : RulebookRunner<ActionWear>) => {
  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 wear Clothing.");
  32. }
  33. return false;
  34. }
  35. }
  36. });
  37. public static checkIsHeld = ActionWear.check.createAndAddRule({
  38. name : "Is noun held",
  39. firstPriority : Rule.PRIORITY_HIGH,
  40. code : async (runner : RulebookRunner<ActionWear>) => {
  41. let action = runner.noun;
  42. let noun = action.getClothing();
  43. if (!Thing.CarryRelation.isRight(action.actor, noun)) {
  44. if (action.actor == WorldState.player) {
  45. action.say.add("(first taking the ", noun, ")", Say.LINE_BREAK);
  46. }
  47. let takingAction = new ActionTake(action.actor, noun);
  48. await takingAction.execute();
  49. action.say.add(takingAction.say);
  50. if (!Thing.CarryRelation.isRight(action.actor, noun)) {
  51. return false;
  52. }
  53. }
  54. }
  55. });
  56. public static carryDefault = ActionWear.carry.createAndAddRule({
  57. name : "Set Clothing as Worn",
  58. firstPriority : Rule.PRIORITY_MEDIUM,
  59. code : (runner : RulebookRunner<ActionWear>) => {
  60. let action = runner.noun;
  61. let noun = action.getClothing();
  62. Thing.WearRelation.setRelation(action.actor, noun);
  63. let actor = action.actor;
  64. let thing = (<Thing>action.getNoun(0));
  65. if (actor == WorldState.player) {
  66. action.say.add(new SayBold(thing, ": "), "Worn.");
  67. } else {
  68. action.say.add(new SayThe(), actor, " puts on ", new SayThe(), thing, ".");
  69. }
  70. }
  71. });
  72. }
  73. /**
  74. * Hyperlinking
  75. */
  76. Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
  77. {
  78. name : "Hyperlink - Wear",
  79. firstPriority : Rule.PRIORITY_HIGHEST,
  80. code : (rulebook : RulebookRunner<Thing>) => {
  81. let thing = <Thing> rulebook.noun;
  82. if (thing instanceof Clothing && !(Thing.WearRelation.isRight(WorldState.player, thing))) {
  83. Elements.HyperlinkHandler.addAvailableAction("Wear", new ActionWear(WorldState.player, thing));
  84. }
  85. }
  86. }
  87. ));
  88. /**
  89. Inventory
  90. */
  91. Elements.InventoryHandler.LinkingThing.addRule(new Rule(
  92. {
  93. name : "Inventory - Wear",
  94. firstPriority : Rule.PRIORITY_LOWEST,
  95. code : (rulebook : RulebookRunner<Thing>) => {
  96. let thing = <Thing> rulebook.noun;
  97. if (thing instanceof Clothing && !(Thing.WearRelation.isRight(WorldState.player, thing))) {
  98. Elements.InventoryHandler.printThingLink("W", new ActionWear(WorldState.player, thing));
  99. }
  100. }
  101. }
  102. ));