优化matcher内部实现改为querysystem
完善type类型 更新文档
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { Entity } from '../Entity';
|
||||
import { Core } from '../../Core';
|
||||
import { Matcher } from '../Utils/Matcher';
|
||||
import { PerformanceMonitor } from '../../Utils/PerformanceMonitor';
|
||||
import { Matcher } from '../Utils/Matcher';
|
||||
import type { Scene } from '../Scene';
|
||||
import type { ISystemBase } from '../../Types';
|
||||
import type { QuerySystem } from '../Core/QuerySystem';
|
||||
|
||||
/**
|
||||
* 实体系统的基类
|
||||
@@ -15,7 +15,7 @@ import type { ISystemBase } from '../../Types';
|
||||
* ```typescript
|
||||
* class MovementSystem extends EntitySystem {
|
||||
* constructor() {
|
||||
* super(Matcher.empty().all(Transform, Velocity));
|
||||
* super(Transform, Velocity);
|
||||
* }
|
||||
*
|
||||
* protected process(entities: Entity[]): void {
|
||||
@@ -29,18 +29,18 @@ import type { ISystemBase } from '../../Types';
|
||||
* ```
|
||||
*/
|
||||
export abstract class EntitySystem implements ISystemBase {
|
||||
private _entities: Entity[] = [];
|
||||
private _updateOrder: number = 0;
|
||||
private _enabled: boolean = true;
|
||||
private _performanceMonitor = PerformanceMonitor.instance;
|
||||
private _systemName: string;
|
||||
private _initialized: boolean = false;
|
||||
private _matcher: Matcher;
|
||||
|
||||
/**
|
||||
* 获取系统处理的实体列表
|
||||
* 获取系统处理的实体列表(动态查询)
|
||||
*/
|
||||
public get entities(): readonly Entity[] {
|
||||
return this._entities;
|
||||
return this.queryEntities();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,18 +91,15 @@ export abstract class EntitySystem implements ISystemBase {
|
||||
|
||||
public set scene(value: Scene | null) {
|
||||
this._scene = value;
|
||||
this._entities = [];
|
||||
}
|
||||
|
||||
private _matcher: Matcher;
|
||||
|
||||
/**
|
||||
* 获取实体匹配器
|
||||
*/
|
||||
public get matcher(): Matcher {
|
||||
return this._matcher;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置更新时序
|
||||
* @param order 更新时序
|
||||
@@ -115,11 +112,9 @@ export abstract class EntitySystem implements ISystemBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统初始化
|
||||
* 系统初始化(框架调用)
|
||||
*
|
||||
* 在系统创建时调用,自动检查场景中已存在的实体是否匹配此系统。
|
||||
* 防止重复初始化以避免实体被重复处理。
|
||||
* 子类可以重写此方法进行额外的初始化操作。
|
||||
* 在系统创建时调用。框架内部使用,用户不应直接调用。
|
||||
*/
|
||||
public initialize(): void {
|
||||
// 防止重复初始化
|
||||
@@ -129,13 +124,17 @@ export abstract class EntitySystem implements ISystemBase {
|
||||
|
||||
this._initialized = true;
|
||||
|
||||
if (this.scene?.entities?.buffer) {
|
||||
for (const entity of this.scene.entities.buffer) {
|
||||
this.onChanged(entity);
|
||||
}
|
||||
}
|
||||
|
||||
// 子类可以重写此方法进行额外初始化
|
||||
// 调用用户可重写的初始化方法
|
||||
this.onInitialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统初始化回调
|
||||
*
|
||||
* 子类可以重写此方法进行初始化操作。
|
||||
*/
|
||||
protected onInitialize(): void {
|
||||
// 子类可以重写此方法进行初始化
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,91 +144,181 @@ export abstract class EntitySystem implements ISystemBase {
|
||||
*/
|
||||
public reset(): void {
|
||||
this._initialized = false;
|
||||
this._entities.length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当实体的组件发生变化时调用
|
||||
*
|
||||
* 检查实体是否仍然符合系统的匹配条件,并相应地添加或移除实体。
|
||||
*
|
||||
* @param entity 发生变化的实体
|
||||
* 查询匹配的实体
|
||||
*/
|
||||
public onChanged(entity: Entity): void {
|
||||
const contains = this._entities.includes(entity);
|
||||
const interest = this._matcher.isInterestedEntity(entity);
|
||||
|
||||
if (interest && !contains) {
|
||||
this.add(entity);
|
||||
} else if (!interest && contains) {
|
||||
this.remove(entity);
|
||||
private queryEntities(): Entity[] {
|
||||
if (!this.scene?.querySystem || !this._matcher) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加实体到系统
|
||||
*
|
||||
* @param entity 要添加的实体
|
||||
*/
|
||||
public add(entity: Entity): void {
|
||||
if (!this._entities.includes(entity)) {
|
||||
this._entities.push(entity);
|
||||
this.onAdded(entity);
|
||||
const condition = this._matcher.getCondition();
|
||||
const querySystem = this.scene.querySystem;
|
||||
|
||||
// 空条件返回所有实体
|
||||
if (this._matcher.isEmpty()) {
|
||||
return querySystem.getAllEntities();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当实体被添加到系统时调用
|
||||
*
|
||||
* 子类可以重写此方法来处理实体添加事件。
|
||||
*
|
||||
* @param entity 被添加的实体
|
||||
*/
|
||||
protected onAdded(entity: Entity): void {
|
||||
// 子类可以重写此方法
|
||||
}
|
||||
|
||||
/**
|
||||
* 从系统中移除实体
|
||||
*
|
||||
* @param entity 要移除的实体
|
||||
*/
|
||||
public remove(entity: Entity): void {
|
||||
const index = this._entities.indexOf(entity);
|
||||
if (index !== -1) {
|
||||
this._entities.splice(index, 1);
|
||||
this.onRemoved(entity);
|
||||
// 单一条件优化查询
|
||||
if (this.isSingleCondition(condition)) {
|
||||
return this.executeSingleConditionQuery(condition, querySystem);
|
||||
}
|
||||
|
||||
// 复合查询
|
||||
return this.executeComplexQuery(condition, querySystem);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当实体从系统中移除时调用
|
||||
*
|
||||
* 子类可以重写此方法来处理实体移除事件。
|
||||
*
|
||||
* @param entity 被移除的实体
|
||||
* 检查是否为单一条件查询
|
||||
*/
|
||||
protected onRemoved(entity: Entity): void {
|
||||
// 子类可以重写此方法
|
||||
private isSingleCondition(condition: any): boolean {
|
||||
const conditionCount =
|
||||
(condition.all.length > 0 ? 1 : 0) +
|
||||
(condition.any.length > 0 ? 1 : 0) +
|
||||
(condition.none.length > 0 ? 1 : 0) +
|
||||
(condition.tag !== undefined ? 1 : 0) +
|
||||
(condition.name !== undefined ? 1 : 0) +
|
||||
(condition.component !== undefined ? 1 : 0);
|
||||
|
||||
return conditionCount === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行单一条件查询
|
||||
*/
|
||||
private executeSingleConditionQuery(condition: any, querySystem: any): Entity[] {
|
||||
// 按标签查询
|
||||
if (condition.tag !== undefined) {
|
||||
return querySystem.queryByTag(condition.tag).entities;
|
||||
}
|
||||
|
||||
// 按名称查询
|
||||
if (condition.name !== undefined) {
|
||||
return querySystem.queryByName(condition.name).entities;
|
||||
}
|
||||
|
||||
// 单组件查询
|
||||
if (condition.component !== undefined) {
|
||||
return querySystem.queryByComponent(condition.component).entities;
|
||||
}
|
||||
|
||||
// 基础组件查询
|
||||
if (condition.all.length > 0 && condition.any.length === 0 && condition.none.length === 0) {
|
||||
return querySystem.queryAll(...condition.all).entities;
|
||||
}
|
||||
|
||||
if (condition.all.length === 0 && condition.any.length > 0 && condition.none.length === 0) {
|
||||
return querySystem.queryAny(...condition.any).entities;
|
||||
}
|
||||
|
||||
if (condition.all.length === 0 && condition.any.length === 0 && condition.none.length > 0) {
|
||||
return querySystem.queryNone(...condition.none).entities;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行复合查询
|
||||
*/
|
||||
private executeComplexQuery(condition: any, querySystem: QuerySystem): Entity[] {
|
||||
let result: Set<Entity> | null = null;
|
||||
|
||||
// 1. 应用标签条件作为基础集合
|
||||
if (condition.tag !== undefined) {
|
||||
const tagResult = querySystem.queryByTag(condition.tag);
|
||||
result = new Set(tagResult.entities);
|
||||
}
|
||||
|
||||
// 2. 应用名称条件
|
||||
if (condition.name !== undefined) {
|
||||
const nameResult = querySystem.queryByName(condition.name);
|
||||
const nameSet = new Set(nameResult.entities);
|
||||
|
||||
if (result) {
|
||||
result = new Set([...result].filter(e => nameSet.has(e)));
|
||||
} else {
|
||||
result = nameSet;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 应用单组件条件
|
||||
if (condition.component !== undefined) {
|
||||
const componentResult = querySystem.queryByComponent(condition.component);
|
||||
const componentSet = new Set(componentResult.entities);
|
||||
|
||||
if (result) {
|
||||
result = new Set([...result].filter(e => componentSet.has(e)));
|
||||
} else {
|
||||
result = componentSet;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 应用all条件
|
||||
if (condition.all.length > 0) {
|
||||
const allResult = querySystem.queryAll(...condition.all);
|
||||
const allSet = new Set(allResult.entities);
|
||||
|
||||
if (result) {
|
||||
result = new Set([...result].filter(e => allSet.has(e)));
|
||||
} else {
|
||||
result = allSet;
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 应用any条件(求交集)
|
||||
if (condition.any.length > 0) {
|
||||
const anyResult = querySystem.queryAny(...condition.any);
|
||||
const anySet = new Set(anyResult.entities);
|
||||
|
||||
if (result) {
|
||||
result = new Set([...result].filter(e => anySet.has(e)));
|
||||
} else {
|
||||
result = anySet;
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 应用none条件(排除)
|
||||
if (condition.none.length > 0) {
|
||||
if (!result) {
|
||||
// 如果没有前置条件,从所有实体开始
|
||||
result = new Set(querySystem.getAllEntities());
|
||||
}
|
||||
|
||||
const noneResult = querySystem.queryAny(...condition.none);
|
||||
const noneSet = new Set(noneResult.entities);
|
||||
result = new Set([...result].filter(e => !noneSet.has(e)));
|
||||
}
|
||||
|
||||
return result ? Array.from(result) : [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 更新系统
|
||||
*
|
||||
* 在每帧调用,处理系统的主要逻辑。
|
||||
*/
|
||||
public update(): void {
|
||||
if (!this._enabled || !this.checkProcessing()) {
|
||||
if (!this._enabled || !this.onCheckProcessing()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const startTime = this._performanceMonitor.startMonitoring(this._systemName);
|
||||
let entityCount = 0;
|
||||
|
||||
try {
|
||||
this.begin();
|
||||
this.process(this._entities);
|
||||
this.onBegin();
|
||||
// 动态查询实体并处理
|
||||
const entities = this.queryEntities();
|
||||
entityCount = entities.length;
|
||||
this.process(entities);
|
||||
} finally {
|
||||
this._performanceMonitor.endMonitoring(this._systemName, startTime, this._entities.length);
|
||||
this._performanceMonitor.endMonitoring(this._systemName, startTime, entityCount);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,17 +328,21 @@ export abstract class EntitySystem implements ISystemBase {
|
||||
* 在所有系统的update方法执行完毕后调用。
|
||||
*/
|
||||
public lateUpdate(): void {
|
||||
if (!this._enabled || !this.checkProcessing()) {
|
||||
if (!this._enabled || !this.onCheckProcessing()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const startTime = this._performanceMonitor.startMonitoring(`${this._systemName}_Late`);
|
||||
let entityCount = 0;
|
||||
|
||||
try {
|
||||
this.lateProcess(this._entities);
|
||||
this.end();
|
||||
// 动态查询实体并处理
|
||||
const entities = this.queryEntities();
|
||||
entityCount = entities.length;
|
||||
this.lateProcess(entities);
|
||||
this.onEnd();
|
||||
} finally {
|
||||
this._performanceMonitor.endMonitoring(`${this._systemName}_Late`, startTime, this._entities.length);
|
||||
this._performanceMonitor.endMonitoring(`${this._systemName}_Late`, startTime, entityCount);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,7 +351,7 @@ export abstract class EntitySystem implements ISystemBase {
|
||||
*
|
||||
* 子类可以重写此方法进行预处理操作。
|
||||
*/
|
||||
protected begin(): void {
|
||||
protected onBegin(): void {
|
||||
// 子类可以重写此方法
|
||||
}
|
||||
|
||||
@@ -289,7 +382,7 @@ export abstract class EntitySystem implements ISystemBase {
|
||||
*
|
||||
* 子类可以重写此方法进行后处理操作。
|
||||
*/
|
||||
protected end(): void {
|
||||
protected onEnd(): void {
|
||||
// 子类可以重写此方法
|
||||
}
|
||||
|
||||
@@ -301,7 +394,7 @@ export abstract class EntitySystem implements ISystemBase {
|
||||
*
|
||||
* @returns 如果系统应该处理,则为true,如果不处理则为false
|
||||
*/
|
||||
protected checkProcessing(): boolean {
|
||||
protected onCheckProcessing(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -330,17 +423,17 @@ export abstract class EntitySystem implements ISystemBase {
|
||||
this._performanceMonitor.resetSystem(this._systemName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取系统信息的字符串表示
|
||||
*
|
||||
* @returns 系统信息字符串
|
||||
*/
|
||||
public toString(): string {
|
||||
const entityCount = this._entities.length;
|
||||
const entityCount = this.entities.length;
|
||||
const perfData = this.getPerformanceData();
|
||||
const perfInfo = perfData ? ` (${perfData.executionTime.toFixed(2)}ms)` : '';
|
||||
|
||||
return `${this._systemName}[${entityCount} entities]${perfInfo}`;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user