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

65 lines
1.7 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 {
2020-07-23 11:00:46 +08:00
protected allSet = new BitSet();
protected exclusionSet = new BitSet();
protected oneSet = new BitSet();
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;
}
2020-07-28 16:25:20 +08:00
public IsIntersted(e: Entity) {
if (!this.allSet.isEmpty()) {
for (let i = this.allSet.nextSetBit(0); i >= 0; i = this.allSet.nextSetBit(i + 1)) {
2020-07-23 11:00:46 +08:00
if (!e.componentBits.get(i))
return false;
}
2020-06-08 20:11:58 +08:00
}
2020-07-23 11:00:46 +08:00
if (!this.exclusionSet.isEmpty() && this.exclusionSet.intersects(e.componentBits))
return false;
2020-06-08 20:11:58 +08:00
2020-07-23 11:00:46 +08:00
if (!this.oneSet.isEmpty() && !this.oneSet.intersects(e.componentBits))
return false;
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 {
2020-07-23 11:00:46 +08:00
types.forEach(type => {
this.allSet.set(ComponentTypeManager.getIndexFor(type));
});
2020-07-23 11:00:46 +08:00
return this;
}
2020-07-28 16:25:20 +08:00
public exclude(...types: any[]) {
2020-07-23 11:00:46 +08:00
types.forEach(type => {
this.exclusionSet.set(ComponentTypeManager.getIndexFor(type));
});
2020-07-23 11:00:46 +08:00
return this;
}
2020-07-28 16:25:20 +08:00
public one(...types: any[]) {
2020-07-23 11:00:46 +08:00
types.forEach(type => {
this.oneSet.set(ComponentTypeManager.getIndexFor(type));
});
2020-07-23 11:00:46 +08:00
return this;
}
}
2020-07-23 11:00:46 +08:00
}