InventoryHandler.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /// <reference path="../Elements.ts" />
  2. /// <reference path="../../World/Classes/Rulebook.ts" />
  3. /// <reference path="../../World/Classes/Rule.ts" />
  4. /// <reference path="../../World/Classes/Action/ActionExamine.ts" />
  5. /// <reference path="../../World/Classes/Things/CoinPouch.ts" />
  6. module Elements.InventoryHandler {
  7. var container = document.getElementById("inventoryTarget");
  8. function empty () {
  9. while (container.firstChild) {
  10. container.removeChild(container.firstChild);
  11. }
  12. }
  13. function printHeader (header : string) {
  14. let p = document.createElement("p");
  15. p.classList.add("inventoryHeader");
  16. p.appendChild(document.createTextNode(header + ":"));
  17. container.appendChild(p);
  18. }
  19. let currentRow : HTMLElement;
  20. export let LinkingThing = new Rulebook<Thing>("Inventory - Links for a Thing");
  21. export function printThingLink (shortcut : string, action : Action) {
  22. let nameLink = document.createElement("div");
  23. nameLink.classList.add("inventoryLink");
  24. nameLink.appendChild(document.createTextNode(shortcut));
  25. currentRow.appendChild(nameLink);
  26. Controls.Links.makeLink(nameLink, action);
  27. }
  28. async function printThing (thing : Thing) {
  29. currentRow = document.createElement("div");
  30. currentRow.classList.add("inventoryRow");
  31. let nameLink = document.createElement("div");
  32. nameLink.classList.add("inventoryLink", "name");
  33. nameLink.appendChild(document.createTextNode(thing.getPrintedName()));
  34. currentRow.appendChild(nameLink);
  35. Controls.Links.makeLink(nameLink, new ActionExamine(WorldState.player, thing));
  36. await LinkingThing.execute({noun : thing});
  37. container.appendChild(currentRow);
  38. }
  39. export function thingSort (a : Thing, b : Thing) {
  40. let na = a.getPrintedName().toLowerCase();
  41. let nb = b.getPrintedName().toLowerCase();
  42. if (na < nb) return -1;
  43. if (na > nb) return 1;
  44. return 0;
  45. }
  46. export async function updateInventory () {
  47. let player = WorldState.player;
  48. empty();
  49. let wielded = Thing.WieldRelation.getRight(player).sort(thingSort);
  50. let worn = Thing.WearRelation.getRight(player).sort(thingSort);
  51. let carried = Thing.CarryRelation.getRight(player).sort(thingSort);
  52. if (wielded.length > 0) {
  53. printHeader("Wielded");
  54. for (let i = 0; i < wielded.length; i++) {
  55. await printThing(wielded[i]);
  56. }
  57. }
  58. if (worn.length > 0) {
  59. printHeader("Worn");
  60. for (let i = 0; i < worn.length; i++) {
  61. await printThing(worn[i]);
  62. }
  63. }
  64. if (carried.length > 0) {
  65. printHeader("Carried");
  66. for (let i = 0; i < carried.length; i++) {
  67. await printThing(carried[i]);
  68. }
  69. }
  70. let pouch = <Array<CoinPouch>> Thing.CarryRelation.getRightType(WorldState.player, CoinPouch);
  71. if (pouch.length > 0) {
  72. let p = document.createElement("p");
  73. p.classList.add("inventoryGold");
  74. let s : Say;
  75. if (pouch[0].getCoins() > 0) {
  76. s = new Say("There are " + pouch[0].getCoins().toString() + " coins in your ", pouch[0], ".");
  77. } else {
  78. s = new Say("There are no coins in your ", pouch[0], ".");
  79. }
  80. await s.getPureElements().then(elements => {
  81. elements.forEach(element => {
  82. p.appendChild(element);
  83. });
  84. });
  85. container.appendChild(p);
  86. }
  87. }
  88. }