2024-06-10 22:42:30 -05:00
|
|
|
export default class Query {
|
|
|
|
|
|
|
|
$$criteria = {with: [], without: []};
|
2024-06-26 07:41:07 -05:00
|
|
|
$$ecs;
|
2024-08-04 20:54:56 -05:00
|
|
|
$$map = new Map();
|
2024-06-10 22:42:30 -05:00
|
|
|
|
2024-06-26 07:41:07 -05:00
|
|
|
constructor(parameters, ecs) {
|
|
|
|
this.$$ecs = ecs;
|
2024-06-10 22:42:30 -05:00
|
|
|
for (let i = 0; i < parameters.length; ++i) {
|
|
|
|
const parameter = parameters[i];
|
|
|
|
switch (parameter.charCodeAt(0)) {
|
|
|
|
case '!'.charCodeAt(0):
|
2024-06-26 07:41:07 -05:00
|
|
|
this.$$criteria.without.push(ecs.Components[parameter.slice(1)]);
|
2024-06-10 22:42:30 -05:00
|
|
|
break;
|
|
|
|
default:
|
2024-06-26 07:41:07 -05:00
|
|
|
this.$$criteria.with.push(ecs.Components[parameter]);
|
2024-06-10 22:42:30 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get count() {
|
2024-08-04 20:54:56 -05:00
|
|
|
return this.$$map.size;
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
|
|
|
|
2024-06-11 19:10:57 -05:00
|
|
|
deindex(entityIds) {
|
2024-07-22 01:12:17 -05:00
|
|
|
for (const entityId of entityIds) {
|
2024-08-04 20:54:56 -05:00
|
|
|
this.$$map.delete(entityId);
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-11 19:10:57 -05:00
|
|
|
reindex(entityIds) {
|
2024-06-10 22:42:30 -05:00
|
|
|
if (0 === this.$$criteria.with.length && 0 === this.$$criteria.without.length) {
|
2024-06-11 19:10:57 -05:00
|
|
|
for (const entityId of entityIds) {
|
2024-08-04 20:54:56 -05:00
|
|
|
this.$$map.set(entityId, this.$$ecs.get(entityId));
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2024-06-11 19:10:57 -05:00
|
|
|
for (const entityId of entityIds) {
|
2024-06-10 22:42:30 -05:00
|
|
|
let should = true;
|
|
|
|
for (let j = 0; j < this.$$criteria.with.length; ++j) {
|
2024-06-11 19:10:57 -05:00
|
|
|
if ('undefined' === typeof this.$$criteria.with[j].get(entityId)) {
|
2024-06-10 22:42:30 -05:00
|
|
|
should = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (should) {
|
|
|
|
for (let j = 0; j < this.$$criteria.without.length; ++j) {
|
2024-06-11 19:10:57 -05:00
|
|
|
if ('undefined' !== typeof this.$$criteria.without[j].get(entityId)) {
|
2024-06-10 22:42:30 -05:00
|
|
|
should = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (should) {
|
2024-08-04 20:54:56 -05:00
|
|
|
this.$$map.set(entityId, this.$$ecs.get(entityId));
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
|
|
|
else if (!should) {
|
2024-08-04 20:54:56 -05:00
|
|
|
this.$$map.delete(entityId);
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select() {
|
2024-08-04 20:54:56 -05:00
|
|
|
return this.$$map.values();
|
2024-06-10 22:42:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|