123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /// <reference path="../Elements.ts" />
- /// <reference path="../../World/Classes/Rulebook.ts" />
- /// <reference path="../../World/Classes/Rule.ts" />
- /// <reference path="../../World/Classes/Action/ActionExamine.ts" />
- /// <reference path="../../World/Classes/Things/CoinPouch.ts" />
- module Elements.InventoryHandler {
- var container = document.getElementById("inventoryTarget");
- function empty () {
- while (container.firstChild) {
- container.removeChild(container.firstChild);
- }
- }
- function printHeader (header : string) {
- let p = document.createElement("p");
- p.classList.add("inventoryHeader");
- p.appendChild(document.createTextNode(header + ":"));
- container.appendChild(p);
- }
- let currentRow : HTMLElement;
- export let LinkingThing = new Rulebook<Thing>("Inventory - Links for a Thing");
- export function printThingLink (shortcut : string, action : Action) {
- let nameLink = document.createElement("div");
- nameLink.classList.add("inventoryLink");
- nameLink.appendChild(document.createTextNode(shortcut));
- currentRow.appendChild(nameLink);
- Controls.Links.makeLink(nameLink, action);
- }
- async function printThing (thing : Thing) {
- currentRow = document.createElement("div");
- currentRow.classList.add("inventoryRow");
- let nameLink = document.createElement("div");
- nameLink.classList.add("inventoryLink", "name");
- nameLink.appendChild(document.createTextNode(thing.getPrintedName()));
- currentRow.appendChild(nameLink);
- Controls.Links.makeLink(nameLink, new ActionExamine(WorldState.player, thing));
- await LinkingThing.execute({noun : thing});
- container.appendChild(currentRow);
- }
- export function thingSort (a : Thing, b : Thing) {
- let na = a.getPrintedName().toLowerCase();
- let nb = b.getPrintedName().toLowerCase();
- if (na < nb) return -1;
- if (na > nb) return 1;
- return 0;
- }
- export async function updateInventory () {
- let player = WorldState.player;
- empty();
- let wielded = Thing.WieldRelation.getRight(player).sort(thingSort);
- let worn = Thing.WearRelation.getRight(player).sort(thingSort);
- let carried = Thing.CarryRelation.getRight(player).sort(thingSort);
- if (wielded.length > 0) {
- printHeader("Wielded");
- for (let i = 0; i < wielded.length; i++) {
- await printThing(wielded[i]);
- }
- }
- if (worn.length > 0) {
- printHeader("Worn");
- for (let i = 0; i < worn.length; i++) {
- await printThing(worn[i]);
- }
- }
- if (carried.length > 0) {
- printHeader("Carried");
- for (let i = 0; i < carried.length; i++) {
- await printThing(carried[i]);
- }
- }
- let pouch = <Array<CoinPouch>> Thing.CarryRelation.getRightType(WorldState.player, CoinPouch);
- if (pouch.length > 0) {
- let p = document.createElement("p");
- p.classList.add("inventoryGold");
- let s : Say;
- if (pouch[0].getCoins() > 0) {
- s = new Say("There are " + pouch[0].getCoins().toString() + " coins in your ", pouch[0], ".");
- } else {
- s = new Say("There are no coins in your ", pouch[0], ".");
- }
- await s.getPureElements().then(elements => {
- elements.forEach(element => {
- p.appendChild(element);
- });
- });
- container.appendChild(p);
- }
- }
- }
|