fix: change deferral
This commit is contained in:
parent
eb40df98cf
commit
b65714589f
|
@ -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]]));
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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']);
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user