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-01 14:54:35 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public getExclusionSet() {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return this.exclusionSet;
|
|
|
|
|
|
}
|
2020-07-01 14:54:35 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public getOneSet() {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return this.oneSet;
|
|
|
|
|
|
}
|
2020-07-01 14:54:35 +08:00
|
|
|
|
|
2020-11-24 18:21:26 +08:00
|
|
|
|
public isInterestedEntity(e: Entity) {
|
|
|
|
|
|
return this.isInterested(e.componentBits);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public isInterested(componentBits: BitSet) {
|
|
|
|
|
|
// 检查实体是否拥有该方面中定义的所有组件
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (!this.allSet.isEmpty()) {
|
|
|
|
|
|
for (let i = this.allSet.nextSetBit(0); i >= 0; i = this.allSet.nextSetBit(i + 1)) {
|
2020-11-24 18:21:26 +08:00
|
|
|
|
if (!componentBits.get(i))
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-24 18:21:26 +08:00
|
|
|
|
// 如果我们仍然感兴趣,检查该实体是否拥有任何一个排除组件,如果有,那么系统就不感兴趣
|
|
|
|
|
|
if (!this.exclusionSet.isEmpty() && this.exclusionSet.intersects(componentBits))
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return false;
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-11-24 18:21:26 +08:00
|
|
|
|
// 如果我们仍然感兴趣,检查该实体是否拥有oneSet中的任何一个组件。如果是,系统就会感兴趣
|
|
|
|
|
|
if (!this.oneSet.isEmpty() && !this.oneSet.intersects(componentBits))
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return false;
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2020-07-01 14:54:35 +08:00
|
|
|
|
|
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-01 14:54:35 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
2020-07-01 14:54:35 +08:00
|
|
|
|
|
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-01 14:54:35 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
2020-07-01 14:54:35 +08:00
|
|
|
|
|
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-01 14:54:35 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
2020-07-01 14:54:35 +08:00
|
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|