Rooms, items and some player stuff
This commit is contained in:
27
src/framework/world/query.js
Normal file
27
src/framework/world/query.js
Normal file
@@ -0,0 +1,27 @@
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user