refactor: fromJSON
This commit is contained in:
parent
7a1e08bc1a
commit
88055be00a
|
@ -283,29 +283,7 @@ export default class Ecs {
|
|||
}
|
||||
|
||||
static deserialize(ecs, view) {
|
||||
const componentNames = Object.keys(ecs.Components);
|
||||
const {entities, systems} = decoder.decode(view.buffer);
|
||||
for (const system of systems) {
|
||||
const System = ecs.system(system);
|
||||
if (System) {
|
||||
System.active = true;
|
||||
}
|
||||
}
|
||||
const specifics = [];
|
||||
let max = 1;
|
||||
for (const id in entities) {
|
||||
max = Math.max(max, parseInt(id));
|
||||
specifics.push([
|
||||
parseInt(id),
|
||||
Object.fromEntries(
|
||||
Object.entries(entities[id])
|
||||
.filter(([componentName]) => componentNames.includes(componentName)),
|
||||
),
|
||||
]);
|
||||
}
|
||||
ecs.$$caret = max + 1;
|
||||
ecs.createManySpecific(specifics);
|
||||
return ecs;
|
||||
return ecs.fromJSON(decoder.decode(view.buffer));
|
||||
}
|
||||
|
||||
destroy(entityId) {
|
||||
|
@ -358,6 +336,32 @@ export default class Ecs {
|
|||
return ids;
|
||||
}
|
||||
|
||||
fromJSON(json) {
|
||||
const componentNames = Object.keys(this.Components);
|
||||
const {entities, systems} = json;
|
||||
for (const system of systems) {
|
||||
const System = this.system(system);
|
||||
if (System) {
|
||||
System.active = true;
|
||||
}
|
||||
}
|
||||
const specifics = [];
|
||||
let max = 1;
|
||||
for (const id in entities) {
|
||||
max = Math.max(max, parseInt(id));
|
||||
specifics.push([
|
||||
parseInt(id),
|
||||
Object.fromEntries(
|
||||
Object.entries(entities[id])
|
||||
.filter(([componentName]) => componentNames.includes(componentName)),
|
||||
),
|
||||
]);
|
||||
}
|
||||
this.$$caret = max + 1;
|
||||
this.createManySpecific(specifics);
|
||||
return this;
|
||||
}
|
||||
|
||||
get(entityId) {
|
||||
return this.$$entities[entityId];
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user