feat: engine readAsset

This commit is contained in:
cha0s 2024-06-27 07:37:20 -05:00
parent d8528ad7a5
commit c8622c6814
4 changed files with 15 additions and 3 deletions

View File

@ -48,8 +48,9 @@ export default class Inventory extends Component {
if (!(slot in slots)) {
return undefined;
}
const {Engine: {readAsset}} = Component.ecs.get(1);
const json = await (
fetch([slots[slot].source, 'item.json'].join('/'))
readAsset([slots[slot].source, 'item.json'].join('/'))
.then((response) => (response.ok ? response.json() : {}))
);
const item = {

View File

@ -217,11 +217,11 @@ export default class Engine {
}
async loadEcs(path) {
this.ecses[path] = Ecs.deserialize(
const ecs = this.ecses[path] = await Ecs.deserialize(
this.createEcs(),
await this.server.readData(path),
);
this.ecses[path].get(1).Engine.engine = this;
ecs.get(1).Engine.readAsset = (uri) => this.server.readAsset(uri);
}
async loadPlayer(id) {

View File

@ -154,6 +154,9 @@ export default function Ui({disconnected}) {
return;
}
await ecs.apply(payload.ecs);
if (payload.ecs[1]) {
ecs.get(1).Engine.readAsset = (uri) => fetch(new URL(uri, window.location.origin));
}
for (const listener of client.listeners[':Ecs'] ?? []) {
listener(payload.ecs);
}

View File

@ -48,6 +48,14 @@ class SocketServer extends Server {
static qualify(path) {
return join(import.meta.dirname, 'data', 'remote', 'UNIVERSE', path);
}
async readAsset(path) {
const url = new URL(path, 'https://localhost:3000')
console.log({url});
if ('production' === process.env.NODE_ENV) {
url.protocol = 'http:';
}
return fetch(url.href);
}
async readData(path) {
const qualified = this.constructor.qualify(path);
await this.ensurePath(dirname(qualified));