Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// updateInventoryDocks.js

(function () {
  /*global adventurejs A*/

  var p = adventurejs.Display.prototype;

  /**
   * Set inventory in inventory docks.
   * @method adventurejs.Display#updateInventoryDocks
   * @param {Object} properties
   * @TODO different lists for specific docks
   */
  p.updateInventoryDocks = function Display_updateInventoryDocks(inventory) {
    this.inventorydocks.forEach(function (dock) {
      let html = '<div class="inventorydock">';
      let assetclasses, is, is_in;
      if (dock.dataset.assetclasses) {
        assetclasses = dock.dataset.assetclasses.split(",");
      }
      if (dock.dataset.is) {
        is = dock.dataset.is.split(",");
      }
      if (dock.dataset.is_in) {
        is_in = dock.dataset.is_in.split(",");
      }

      inventory.forEach(function (item) {
        let asset = this.game.getAsset(item);
        let image = "";
        let hasimage = "";
        if (asset.image) {
          image = `<span class="inventory icon"><img src="${
            this.game.image_lookup[asset.image]
          }"/></span>`;
          hasimage = "hasimage";
        }
        let allow = true;

        // assetclasses limits to specified classes, like Weapon or Key
        if (assetclasses && assetclasses.length) {
          let allowed_class = false;
          for (var klas in assetclasses) {
            if (asset instanceof adventurejs[assetclasses[klas]]) {
              allowed_class = true;
              continue;
            }
          }
          if (!allowed_class) allow = false;
        }

        // is means limit to inventory with particular state, like is.worn for clothes
        if (is && is.length) {
          let allowed_is = false;
          for (var truth in is) {
            if (asset.is[is[truth]]) {
              allowed_is = true;
              continue;
            }
          }
          if (!allowed_is) allow = false;
        }

        // is_in means limit to inventory in a container, like a knapsack
        if (is_in && is_in.length) {
          console.warn("asset", asset);
          console.warn("is_in", is_in);
          let allowed_is_in = false;
          for (var id in is_in) {
            if (asset.isWithin(is_in[id])) {
              allowed_is_in = true;
              continue;
            }
          }
          if (!allowed_is_in) allow = false;
        }

        if (allow) {
          let itemhtml = `<button data-id="${asset.id}" data-name="${asset.name}" class="dock_button btn_inventory inventory id_${asset.id} class_${asset.class} ${hasimage}" data-name="${asset.name}"><span class="inventory name">${asset.name}</span>${image}</button>`;
          html += itemhtml;
        }
      }, this);

      html += "</div>";
      dock.innerHTML = html;

      dock.querySelectorAll(".btn_inventory").forEach(function (el) {
        el.game = this.game;
        el.addEventListener("click", function () {
          if (this.game.display.inputEl.value.length) {
            this.game.sendToInput(el.dataset.name);
          } else {
            this.game.sendToParser("examine " + el.dataset.name);
          }
        });
      }, this);
    }, this);
  };
})();