ActionInventory.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /// <reference path="../Action.ts" />
  2. /// <reference path="../../../Elements/Modules/InventoryHandler.ts" />
  3. /// <reference path="../../../Elements/Classes/Say.ts" />
  4. /// <reference path="../../../Elements/Classes/Say/SayBold.ts" />
  5. class ActionInventory extends Action {
  6. public requiresTurn = false;
  7. public requiresNoun = false;
  8. public requiresVisibility = false;
  9. public static check : Rulebook<ActionInventory> = new Rulebook("Check Taking Inventory");
  10. public static carry : Rulebook<ActionInventory> = new Rulebook("Carry out Taking Inventory");
  11. /**
  12. * Needs to return a string explaining what the player will do if he does this action.
  13. * For instance, ActionTaking should return something like return "take " + this.nouns[0].getName(),
  14. * which would read as "take thing".
  15. * remember that things implement PRINTABLE interface, so you can get their names.
  16. * @returns {Say}
  17. */
  18. public getCommandText () {
  19. return "take inventory";
  20. }
  21. public static async createButton (thing : Thing, resolve : (t : Thing) => void) {
  22. let p = document.createElement("p");
  23. p.classList.add("choice");
  24. p.classList.add("small");
  25. let elements = await ((new Say(thing)).getPureElements());
  26. elements.forEach(ele => {
  27. p.appendChild(ele);
  28. });
  29. p.addEventListener("click", () => {
  30. resolve(thing);
  31. });
  32. Controls.KeyHandler.applyCode(p, Controls.KeyHandler.getFirstKeyCode());
  33. return p;
  34. }
  35. public static checkInventoryRule = ActionInventory.check.createAndAddRule({
  36. name : "List inventory as options",
  37. code : async (rulebook : RulebookRunner<ActionTake>) => {
  38. let player = WorldState.player;
  39. let wielded = Thing.WieldRelation.getRight(player).sort(Elements.InventoryHandler.thingSort);
  40. let worn = Thing.WearRelation.getRight(player).sort(Elements.InventoryHandler.thingSort);
  41. let carried = Thing.CarryRelation.getRight(player).sort(Elements.InventoryHandler.thingSort);
  42. let buttons;
  43. let chosenPromise = new Promise((async resolve => {
  44. Controls.KeyHandler.reset();
  45. let say = new Say();
  46. if (wielded.length > 0) {
  47. say.add(new SayBold("Wielded:"), Say.PARAGRAPH_BREAK);
  48. for (let i = 0; i < wielded.length; i++) {
  49. say.add(await ActionInventory.createButton(wielded[i], resolve));
  50. }
  51. }
  52. if (worn.length > 0) {
  53. if (say.sequence.length > 0) {
  54. say.add(Say.PARAGRAPH_BREAK);
  55. }
  56. say.add(new SayBold("Worn:"), Say.LINE_BREAK);
  57. for (let i = 0; i < worn.length; i++) {
  58. say.add(await ActionInventory.createButton(worn[i], resolve));
  59. }
  60. }
  61. if (carried.length > 0) {
  62. if (say.sequence.length > 0) {
  63. say.add(Say.PARAGRAPH_BREAK);
  64. }
  65. say.add(new SayBold("Carried:"), Say.LINE_BREAK);
  66. for (let i = 0; i < carried.length; i++) {
  67. say.add(await ActionInventory.createButton(carried[i], resolve));
  68. }
  69. }
  70. if (say.sequence.length == 0) {
  71. return resolve(undefined);
  72. }
  73. buttons = await say.getHTMLContent();
  74. Elements.CurrentTurnHandler.print(...(buttons));
  75. }));
  76. let chosenThing = await chosenPromise;
  77. rulebook.noun.setNoun(0, chosenThing);
  78. if (chosenThing != undefined) {
  79. Elements.CurrentTurnHandler.unprint(...buttons);
  80. Controls.KeyHandler.reset();
  81. return new ActionExamine(WorldState.player, chosenThing);
  82. } else {
  83. rulebook.noun.say.add("You have nothing.");
  84. return false;
  85. }
  86. }
  87. });
  88. }
  89. Elements.HyperlinkHandler.CommonActionsRulebook.addRule(new Rule({
  90. name : "Add Take Inventory Command Rule",
  91. firstPriority : Rule.PRIORITY_LOWEST,
  92. priority : Rule.PRIORITY_MEDIUM,
  93. code : (rulebook : RulebookRunner<void>) => {
  94. Elements.HyperlinkHandler.addCommonAction("Inventory", new ActionInventory(WorldState.player));
  95. }
  96. }));