1
1

ActionDrop.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /// <reference path="../Action.ts" />
  2. /// <reference path="../Rule.ts" />
  3. /// <reference path="../Rulebook.ts" />
  4. /// <reference path="../../../Elements/Classes/Say/SayBold.ts" />
  5. /// <reference path="../../../Elements/Modules/HyperlinkHandler.ts" />
  6. /// <reference path="../../../Elements/Modules/InventoryHandler.ts" />
  7. class ActionDrop extends Action {
  8. public static check = new Rulebook<ActionDrop>("Check Dropping");
  9. public static carry = new Rulebook<ActionDrop>("Carry out Dropping");
  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 "drop " + (this.getNoun(0) != undefined ? this.getNoun(0).getPrintedName() : "");
  19. }
  20. }
  21. ActionDrop.check.addRule(
  22. new Rule({
  23. name : "Check Dropping - Are you Wearing it?",
  24. firstPriority : Rule.PRIORITY_LOWEST,
  25. code : async (rulebook : RulebookRunner<ActionDrop>) => {
  26. let action = <ActionGo> rulebook.noun;
  27. let actor = action.actor;
  28. let thing = (<Thing>action.getNoun(0));
  29. if (Thing.WearRelation.getLeft(thing) == actor || Thing.WieldRelation.getLeft(thing) == actor){
  30. if (action.actor == WorldState.player) {
  31. action.say.add("(first taking off the ", thing, ")", Say.LINE_BREAK);
  32. }
  33. let takingAction = new ActionRemove(actor, thing);
  34. await takingAction.execute();
  35. action.say.add(takingAction.say);
  36. if (Thing.WearRelation.getLeft(thing) == actor || Thing.WieldRelation.getLeft(thing) == actor) {
  37. return false;
  38. }
  39. action.say.add(Say.LINE_BREAK);
  40. }
  41. }
  42. })
  43. );
  44. ActionDrop.check.addRule(
  45. new Rule({
  46. name : "Check Dropping - Do you have it??",
  47. firstPriority : Rule.PRIORITY_LOWEST,
  48. code : (rulebook : RulebookRunner<ActionDrop>) => {
  49. let action = <ActionGo> rulebook.noun;
  50. let actor = action.actor;
  51. let thing = (<Thing>action.getNoun(0));
  52. if (Thing.CarryRelation.getLeft(thing) != actor){
  53. if (actor == WorldState.player) {
  54. action.say.add("You don't have it.");
  55. }
  56. return false;
  57. }
  58. }
  59. })
  60. );
  61. ActionDrop.carry.addRule(
  62. new Rule({
  63. name : "Dropping - Place the noun on the floor",
  64. code : (rulebook : RulebookRunner<ActionDrop>) => {
  65. let action = <ActionGo> rulebook.noun;
  66. let actor = action.actor;
  67. let thing = (<Thing>action.getNoun(0));
  68. Thing.EnclosedRelation.unsetRight(thing);
  69. actor.getRoom().place(thing);
  70. if (actor == WorldState.player) {
  71. action.say.add(new SayBold((<Thing> action.getNoun(0)).getPrintedName() + ": "), "Dropped.");
  72. } else {
  73. action.say.add(new SayThe(), actor, " drops ", new SayThe(), (<Thing> action.getNoun(0)), " on the floor.");
  74. }
  75. }
  76. })
  77. );
  78. /**
  79. * Hyperlinking
  80. */
  81. Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
  82. {
  83. name : "Hyperlink - Drop",
  84. firstPriority : Rule.PRIORITY_HIGHEST,
  85. code : (rulebook : RulebookRunner<Thing>) => {
  86. let thing = <Thing> rulebook.noun;
  87. if (Thing.CarryRelation.getLeft(thing) == WorldState.player || Thing.WieldRelation.getLeft(thing) == WorldState.player || Thing.WearRelation.getLeft(thing) == WorldState.player) {
  88. Elements.HyperlinkHandler.addAvailableAction("Drop", new ActionDrop(WorldState.player, thing));
  89. }
  90. }
  91. }
  92. ));
  93. /**
  94. Inventory
  95. */
  96. Elements.InventoryHandler.LinkingThing.addRule(new Rule(
  97. {
  98. name : "Inventory - Drop",
  99. firstPriority : Rule.PRIORITY_LOWEST,
  100. code : (rulebook : RulebookRunner<Thing>) => {
  101. let thing = <Thing> rulebook.noun;
  102. Elements.InventoryHandler.printThingLink("D", new ActionDrop(WorldState.player, thing));
  103. }
  104. }
  105. ));