From b65714589f04816ac5351b95e7abc14c988a6f92 Mon Sep 17 00:00:00 2001 From: cha0s Date: Tue, 2 Jul 2024 17:46:31 -0500 Subject: [PATCH] fix: change deferral --- app/ecs/component.js | 5 +++-- app/ecs/ecs.js | 14 ++++++++++++++ app/ecs/ecs.test.js | 10 +++++----- app/engine.js | 40 ++++++++++++++++------------------------ 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/app/ecs/component.js b/app/ecs/component.js index 6a4b56d..698a858 100644 --- a/app/ecs/component.js +++ b/app/ecs/component.js @@ -45,11 +45,12 @@ export default class Component { for (let k = 0; k < keys.length; ++k) { const j = keys[k]; const {defaultValue} = properties[j]; + const instance = this.data[allocated[i]]; if (j in values) { - this.data[allocated[i]][j] = values[j]; + instance[j] = values[j]; } else if ('undefined' !== typeof defaultValue) { - this.data[allocated[i]][j] = defaultValue; + instance[j] = defaultValue; } } promises.push(this.load(this.data[allocated[i]])); diff --git a/app/ecs/ecs.js b/app/ecs/ecs.js index c9df686..e9fa908 100644 --- a/app/ecs/ecs.js +++ b/app/ecs/ecs.js @@ -11,6 +11,8 @@ export default class Ecs { Components = {}; + deferredChanges = {} + diff = {}; Systems = {}; @@ -113,6 +115,7 @@ export default class Ecs { const creating = {}; for (let i = 0; i < specificsList.length; i++) { const [entityId, components] = specificsList[i]; + this.deferredChanges[entityId] = []; const componentNames = []; for (const componentName in components) { if (this.Components[componentName]) { @@ -134,6 +137,14 @@ export default class Ecs { promises.push(this.Components[i].createMany(creating[i])); } await Promise.all(promises); + for (let i = 0; i < specificsList.length; i++) { + const [entityId] = specificsList[i]; + const changes = this.deferredChanges[entityId]; + delete this.deferredChanges[entityId]; + for (const components of changes) { + this.markChange(entityId, components); + } + } this.reindex(entityIds); return entityIds; } @@ -255,6 +266,9 @@ export default class Ecs { } markChange(entityId, components) { + if (this.deferredChanges[entityId]) { + this.deferredChanges[entityId].push(components); + } // Deleted? if (false === components) { this.diff[entityId] = false; diff --git a/app/ecs/ecs.test.js b/app/ecs/ecs.test.js index e1a84c1..f72dcc0 100644 --- a/app/ecs/ecs.test.js +++ b/app/ecs/ecs.test.js @@ -352,9 +352,9 @@ test('generates diffs for deletions', async () => { .to.deep.equal({[entity]: false}); }); -test('applies creation patches', () => { +test('applies creation patches', async () => { const ecs = new Ecs({Components: {Position}}); - ecs.apply({16: {Position: {x: 64}}}); + await ecs.apply({16: {Position: {x: 64}}}); expect(Array.from(ecs.entities).length) .to.equal(1); expect(ecs.get(16).Position.x) @@ -379,12 +379,12 @@ test('applies entity deletion patches', () => { .to.equal(0); }); -test('applies component deletion patches', () => { +test('applies component deletion patches', async () => { const ecs = new Ecs({Components: {Empty, Position}}); - ecs.createSpecific(16, {Empty: {}, Position: {x: 64}}); + await ecs.createSpecific(16, {Empty: {}, Position: {x: 64}}); expect(ecs.get(16).constructor.componentNames) .to.deep.equal(['Empty', 'Position']); - ecs.apply({16: {Empty: false}}); + await ecs.apply({16: {Empty: false}}); expect(ecs.get(16).constructor.componentNames) .to.deep.equal(['Position']); }); diff --git a/app/engine.js b/app/engine.js index b372d75..b875aab 100644 --- a/app/engine.js +++ b/app/engine.js @@ -55,7 +55,7 @@ export default class Engine { }); } - async acceptActions() { + acceptActions() { for (const [ entity, payload, @@ -105,28 +105,21 @@ export default class Engine { } async connectPlayer(connection, id) { - this.connectingPlayers.push([connection, id]); - } - - async connectPlayers() { - for (const [connection, id] of this.connectingPlayers) { - const entityJson = await this.loadPlayer(id); - if (!this.ecses[entityJson.Ecs.path]) { - await this.loadEcs(entityJson.Ecs.path); - } - const ecs = this.ecses[entityJson.Ecs.path]; - const entity = await ecs.create(entityJson); - this.connections.push(connection); - this.connectedPlayers.set( - connection, - { - entity: ecs.get(entity), - id, - memory: new Set(), - }, - ); + const entityJson = await this.loadPlayer(id); + if (!this.ecses[entityJson.Ecs.path]) { + await this.loadEcs(entityJson.Ecs.path); } - this.connectingPlayers = []; + const ecs = this.ecses[entityJson.Ecs.path]; + const entity = await ecs.create(entityJson); + this.connections.push(connection); + this.connectedPlayers.set( + connection, + { + entity: ecs.get(entity), + id, + memory: new Set(), + }, + ); } createEcs() { @@ -300,8 +293,7 @@ export default class Engine { start() { const loop = async () => { - await this.connectPlayers(); - await this.acceptActions(); + this.acceptActions(); const elapsed = (Date.now() - this.last) / 1000; this.last = Date.now(); this.tick(elapsed);