export class EcsWorld { constructor () { this.nextId = 1; this.alive = new Set(); this.stores = new Map(); } entity () { const id = this.nextId++; this.alive.add(id); return id; } destroy (id) { this.alive.delete(id); for (const store of this.stores.values()) { store.delete(id); } } add (id, name, data) { let store = this.stores.get(name); if (!store) { store = new Map(); this.stores.set(name, store); } store.set(id, data); return data; } get (id, name) { return this.stores.get(name)?.get(id); } has (id, name) { return this.stores.get(name)?.has(id) ?? false; } remove (id, name) { this.stores.get(name)?.delete(id); } query (...names) { const primary = this.stores.get(names[0]); if (!primary) { return []; } const results = []; for (const [id, first] of primary) { if (!this.alive.has(id)) { continue; } const record = { id, [names[0]]: first }; let matched = true; for (let i = 1; i < names.length; i++) { const component = this.get(id, names[i]); if (!component) { matched = false; break; } record[names[i]] = component; } if (matched) { results.push(record); } } return results; } }