refactor: inventory slots as map

This commit is contained in:
cha0s 2024-06-24 04:20:07 -05:00
parent 9fe10408f7
commit d21e454c8a
3 changed files with 56 additions and 56 deletions

View File

@ -2,62 +2,64 @@ import Schema from '@/ecs/schema.js';
export default function(Component) { export default function(Component) {
return class Inventory extends Component { return class Inventory extends Component {
insertMany(entities) { insertMany(entities) {
for (const [id, {slotChanged}] of entities) { for (const [id, {slotChange}] of entities) {
if (slotChanged) { if (slotChange) {
for (const slotIndex in slotChanged) { const {slots} = this.get(id);
this.get(id).slots[slotIndex] = { for (const slotIndex in slotChange) {
...this.get(id).slots[slotIndex], if (false === slotChange[slotIndex]) {
...slotChanged[slotIndex], delete slots[slotIndex];
}; }
else {
slots[slotIndex] = {
...slots[slotIndex],
...slotChange[slotIndex],
};
}
} }
} }
} }
return super.insertMany(entities); return super.insertMany(entities);
} }
markChange(entityId, key, value) {
if ('slotChange' === key) {
const {index, change} = value;
return super.markChange(entityId, key, {[index]: change});
}
return super.markChange(entityId, key, value);
}
mergeDiff(original, update) { mergeDiff(original, update) {
if (update.slotChange) { if (!update.slotChange) {
if (!original.slotChange) { return super.mergeDiff(original, update);
original.slotChange = {}; }
const slotChange = {
...original.slotChange,
};
for (const index in update.slotChange) {
if (false === update.slotChange[index]) {
slotChange[index] = false;
} }
const merged = {}; else {
for (const index in update.slotChange) { slotChange[index] = {
merged[index] = { ...slotChange[index],
...original.slotChange[index],
...update.slotChange[index], ...update.slotChange[index],
}; };
} }
return {slotChanged: merged};
} }
return super.mergeDiff(original, update); return {slotChange};
} }
instanceFromSchema() { instanceFromSchema() {
const Instance = super.instanceFromSchema(); const Instance = super.instanceFromSchema();
const Component = this; const Component = this;
Instance.prototype.item = function (slot) { Instance.prototype.item = function (slot) {
const {slots} = this; const {slots} = this;
const instance = this; if (!(slot in slots)) {
for (const index in slots) { return undefined;
const item = slots[index];
if (slot === item.slotIndex) {
const proxy = new Proxy(item, {
set(target, property, value) {
target[property] = value;
const change = {[property]: value};
Component.markChange(instance.entity, 'slotChange', {index, change});
return true;
},
});
return proxy;
}
} }
const instance = this;
const proxy = new Proxy(slots[slot], {
set(target, property, value) {
target[property] = value;
Component.markChange(instance.entity, 'slotChange', {[slot]: {[property]: value}});
return true;
},
});
return proxy;
}; };
return Instance; return Instance;
} }
@ -65,12 +67,11 @@ export default function(Component) {
type: 'object', type: 'object',
properties: { properties: {
slots: { slots: {
type: 'array', type: 'map',
subtype: { value: {
type: 'object', type: 'object',
properties: { properties: {
quantity: {type: 'uint16'}, quantity: {type: 'uint16'},
slotIndex: {type: 'uint16'},
source: {type: 'string'}, source: {type: 'string'},
}, },
}, },

View File

@ -112,13 +112,12 @@ export default class Engine {
Ecs: {path: join('homesteads', `${id}`)}, Ecs: {path: join('homesteads', `${id}`)},
Momentum: {}, Momentum: {},
Inventory: { Inventory: {
slots: [ slots: {
{ 1: {
qty: 500, qty: 500,
slotIndex: 0,
source: '/assets/potion', source: '/assets/potion',
} },
] },
}, },
Health: {health: 100}, Health: {health: 100},
Position: {x: 368, y: 368}, Position: {x: 368, y: 368},

View File

@ -148,23 +148,23 @@ export default function Ui({disconnected}) {
if (localMainEntity === id) { if (localMainEntity === id) {
if (update.Inventory) { if (update.Inventory) {
const newHotbarSlots = [...hotbarSlots]; const newHotbarSlots = [...hotbarSlots];
const {slots, slotChanged} = update.Inventory; const {slots, slotChange} = update.Inventory;
if (slotChanged) { if (slotChange) {
for (const slotIndex in slotChanged) { for (const slotIndex in slotChange) {
newHotbarSlots[slotIndex] = { newHotbarSlots[slotIndex] = {
...newHotbarSlots[slotIndex], ...newHotbarSlots[slotIndex],
...slotChanged[slotIndex], ...slotChange[slotIndex],
}; };
} }
} }
if (slots) { if (slots) {
slots for (const slotIndex in slots) {
.forEach(({qty, slotIndex, source}) => { const {qty, source} = slots[slotIndex];
newHotbarSlots[slotIndex] = { newHotbarSlots[slotIndex] = {
image: source + '/icon.png', image: source + '/icon.png',
qty, qty,
}; };
}); }
} }
setHotbarSlots(newHotbarSlots); setHotbarSlots(newHotbarSlots);
} }