assassin-bug/framework/world/query.js

28 lines
1.0 KiB
JavaScript
Raw Normal View History

2022-11-26 01:22:02 +00:00
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;
}
}