123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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;
- }
- }
|