UNPKG

camelot-unchained

Version:
100 lines (87 loc) 3.39 kB
/** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict"; /** * Inventory */ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Inventory = function () { /** * Inventory Constructor * @param {Inventory = <Inventory>{}} inventory - provide an existing inventory to copy all items into new inventory */ function Inventory() { var inventory = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; _classCallCheck(this, Inventory); this.items = inventory.items || []; } /** * Check if the inventory contains an item * @param {string} id - the id of item to look for * @return {boolean} returns true if the item existing in the inventory */ _createClass(Inventory, [{ key: "hasItem", value: function hasItem(id) { return this.items.filter(function (item) { return item.id == id; }).length > 0; } /** * Adds an item to the inventory * @param {Item} item - the item to add to inventory */ }, { key: "addItem", value: function addItem(item) { if (this.hasItem(item.id) == false) { this.items.push(item); } } /** * Removes an item from the inventory with the given item id * @param {string} id - the item id to remove */ }, { key: "removeItem", value: function removeItem(id) { if (this.hasItem(id)) { var itemIndex = null; this.items.forEach(function (item, index) { if (item.id == id) { itemIndex = index; } }); if (itemIndex != null) { this.items.splice(itemIndex, 1); } } } /** * Get a list of all item ID's currently in the inventory * @return {string[]} an array of item ID's */ }, { key: "getItemIDs", value: function getItemIDs() { var itemIDs = []; this.items.forEach(function (item) { itemIDs.push(item.id); }); return itemIDs; } }], [{ key: "create", value: function create() { var a = new Inventory(); return a; } }]); return Inventory; }(); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = Inventory;