1
1

ActionTake.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /// <reference path="../Action.ts" />
  2. /// <reference path="../../../Elements/Classes/Say/SayBold.ts" />
  3. /// <reference path="../../../Elements/Classes/Say/SayThe.ts" />
  4. /// <reference path="../../../Elements/Modules/HyperlinkHandler.ts" />
  5. class ActionTake extends Action {
  6. public static check : Rulebook<ActionTake> = new Rulebook("Check Taking");
  7. public static carry : Rulebook<ActionTake> = new Rulebook("Carry out Taking");
  8. /**
  9. * Needs to return a string explaining what the player will do if he does this action.
  10. * For instance, ActionTaking should return something like return "take " + this.nouns[0].getName(),
  11. * which would read as "take thing".
  12. * remember that things implement PRINTABLE interface, so you can get their names.
  13. * @returns {Say}
  14. */
  15. public getCommandText () {
  16. return "take " + (this.getNoun(0) != undefined ? this.getNoun(0).getPrintedName() : "");
  17. }
  18. public static defaultCarryTakingRule = new Rule({
  19. name : "Taking - Add the thing to your inventory",
  20. code : (rulebook : RulebookRunner<ActionTake>) => {
  21. let action = <ActionGo> rulebook.noun;
  22. let actor = action.actor;
  23. let thing = (<Thing>action.getNoun(0));
  24. if (thing.getEnclosedOne() != undefined) {
  25. // Remove Part Of, Carried or Wielded. It's essentially stealing.
  26. Thing.EnclosedRelation.unsetRight(thing);
  27. } else {
  28. thing.removeFromRoom();
  29. }
  30. Thing.CarryRelation.setRelation(actor, action.getNoun(0));
  31. if (actor == WorldState.player) {
  32. action.say.add(new SayBold((<Thing> action.getNoun(0)).getPrintedName() + ": "), "Taken.");
  33. } else {
  34. action.say.add(new SayThe(), actor, " takes ", new SayThe(undefined, true), (<Thing> action.getNoun(0)), ".");
  35. }
  36. }
  37. });
  38. }
  39. ActionTake.check.addRule(
  40. new Rule({
  41. name : "Check Taking - Who has it, really?",
  42. priority : Rule.PRIORITY_HIGHEST,
  43. code : (rulebook : RulebookRunner<ActionTake>) => {
  44. let action = <ActionGo> rulebook.noun;
  45. let actor = action.actor;
  46. let thing = (<Thing>action.getNoun(0));
  47. let owner = <Thing> thing.getEnclosedOne();
  48. if (owner == actor){
  49. if (owner == WorldState.player) {
  50. action.say.add("You already have it.");
  51. }
  52. return false;
  53. }
  54. }
  55. })
  56. );
  57. ActionTake.check.addRule(
  58. new Rule({
  59. name : "Check Taking - Donut steal",
  60. code : (rulebook : RulebookRunner<ActionTake>) => {
  61. let action = <ActionGo> rulebook.noun;
  62. let actor = action.actor;
  63. let thing = (<Thing>action.getNoun(0));
  64. let owner = <Thing> thing.getEnclosedOne();
  65. if (owner != undefined && owner.animated){
  66. if (actor == WorldState.player) {
  67. action.say.add(owner.getPrintedName() + " wouldn't like that.");
  68. }
  69. return false;
  70. }
  71. }
  72. })
  73. );
  74. ActionTake.check.addRule(
  75. new Rule({
  76. name : "Check Taking - Can't take fixed in place",
  77. code : (rulebook : RulebookRunner<ActionTake>) => {
  78. let action = <ActionGo> rulebook.noun;
  79. let actor = action.actor;
  80. let thing = (<Thing>action.getNoun(0));
  81. if (thing.fixedInPlace){
  82. if (actor == WorldState.player) {
  83. action.say.add("You can't take that.");
  84. }
  85. return false;
  86. }
  87. }
  88. })
  89. );
  90. ActionTake.carry.addRule(
  91. ActionTake.defaultCarryTakingRule
  92. );
  93. Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
  94. {
  95. name : "Hyperlink - Take",
  96. firstPriority : Rule.PRIORITY_HIGHEST,
  97. code : (rulebook : RulebookRunner<Thing>) => {
  98. let thing = <Thing> rulebook.noun;
  99. if (!thing.animated && !thing.fixedInPlace && thing.getRoom() == WorldState.player.getRoom() && thing.getEnclosedOne() instanceof Room) {
  100. Elements.HyperlinkHandler.addAvailableAction("Take", new ActionTake(WorldState.player, thing));
  101. }
  102. }
  103. }
  104. ));