48 lines
1.2 KiB
JavaScript
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'},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
}
|