Files
esengine/source/src/ECS/Utils/Matcher.ts

85 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-07-23 11:00:46 +08:00
module es {
2020-07-28 16:25:20 +08:00
export class Matcher {
2021-05-07 16:23:15 +08:00
protected allSet: (new (...args: any[]) => Component)[] = [];
protected exclusionSet: (new (...args: any[]) => Component)[] = [];
protected oneSet: (new (...args: any[]) => Component)[] = [];
2020-06-08 20:11:58 +08:00
2020-07-28 16:25:20 +08:00
public static empty() {
2020-07-23 11:00:46 +08:00
return new Matcher();
}
2020-06-08 20:11:58 +08:00
2020-07-28 16:25:20 +08:00
public getAllSet() {
2020-07-23 11:00:46 +08:00
return this.allSet;
}
2020-07-28 16:25:20 +08:00
public getExclusionSet() {
2020-07-23 11:00:46 +08:00
return this.exclusionSet;
}
2020-07-28 16:25:20 +08:00
public getOneSet() {
2020-07-23 11:00:46 +08:00
return this.oneSet;
}
public isInterestedEntity(e: Entity) {
return this.isInterested(e.componentBits);
}
2021-05-07 16:23:15 +08:00
public isInterested(components: Bits) {
if (this.allSet.length != 0) {
for (let i = 0, s = this.allSet.length; i < s; ++ i) {
let type = this.allSet[i];
if (!components.get(ComponentTypeManager.getIndexFor(type)))
2020-07-23 11:00:46 +08:00
return false;
}
2020-06-08 20:11:58 +08:00
}
2021-05-07 16:23:15 +08:00
if (this.exclusionSet.length != 0) {
for (let i = 0, s = this.exclusionSet.length; i < s; ++ i) {
let type = this.exclusionSet[i];
if (components.get(ComponentTypeManager.getIndexFor(type)))
2021-05-07 16:23:15 +08:00
return false;
}
}
2020-06-08 20:11:58 +08:00
2021-05-07 16:23:15 +08:00
if (this.oneSet.length != 0) {
for (let i = 0, s = this.oneSet.length; i < s; ++ i) {
let type = this.oneSet[i];
if (components.get(ComponentTypeManager.getIndexFor(type)))
2021-05-07 16:23:15 +08:00
return true;
}
}
2020-06-08 20:11:58 +08:00
2020-07-23 11:00:46 +08:00
return true;
}
2020-07-28 16:25:20 +08:00
public all(...types: any[]): Matcher {
2021-05-07 16:23:15 +08:00
let t;
for (let i = 0, s = types.length; i < s; ++ i) {
t = types[i];
2021-05-07 16:23:15 +08:00
this.allSet.push(t);
}
2020-07-23 11:00:46 +08:00
return this;
}
2020-07-28 16:25:20 +08:00
public exclude(...types: any[]) {
2021-05-07 16:23:15 +08:00
let t;
for (let i = 0, s = types.length; i < s; ++ i) {
t = types[i];
2021-05-07 16:23:15 +08:00
this.exclusionSet.push(t);
}
2020-07-23 11:00:46 +08:00
return this;
}
2020-07-28 16:25:20 +08:00
public one(...types: any[]) {
for (let i = 0, s = types.length; i < s; ++ i) {
const t = types[i];
2021-05-07 16:23:15 +08:00
this.oneSet.push(t);
}
2020-07-23 11:00:46 +08:00
return this;
}
}
2020-07-23 11:00:46 +08:00
}