refactor: readAsset to first-class

This commit is contained in:
cha0s 2024-06-27 10:53:52 -05:00
parent c6557bee39
commit 15674fb1d7
4 changed files with 17 additions and 10 deletions

View File

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

View File

@ -32,7 +32,12 @@ export default class Engine {
super.transmit(connection, encode(packet)); super.transmit(connection, encode(packet));
} }
} }
this.server = new SilphiusServer(); const server = this.server = new SilphiusServer();
this.Ecs = class EngineEcs extends Ecs {
readAsset(uri) {
return server.readAsset(uri);
}
}
this.server.addPacketListener('Action', (connection, payload) => { this.server.addPacketListener('Action', (connection, payload) => {
this.incomingActions.push([this.connectedPlayers.get(connection).entity, payload]); this.incomingActions.push([this.connectedPlayers.get(connection).entity, payload]);
}); });
@ -112,7 +117,7 @@ export default class Engine {
} }
createEcs() { createEcs() {
return new Ecs({Components, Systems}); return new this.Ecs({Components, Systems});
} }
async createHomestead(id) { async createHomestead(id) {
@ -217,11 +222,10 @@ export default class Engine {
} }
async loadEcs(path) { async loadEcs(path) {
const ecs = this.ecses[path] = await Ecs.deserialize( this.ecses[path] = await this.Ecs.deserialize(
this.createEcs(), this.createEcs(),
await this.server.readData(path), await this.server.readData(path),
); );
ecs.get(1).Engine.readAsset = (uri) => this.server.readAsset(uri);
} }
async loadPlayer(id) { async loadPlayer(id) {
@ -240,7 +244,7 @@ export default class Engine {
} }
async saveEcs(path, ecs) { async saveEcs(path, ecs) {
const view = Ecs.serialize(ecs); const view = this.Ecs.serialize(ecs);
await this.server.writeData(path, view); await this.server.writeData(path, view);
} }

View File

@ -154,9 +154,6 @@ export default function Ui({disconnected}) {
return; return;
} }
await ecs.apply(payload.ecs); 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'] ?? []) { for (const listener of client.listeners[':Ecs'] ?? []) {
listener(payload.ecs); listener(payload.ecs);
} }

View File

@ -12,6 +12,12 @@ import Systems from '@/ecs-systems/index.js';
import Ui from '@/react-components/ui.jsx'; import Ui from '@/react-components/ui.jsx';
import {juggleSession} from '@/session.server'; import {juggleSession} from '@/session.server';
class ClientEcs extends Ecs {
readAsset(uri) {
return fetch(new URL(uri, window.location.origin));
}
}
export async function loader({request}) { export async function loader({request}) {
await juggleSession(request); await juggleSession(request);
return json({}); return json({});
@ -22,7 +28,7 @@ export default function PlaySpecific() {
const [client, setClient] = useState(); const [client, setClient] = useState();
const mainEntityTuple = useState(); const mainEntityTuple = useState();
const debugTuple = useState(false); const debugTuple = useState(false);
const ecsTuple = useState(new Ecs({Components, Systems})); const ecsTuple = useState(new ClientEcs({Components, Systems}));
const [disconnected, setDisconnected] = useState(false); const [disconnected, setDisconnected] = useState(false);
const params = useParams(); const params = useParams();
const [type, url] = params['*'].split('/'); const [type, url] = params['*'].split('/');