28 lines
1.0 KiB
JavaScript
28 lines
1.0 KiB
JavaScript
|
export class Query {
|
||
|
constructor(include, exclude) {
|
||
|
this.include = include;
|
||
|
this.exclude = exclude;
|
||
|
this.isDirty = true;
|
||
|
this.results = new Array();
|
||
|
this.includeComponentIds = include.map((component) => component.id);
|
||
|
this.excludeComponentIds = exclude.map((component) => component.id);
|
||
|
}
|
||
|
execute(world) {
|
||
|
if (!this.isDirty && this.results) {
|
||
|
return this.results;
|
||
|
}
|
||
|
let filtered;
|
||
|
const entities = world.entities.filter(entity => {
|
||
|
let ids = entity.components.map(component => component.id);
|
||
|
let includes = ids.map(id => this.includeComponentIds.includes(id)).includes(true);
|
||
|
let excludes = ids.map(id => this.excludeComponentIds.includes(id)).includes(true);
|
||
|
return includes && !excludes;
|
||
|
});
|
||
|
if (entities.length > 0) {
|
||
|
this.isDirty = false;
|
||
|
this.results = entities;
|
||
|
}
|
||
|
return entities;
|
||
|
}
|
||
|
}
|