silphius/app/ecs-components/inventory.js
2024-06-22 20:33:44 -05:00

48 lines
1.2 KiB
JavaScript

import Schema from '@/ecs/schema.js';
export default function(Component) {
return class Inventory extends Component {
instanceFromSchema() {
const Instance = super.instanceFromSchema();
Instance.prototype.item = function (slot) {
const {slots} = this;
const instance = this;
for (const index in slots) {
const item = slots[index];
if (slot === item.slotIndex) {
const proxy = new Proxy(item, {
set(target, property, value) {
const newSlots = [...slots];
newSlots[index] = {
...target,
[property]: value,
};
instance.slots = newSlots;
return true;
},
});
return proxy;
}
}
};
return Instance;
}
static schema = new Schema({
type: 'object',
properties: {
slots: {
type: 'array',
subtype: {
type: 'object',
properties: {
quantity: {type: 'uint16'},
slotIndex: {type: 'uint16'},
source: {type: 'string'},
},
},
},
},
});
}
}