refactor: item access

This commit is contained in:
cha0s 2024-06-25 05:46:03 -05:00
parent d6011f905e
commit 931a6c0e81
5 changed files with 62 additions and 39 deletions

View File

@ -0,0 +1 @@
export default {};

View File

@ -44,13 +44,21 @@ export default function(Component) {
instanceFromSchema() { instanceFromSchema() {
const Instance = super.instanceFromSchema(); const Instance = super.instanceFromSchema();
const Component = this; const Component = this;
Instance.prototype.item = function (slot) { Instance.prototype.item = async function (slot) {
const {slots} = this; const {slots} = this;
if (!(slot in slots)) { if (!(slot in slots)) {
return undefined; return undefined;
} }
const json = await (
fetch([slots[slot].source, 'item.json'].join('/'))
.then((response) => (response.ok ? response.json() : {}))
);
const item = {
...slots[slot],
...json,
};
const instance = this; const instance = this;
const proxy = new Proxy(slots[slot], { const proxy = new Proxy(item, {
set(target, property, value) { set(target, property, value) {
target[property] = value; target[property] = value;
if ('qty' === property && value <= 0) { if ('qty' === property && value <= 0) {

View File

@ -1,3 +1,21 @@
export default { import Schema from '@/ecs/schema.js';
activeSlot: {type: 'uint16'},
}; export default function(Component) {
return class Wielder extends Component {
instanceFromSchema() {
const Instance = super.instanceFromSchema();
const Component = this;
Instance.prototype.activeItem = async function () {
const {Inventory, Wielder} = Component.ecs.get(this.entity);
return Inventory.item(Wielder.activeSlot + 1);
};
return Instance;
}
static schema = new Schema({
type: 'object',
properties: {
activeSlot: {type: 'uint16'},
},
});
}
}

View File

@ -72,6 +72,7 @@ export default class Engine {
const area = {x: 100, y: 60}; const area = {x: 100, y: 60};
ecs.create({ ecs.create({
AreaSize: {x: area.x * 16, y: area.y * 16}, AreaSize: {x: area.x * 16, y: area.y * 16},
Engine: {},
TileLayers: { TileLayers: {
layers: [ layers: [
{ {
@ -169,6 +170,7 @@ export default class Engine {
this.createEcs(), this.createEcs(),
await this.server.readData(path), await this.server.readData(path),
); );
this.ecses[path].get(1).Engine.engine = this;
} }
async loadPlayer(id) { async loadPlayer(id) {
@ -246,21 +248,24 @@ export default class Engine {
break; break;
} }
case 'use': { case 'use': {
const item = Inventory.item(Wielder.activeSlot + 1); Inventory.item(Wielder.activeSlot + 1).then(async (item) => {
if (item) { if (item) {
this.server.readAsset([ const code = await(
item.source, this.server.readAsset([
payload.value ? 'start.js' : 'stop.js', item.source,
].join('/')) payload.value ? 'start.js' : 'stop.js',
.then((response) => response.ok ? response.text() : '') ].join('/'))
.then((code) => { .then((script) => (script.ok ? script.text() : ''))
if (code) { );
Ticking.addTickingPromise( if (code) {
Script.tickingPromise(code, {item, wielder: entity}), const context = {
); item,
} wielder: entity,
}); };
} Ticking.addTickingPromise(Script.tickingPromise(code, context));
}
}
});
break; break;
} }
} }

View File

@ -11,33 +11,20 @@ import TargetingGhost from './targeting-ghost.jsx';
import TargetingGrid from './targeting-grid.jsx'; import TargetingGrid from './targeting-grid.jsx';
import TileLayer from './tile-layer.jsx'; import TileLayer from './tile-layer.jsx';
function entityActiveItem(entity) {
const {Inventory, Wielder} = entity;
return Inventory.item(Wielder.activeSlot + 1);
}
export default function EcsComponent() { export default function EcsComponent() {
const [ecs] = useEcs(); const [ecs] = useEcs();
const [entities, setEntities] = useState({}); const [entities, setEntities] = useState({});
const [activeItem, setActiveItem] = useState(false);
const [activeTool, setActiveTool] = useState(false); const [activeTool, setActiveTool] = useState(false);
const [mainEntity] = useMainEntity(); const [mainEntity] = useMainEntity();
useEffect(() => { useEffect(() => {
if (mainEntity) { if (mainEntity) {
setActiveItem(entityActiveItem(ecs.get(mainEntity))); ecs.get(mainEntity)
.Wielder.activeItem()
.then(({tool} = {}) => {
setActiveTool(tool);
});
} }
}, [ecs, mainEntity]); }, [ecs, mainEntity]);
useEffect(() => {
if (!activeItem) {
setActiveTool(undefined);
return;
}
fetch([activeItem.source, 'item.json'].join('/'))
.then((response) => (response.ok ? response.json() : {}))
.then(({tool}) => {
setActiveTool(tool);
});
}, [activeItem]);
usePacket('Tick', (payload) => { usePacket('Tick', (payload) => {
if (0 === Object.keys(payload.ecs).length) { if (0 === Object.keys(payload.ecs).length) {
return; return;
@ -47,7 +34,11 @@ export default function EcsComponent() {
&& payload.ecs[mainEntity] && payload.ecs[mainEntity]
&& (payload.ecs[mainEntity].Inventory || payload.ecs[mainEntity].Wielder) && (payload.ecs[mainEntity].Inventory || payload.ecs[mainEntity].Wielder)
) { ) {
setActiveItem(entityActiveItem(ecs.get(mainEntity))); ecs.get(mainEntity)
.Wielder.activeItem()
.then(({tool} = {}) => {
setActiveTool(tool);
});
} }
const updatedEntities = {...entities}; const updatedEntities = {...entities};
for (const id in payload.ecs) { for (const id in payload.ecs) {