import Component from '@/ecs/component.js'; export default class Inventory extends Component { insertMany(entities) { for (const [id, {cleared, qtyUpdated, swapped}] of entities) { const {$$items, slots} = this.get(id); if (cleared) { for (const slot in cleared) { delete slots[slot]; } } if (qtyUpdated) { for (const slot in qtyUpdated) { slots[slot].qty = qtyUpdated[slot]; } } if (swapped) { for (const [l, r] of swapped) { const tmp = [$$items[l], slots[l]]; [$$items[l], slots[l]] = [$$items[r], slots[r]]; [$$items[r], slots[r]] = tmp; } } } return super.insertMany(entities); } instanceFromSchema() { const Instance = super.instanceFromSchema(); const Component = this; return class InventoryInstance extends Instance { $$items = {}; item(slot) { return this.$$items[slot]; } swapSlots(l, r) { const {$$items, slots} = this; const tmp = [$$items[l], slots[l]]; [$$items[l], slots[l]] = [$$items[r], slots[r]]; [$$items[r], slots[r]] = tmp; Component.markChange(this.entity, 'swapped', [[l, r]]); } } } async load(instance) { const Component = this; const {slots} = instance; class ItemProxy { constructor(slot, json, scripts) { this.json = json; this.scripts = scripts; this.slot = slot; } project(position, direction) { const {TileLayers} = Component.ecs.get(1); const layer = TileLayers.layer(0); const {projection} = this; if (!projection) { return undefined; } let startX = position.x; let startY = position.y; switch (direction) { case 0: startX += projection.distance[1]; startY -= projection.distance[0]; break; case 1: startX += projection.distance[0]; startY += projection.distance[1]; break; case 2: startX -= projection.distance[1]; startY += projection.distance[0]; break; case 3: startX -= projection.distance[0]; startY -= projection.distance[1]; break; } const projected = []; for (const row in projection.grid) { const columns = projection.grid[row]; for (const column in columns) { const targeted = projection.grid[row][column]; if (targeted) { let axe; switch (direction) { case 0: axe = [column, row]; break; case 1: axe = [-row, column]; break; case 2: axe = [-column, -row]; break; case 3: axe = [row, -column]; break; } const x = startX + parseInt(axe[0]); const y = startY + parseInt(axe[1]); if (x < 0 || y < 0 || x >= layer.area.x || y >= layer.area.y) { continue; } projected.push({x, y}); } } } if (this.scripts.projectionCheckInstance) { this.scripts.projectionCheckInstance.context.ecs = Component.ecs; this.scripts.projectionCheckInstance.context.layer = layer; this.scripts.projectionCheckInstance.context.projected = projected; return this.scripts.projectionCheckInstance.evaluateSync(); } else { return projected; } } get icon() { return this.json.icon; } get projection() { return this.json.projection; } get qty() { return this.slot.qty; } set qty(qty) { let slot; for (slot in slots) { if (slots[slot] === this.slot) { break; } } if (qty <= 0) { Component.markChange(instance.entity, 'cleared', {[slot]: true}); delete slots[slot]; delete instance.$$items[slot]; } else { slots[slot].qty = qty; Component.markChange(instance.entity, 'qtyUpdated', {[slot]: qty}); } } } for (const slot in slots) { const json = await this.ecs.readJson(slots[slot].source); const scripts = {}; if (json.projectionCheck) { scripts.projectionCheckInstance = await this.ecs.readScript(json.projectionCheck); } if (json.start) { scripts.startInstance = await this.ecs.readScript(json.start); } if (json.stop) { scripts.stopInstance = await this.ecs.readScript(json.stop); } instance.$$items[slot] = new ItemProxy(slots[slot], json, scripts); } } mergeDiff(original, update) { const merged = { ...original, qtyUpdated: { ...original.qtyUpdated, ...update.qtyUpdated, }, cleared: { ...original.cleared, ...update.cleared, }, swapped: [ ...(original.swapped || []), ...(update.swapped || []), ], }; return merged; } static properties = { slots: { type: 'map', value: { type: 'object', properties: { quantity: {type: 'uint16'}, source: {type: 'string'}, }, }, }, }; }