class Inventory { constructor() { } init() { this.hold = []; this.hold.push({ name: "₩", type: "woolong", amount: 10000 }); this.hold.push({ name: "Spare Part", type: "sparepart", amount: 1 }); this.hold.push({ name: "Scrap Metal", type: "scrap", amount: 2 }); this.hold.push({ name: "Hydrogen", type: "element", amount: 128 }); } getAll() { return this.hold; } getAllByType(type) { return this.hold.filter((item) => item.type == type); } getItem(name) { return this.hold.find((item) => item.name == name); } consumeShipRepairMaterials() { let parts = this.getAllByType("sparepart"); if (!parts || parts.length == 0) { return false; } return this.consumeItem(parts[0], 1); } consumeCraftingMaterials(ingredients) { let consumed = []; for (let index = 0; index < ingredients.length; index++) { let ingredient = ingredients[index]; let item = this.getItem(ingredient.name); if (!item) { this.addItems(consumed); return false; } let itemConsumed = this.consumeItem(item, ingredient.amount); if (!itemConsumed) { this.addItems(consumed); return false; } consumed.push(itemConsumed); } return true; } consumeShipTravelMaterials() { let fuel = this.getAllByType("fuel"); if (!fuel || fuel.length == 0) { return false; } return this.consumeItem(fuel[0], 1); } addItem(item, amount) { let foundItem = this.getItem(item.name); if (!foundItem) { this.hold.push(item); } else { foundItem.amount += amount; } game.playerHud.inventory.buildInventory(); } addItems(items) { items.forEach((item) => { this.addItem(item, item.amount); }); game.playerHud.inventory.buildInventory(); } consumeItem(item, amountToConsume) { if (item.amount - amountToConsume < 0) { return false; } if (item.amount == amountToConsume) { this.hold.splice(this.hold.indexOf(item), 1); game.playerHud.inventory.buildInventory(); return item; } let oldItem = JSON.parse(JSON.stringify(item)); item.amount -= amountToConsume; game.playerHud.inventory.buildInventory(); return oldItem; } }