2020-07-23 11:00:46 +08:00
|
|
|
module es {
|
2023-03-13 17:46:16 +08:00
|
|
|
/**
|
|
|
|
|
* 定义一个实体匹配器类。
|
|
|
|
|
*/
|
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-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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 16:23:15 +08:00
|
|
|
public isInterested(components: Bits) {
|
2023-03-13 17:46:16 +08:00
|
|
|
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;
|
2023-03-13 17:46:16 +08:00
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-13 17:46:16 +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;
|
2023-03-13 17:46:16 +08:00
|
|
|
}
|
2021-05-07 16:23:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
2023-03-13 17:46:16 +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;
|
2023-03-13 17:46:16 +08:00
|
|
|
}
|
2021-05-07 16:23:15 +08:00
|
|
|
}
|
2023-03-13 17:46:16 +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;
|
|
|
|
|
}
|
2020-07-01 14:54:35 +08:00
|
|
|
|
2023-03-13 17:46:16 +08:00
|
|
|
/**
|
|
|
|
|
* 添加所有包含的组件类型。
|
|
|
|
|
* @param types 所有包含的组件类型列表
|
|
|
|
|
*/
|
|
|
|
|
public all(...types: (new (...args: any[]) => Component)[]): Matcher {
|
|
|
|
|
this.allSet.push(...types);
|
2020-07-23 11:00:46 +08:00
|
|
|
return this;
|
|
|
|
|
}
|
2020-07-01 14:54:35 +08:00
|
|
|
|
2023-03-13 17:46:16 +08:00
|
|
|
/**
|
|
|
|
|
* 添加排除包含的组件类型。
|
|
|
|
|
* @param types 排除包含的组件类型列表
|
|
|
|
|
*/
|
|
|
|
|
public exclude(...types: (new (...args: any[]) => Component)[]): Matcher {
|
|
|
|
|
this.exclusionSet.push(...types);
|
2020-07-23 11:00:46 +08:00
|
|
|
return this;
|
|
|
|
|
}
|
2020-07-01 14:54:35 +08:00
|
|
|
|
2023-03-13 17:46:16 +08:00
|
|
|
/**
|
|
|
|
|
* 添加至少包含其中之一的组件类型。
|
|
|
|
|
* @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-01 14:54:35 +08:00
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
}
|