CoinPouch.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /// <reference path="../Thing.ts" />
  2. /// <reference path="../Action/ActionTake.ts" />
  3. /// <reference path="../../../Elements/Modules/RoomHandler.ts" />
  4. class CoinPouch extends Thing {
  5. private coins : number = 0;
  6. public addCoins (coins : number) {
  7. this.coins += coins;
  8. }
  9. public removeCoins (coins : number) {
  10. this.coins -= coins;
  11. }
  12. public getCoins () {
  13. return this.coins;
  14. }
  15. public getShiny () {
  16. return this.coins > 0;
  17. }
  18. public constructor (options : ThingOptions) {
  19. super(options);
  20. this.addGetAlterations((purse) => {
  21. return {
  22. coins : purse.getCoins()
  23. };
  24. });
  25. this.addSetAlterations((purse, changeObj) => {
  26. purse.coins = (changeObj.coins);
  27. });
  28. }
  29. public static carryOutTakingCoinPouches = new Rule({
  30. name : "Carry out taking coin pouches",
  31. firstPriority : ActionTake.defaultCarryTakingRule.firstPriority,
  32. priority : ActionTake.defaultCarryTakingRule.priority + 1,
  33. code : async (rulebook : RulebookRunner<ActionTake>) => {
  34. let action = <ActionTake> rulebook.noun;
  35. let actor = action.actor;
  36. let thing = (<CoinPouch>action.getNoun(0));
  37. let actorPouches = Thing.CarryRelation.getRightType(actor, CoinPouch);
  38. if (actorPouches.length > 0) {
  39. let thingCoins = thing.getCoins();
  40. if (thingCoins > 0) {
  41. rulebook.skipRule(ActionTake.defaultCarryTakingRule);
  42. (<CoinPouch> actorPouches[0]).addCoins(thingCoins);
  43. thing.removeCoins(thing.getCoins());
  44. if (actor == WorldState.player) {
  45. action.say.add("You empty ", new SayThe(), thing, " into your ", actorPouches[0], ". Your ", actorPouches[0], " now has " + actorPouches[0].getCoins().toString() + " coins.");
  46. } else {
  47. action.say.add(new SayThe(), actor, " empties ", new SayThe(), thing, " into ", Say.hisHersIts(actor), actorPouches[0], ".");
  48. }
  49. } else {
  50. // give them the ol' switcharoo
  51. let myCoins = actorPouches[0].getCoins();
  52. actorPouches[0].removeCoins(myCoins);
  53. thing.addCoins(myCoins);
  54. if (actor == WorldState.player) {
  55. action.say.add("You empty your ", actorPouches[0], " into ", new SayThe(), thing, ".", Say.PARAGRAPH_BREAK);
  56. }
  57. let drop = new ActionDrop(actor, actorPouches[0]);
  58. await drop.execute();
  59. if (Thing.EnclosedRelation.getLeft(actorPouches[0]) == actor) {
  60. if (actor == WorldState.player) {
  61. action.say.add("You can't get rid of your ", actorPouches[0], "!");
  62. }
  63. return false;
  64. }
  65. }
  66. }
  67. },
  68. conditions : (rulebook : RulebookRunner<ActionTake>) => {
  69. return ((<ActionTake> rulebook.noun).getNoun(0) instanceof CoinPouch);
  70. }
  71. });
  72. }
  73. ActionTake.carry.addRule(CoinPouch.carryOutTakingCoinPouches);
  74. Say.afterPrinting.addRule(new Rule(
  75. {
  76. name : "Include contents of Coin Pouch while Printing Visible Things in a Room",
  77. code : (rulebook : RulebookRunner<Say>) => {
  78. let say = <Say> rulebook.noun;
  79. let pouch = <CoinPouch> say.currentNoun;
  80. say.currentNounElements.push(document.createTextNode(" with " + pouch.getCoins().toString() + " coins"));
  81. },
  82. conditions : (rulebook : RulebookRunner<Say>) => {
  83. return Elements.RoomHandler.PrintingVisibleThingsRulebook.isRunning()
  84. && (<Say> rulebook.noun).currentNoun instanceof CoinPouch
  85. && (<CoinPouch> (<Say> rulebook.noun).currentNoun).getCoins() > 0;
  86. }
  87. }
  88. ));
  89. ActionExamine.carry.addRule(new Rule({
  90. name : "Print description of coins in Coin Pouch",
  91. firstPriority : ActionExamine.PrintDescriptionOfExaminedThingRule.firstPriority,
  92. priority : ActionExamine.PrintDescriptionOfExaminedThingRule.priority - 1,
  93. code : (rulebook : RulebookRunner<ActionExamine>) => {
  94. let action = <ActionExamine> rulebook.noun;
  95. let thing = <CoinPouch> action.getNoun(0);
  96. if (thing.getCoins() > 0) {
  97. action.say.add(" There are " + thing.getCoins().toString() + " coins in it.");
  98. } else {
  99. action.say.add(" There are no coins in it.");
  100. }
  101. },
  102. conditions : (rulebook : RulebookRunner<ActionExamine>) => {
  103. return ((<ActionExamine> rulebook.noun).getNoun(0) instanceof CoinPouch);
  104. }
  105. }));