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

72 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
module es {
export class Matcher {
protected allSet = new BitSet();
protected exclusionSet = new BitSet();
protected oneSet = new BitSet();
public static empty() {
return new Matcher();
}
public getAllSet() {
return this.allSet;
}
public getExclusionSet() {
return this.exclusionSet;
}
public getOneSet() {
return this.oneSet;
}
public isInterestedEntity(e: Entity) {
return this.isInterested(e.componentBits);
}
public isInterested(componentBits: BitSet) {
// 检查实体是否拥有该方面中定义的所有组件
if (!this.allSet.isEmpty()) {
for (let i = this.allSet.nextSetBit(0); i >= 0; i = this.allSet.nextSetBit(i + 1)) {
if (!componentBits.get(i))
return false;
}
}
// 如果我们仍然感兴趣,检查该实体是否拥有任何一个排除组件,如果有,那么系统就不感兴趣
if (!this.exclusionSet.isEmpty() && this.exclusionSet.intersects(componentBits))
return false;
// 如果我们仍然感兴趣检查该实体是否拥有oneSet中的任何一个组件。如果是系统就会感兴趣
if (!this.oneSet.isEmpty() && !this.oneSet.intersects(componentBits))
return false;
return true;
}
public all(...types: any[]): Matcher {
types.forEach(type => {
this.allSet.set(ComponentTypeManager.getIndexFor(type));
});
return this;
}
public exclude(...types: any[]) {
types.forEach(type => {
this.exclusionSet.set(ComponentTypeManager.getIndexFor(type));
});
return this;
}
public one(...types: any[]) {
types.forEach(type => {
this.oneSet.set(ComponentTypeManager.getIndexFor(type));
});
return this;
}
}
}