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

90 lines
2.8 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; i < this.allSet.length; i++) {
const type = this.allSet[i];
if (!components.get(ComponentTypeManager.getIndexFor(type))) {
2020-07-23 11:00:46 +08:00
return false;
}
2020-07-23 11:00:46 +08:00
}
2020-06-08 20:11:58 +08:00
}
if (this.exclusionSet.length !== 0) {
for (let i = 0; i < this.exclusionSet.length; i++) {
const type = this.exclusionSet[i];
if (components.get(ComponentTypeManager.getIndexFor(type))) {
2021-05-07 16:23:15 +08:00
return false;
}
2021-05-07 16:23:15 +08:00
}
}
2020-06-08 20:11:58 +08:00
if (this.oneSet.length !== 0) {
for (let i = 0; i < this.oneSet.length; i++) {
const type = this.oneSet[i];
if (components.get(ComponentTypeManager.getIndexFor(type))) {
2021-05-07 16:23:15 +08:00
return true;
}
2021-05-07 16:23:15 +08:00
}
return false;
2021-05-07 16:23:15 +08:00
}
2020-06-08 20:11:58 +08:00
2020-07-23 11:00:46 +08:00
return true;
}
/**
*
* @param types
*/
public all(...types: (new (...args: any[]) => Component)[]): Matcher {
this.allSet.push(...types);
2020-07-23 11:00:46 +08:00
return this;
}
/**
*
* @param types
*/
public exclude(...types: (new (...args: any[]) => Component)[]): Matcher {
this.exclusionSet.push(...types);
2020-07-23 11:00:46 +08:00
return this;
}
/**
*
* @param types
*/
public one(...types: (new (...args: any[]) => Component)[]): Matcher {
this.oneSet.push(...types);
2020-07-23 11:00:46 +08:00
return this;
}
}
2020-07-23 11:00:46 +08:00
}