refactor: ecs
This commit is contained in:
parent
da6d15e910
commit
39a681dba4
|
@ -18,12 +18,7 @@ export default class Ecs {
|
||||||
|
|
||||||
constructor(Components) {
|
constructor(Components) {
|
||||||
for (const i in Components) {
|
for (const i in Components) {
|
||||||
const comp = new Components[i]();
|
this.Components[i] = this.trackDirtyEntities(Components[i]);
|
||||||
comp.setDirty = (entity) => {
|
|
||||||
comp.$$dirty = true;
|
|
||||||
this.dirty.add(entity);
|
|
||||||
};
|
|
||||||
this.Components[i] = comp;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,25 +141,24 @@ export default class Ecs {
|
||||||
}
|
}
|
||||||
|
|
||||||
destroyMany(entities) {
|
destroyMany(entities) {
|
||||||
const map = {};
|
const destroying = {};
|
||||||
for (let i = 0; i < entities.length; i++) {
|
for (let i = 0; i < entities.length; i++) {
|
||||||
const entity = entities[i];
|
const entity = entities[i];
|
||||||
if (!this.$$entities[entity]) {
|
if (!this.$$entities[entity]) {
|
||||||
throw new Error(`can't destroy non-existent entity ${entity}`);
|
throw new Error(`can't destroy non-existent entity ${entity}`);
|
||||||
}
|
}
|
||||||
for (let j = 0; j < this.$$entities[entity].length; j++) {
|
for (let j = 0; j < this.$$entities[entity].length; j++) {
|
||||||
const key = this.$$entities[entity][j];
|
const component = this.$$entities[entity][j];
|
||||||
if (!map[key]) {
|
if (!destroying[component]) {
|
||||||
map[key] = [];
|
destroying[component] = [];
|
||||||
}
|
}
|
||||||
map[key].push(entity);
|
destroying[component].push(entity);
|
||||||
}
|
}
|
||||||
this.$$pool.push(entity);
|
this.$$pool.push(entity);
|
||||||
delete this.$$entities[entity];
|
delete this.$$entities[entity];
|
||||||
this.dirty.delete(entity);
|
|
||||||
}
|
}
|
||||||
for (const i in map) {
|
for (const i in destroying) {
|
||||||
this.Components[i].destroyMany(map[i]);
|
this.Components[i].destroyMany(destroying[i]);
|
||||||
}
|
}
|
||||||
for (let i = 0; i < this.$$systems.length; i++) {
|
for (let i = 0; i < this.$$systems.length; i++) {
|
||||||
this.$$systems[i].deindex(entities);
|
this.$$systems[i].deindex(entities);
|
||||||
|
@ -189,14 +183,13 @@ export default class Ecs {
|
||||||
entity = entities[i];
|
entity = entities[i];
|
||||||
}
|
}
|
||||||
if (onlyDirty && !this.dirty.has(entity)) {
|
if (onlyDirty && !this.dirty.has(entity)) {
|
||||||
// eslint-disable-next-line no-continue
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
view.setBigUint64(cursor, BigInt(entity), true);
|
view.setBigUint64(cursor, BigInt(entity), true);
|
||||||
cursor += 8;
|
cursor += 8;
|
||||||
const components = this.$$entities[entity];
|
const components = this.$$entities[entity];
|
||||||
view.setUint16(cursor, components.length, true);
|
view.setUint16(cursor, components.length, true);
|
||||||
const componentLengthIndex = cursor;
|
const componentsWrittenIndex = cursor;
|
||||||
cursor += 2;
|
cursor += 2;
|
||||||
if (0 === components.length) {
|
if (0 === components.length) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -215,7 +208,7 @@ export default class Ecs {
|
||||||
instance.serializer.encode(source, view, cursor);
|
instance.serializer.encode(source, view, cursor);
|
||||||
cursor += instance.schema.sizeOf(source);
|
cursor += instance.schema.sizeOf(source);
|
||||||
}
|
}
|
||||||
view.setUint16(componentLengthIndex, componentsWritten, true);
|
view.setUint16(componentsWrittenIndex, componentsWritten, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,6 +249,13 @@ export default class Ecs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setClean() {
|
||||||
|
for (const i in this.Components) {
|
||||||
|
this.Components[i].setClean();
|
||||||
|
}
|
||||||
|
this.dirty.clear();
|
||||||
|
}
|
||||||
|
|
||||||
sizeOf(entities, onlyDirty) {
|
sizeOf(entities, onlyDirty) {
|
||||||
let size = 0;
|
let size = 0;
|
||||||
if (0 === entities.length) {
|
if (0 === entities.length) {
|
||||||
|
@ -294,16 +294,9 @@ export default class Ecs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tickClean() {
|
|
||||||
for (const i in this.Components) {
|
|
||||||
this.Components[i].setClean();
|
|
||||||
}
|
|
||||||
this.dirty.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
tickFinalize() {
|
tickFinalize() {
|
||||||
this.tickDestruction();
|
this.tickDestruction();
|
||||||
this.tickClean();
|
this.setClean();
|
||||||
}
|
}
|
||||||
|
|
||||||
tickDestruction() {
|
tickDestruction() {
|
||||||
|
@ -320,4 +313,13 @@ export default class Ecs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trackDirtyEntities(Component) {
|
||||||
|
const component = new Component();
|
||||||
|
component.setDirty = (entity) => {
|
||||||
|
component.$$dirty = true;
|
||||||
|
this.dirty.add(entity);
|
||||||
|
};
|
||||||
|
return component;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user