fix: change deferral

This commit is contained in:
cha0s 2024-07-02 17:46:31 -05:00
parent eb40df98cf
commit b65714589f
4 changed files with 38 additions and 31 deletions

View File

@ -45,11 +45,12 @@ export default class Component {
for (let k = 0; k < keys.length; ++k) { for (let k = 0; k < keys.length; ++k) {
const j = keys[k]; const j = keys[k];
const {defaultValue} = properties[j]; const {defaultValue} = properties[j];
const instance = this.data[allocated[i]];
if (j in values) { if (j in values) {
this.data[allocated[i]][j] = values[j]; instance[j] = values[j];
} }
else if ('undefined' !== typeof defaultValue) { else if ('undefined' !== typeof defaultValue) {
this.data[allocated[i]][j] = defaultValue; instance[j] = defaultValue;
} }
} }
promises.push(this.load(this.data[allocated[i]])); promises.push(this.load(this.data[allocated[i]]));

View File

@ -11,6 +11,8 @@ export default class Ecs {
Components = {}; Components = {};
deferredChanges = {}
diff = {}; diff = {};
Systems = {}; Systems = {};
@ -113,6 +115,7 @@ export default class Ecs {
const creating = {}; const creating = {};
for (let i = 0; i < specificsList.length; i++) { for (let i = 0; i < specificsList.length; i++) {
const [entityId, components] = specificsList[i]; const [entityId, components] = specificsList[i];
this.deferredChanges[entityId] = [];
const componentNames = []; const componentNames = [];
for (const componentName in components) { for (const componentName in components) {
if (this.Components[componentName]) { if (this.Components[componentName]) {
@ -134,6 +137,14 @@ export default class Ecs {
promises.push(this.Components[i].createMany(creating[i])); promises.push(this.Components[i].createMany(creating[i]));
} }
await Promise.all(promises); 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); this.reindex(entityIds);
return entityIds; return entityIds;
} }
@ -255,6 +266,9 @@ export default class Ecs {
} }
markChange(entityId, components) { markChange(entityId, components) {
if (this.deferredChanges[entityId]) {
this.deferredChanges[entityId].push(components);
}
// Deleted? // Deleted?
if (false === components) { if (false === components) {
this.diff[entityId] = false; this.diff[entityId] = false;

View File

@ -352,9 +352,9 @@ test('generates diffs for deletions', async () => {
.to.deep.equal({[entity]: false}); .to.deep.equal({[entity]: false});
}); });
test('applies creation patches', () => { test('applies creation patches', async () => {
const ecs = new Ecs({Components: {Position}}); 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) expect(Array.from(ecs.entities).length)
.to.equal(1); .to.equal(1);
expect(ecs.get(16).Position.x) expect(ecs.get(16).Position.x)
@ -379,12 +379,12 @@ test('applies entity deletion patches', () => {
.to.equal(0); .to.equal(0);
}); });
test('applies component deletion patches', () => { test('applies component deletion patches', async () => {
const ecs = new Ecs({Components: {Empty, Position}}); 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) expect(ecs.get(16).constructor.componentNames)
.to.deep.equal(['Empty', 'Position']); .to.deep.equal(['Empty', 'Position']);
ecs.apply({16: {Empty: false}}); await ecs.apply({16: {Empty: false}});
expect(ecs.get(16).constructor.componentNames) expect(ecs.get(16).constructor.componentNames)
.to.deep.equal(['Position']); .to.deep.equal(['Position']);
}); });

View File

@ -55,7 +55,7 @@ export default class Engine {
}); });
} }
async acceptActions() { acceptActions() {
for (const [ for (const [
entity, entity,
payload, payload,
@ -105,11 +105,6 @@ export default class Engine {
} }
async connectPlayer(connection, id) { async connectPlayer(connection, id) {
this.connectingPlayers.push([connection, id]);
}
async connectPlayers() {
for (const [connection, id] of this.connectingPlayers) {
const entityJson = await this.loadPlayer(id); const entityJson = await this.loadPlayer(id);
if (!this.ecses[entityJson.Ecs.path]) { if (!this.ecses[entityJson.Ecs.path]) {
await this.loadEcs(entityJson.Ecs.path); await this.loadEcs(entityJson.Ecs.path);
@ -126,8 +121,6 @@ export default class Engine {
}, },
); );
} }
this.connectingPlayers = [];
}
createEcs() { createEcs() {
return new this.Ecs({Components, Systems}); return new this.Ecs({Components, Systems});
@ -300,8 +293,7 @@ export default class Engine {
start() { start() {
const loop = async () => { const loop = async () => {
await this.connectPlayers(); this.acceptActions();
await this.acceptActions();
const elapsed = (Date.now() - this.last) / 1000; const elapsed = (Date.now() - this.last) / 1000;
this.last = Date.now(); this.last = Date.now();
this.tick(elapsed); this.tick(elapsed);