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

79 lines
2.0 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 s of this.allSet) {
if (!components.get(ComponentTypeManager.getIndexFor(s)))
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 s of this.exclusionSet) {
if (components.get(ComponentTypeManager.getIndexFor(s)))
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 s of this.oneSet) {
if (components.get(ComponentTypeManager.getIndexFor(s)))
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 (t of types) {
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 (t of types) {
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[]) {
2021-05-07 16:23:15 +08:00
for (const t of types) {
this.oneSet.push(t);
}
2020-07-23 11:00:46 +08:00
return this;
}
}
2020-07-23 11:00:46 +08:00
}