ActionInventory.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. let elements = await ((new Say(thing)).getPureElements());
  25. elements.forEach(ele => {
  26. p.appendChild(ele);
  27. });
  28. p.addEventListener("click", () => {
  29. resolve(thing);
  30. });
  31. Controls.KeyHandler.applyCode(p, Controls.KeyHandler.getFirstKeyCode());
  32. return p;
  33. }
  34. public static checkInventoryRule = ActionInventory.check.createAndAddRule({
  35. name : "List inventory as options",
  36. code : async (rulebook : RulebookRunner<ActionTake>) => {
  37. let player = WorldState.player;
  38. let wielded = Thing.WieldRelation.getRight(player).sort(Elements.InventoryHandler.thingSort);
  39. let worn = Thing.WearRelation.getRight(player).sort(Elements.InventoryHandler.thingSort);
  40. let carried = Thing.CarryRelation.getRight(player).sort(Elements.InventoryHandler.thingSort);
  41. let buttons;
  42. let chosenPromise = new Promise((async resolve => {
  43. Controls.KeyHandler.reset();
  44. let say = new Say();
  45. if (wielded.length > 0) {
  46. say.add(new SayBold("Wielded:"), Say.LINE_BREAK);
  47. for (let i = 0; i < wielded.length; i++) {
  48. say.add(await ActionInventory.createButton(wielded[i], resolve));
  49. }
  50. }
  51. if (worn.length > 0) {
  52. if (say.sequence.length > 0) {
  53. say.add(Say.PARAGRAPH_BREAK);
  54. }
  55. say.add(new SayBold("Worn:"), Say.LINE_BREAK);
  56. for (let i = 0; i < worn.length; i++) {
  57. say.add(await ActionInventory.createButton(worn[i], resolve));
  58. }
  59. }
  60. if (carried.length > 0) {
  61. if (say.sequence.length > 0) {
  62. say.add(Say.PARAGRAPH_BREAK);
  63. }
  64. say.add(new SayBold("Carried:"), Say.LINE_BREAK);
  65. for (let i = 0; i < carried.length; i++) {
  66. say.add(await ActionInventory.createButton(carried[i], resolve));
  67. }
  68. }
  69. if (say.sequence.length == 0) {
  70. return resolve(undefined);
  71. }
  72. buttons = await say.getHTMLContent();
  73. Elements.CurrentTurnHandler.print(...(buttons));
  74. }));
  75. let chosenThing = await chosenPromise;
  76. rulebook.noun.setNoun(0, chosenThing);
  77. if (chosenThing != undefined) {
  78. Elements.CurrentTurnHandler.unprint(...buttons);
  79. Controls.KeyHandler.reset();
  80. return new ActionExamine(WorldState.player, chosenThing);
  81. } else {
  82. rulebook.noun.say.add("You have nothing.");
  83. return false;
  84. }
  85. }
  86. });
  87. }
  88. Elements.HyperlinkHandler.CommonActionsRulebook.addRule(new Rule({
  89. name : "Add Take Inventory Command Rule",
  90. firstPriority : Rule.PRIORITY_LOWEST,
  91. priority : Rule.PRIORITY_MEDIUM,
  92. code : (rulebook : RulebookRunner<void>) => {
  93. Elements.HyperlinkHandler.addCommonAction("Inventory", new ActionInventory(WorldState.player));
  94. }
  95. }));