feat(ecs): 核心系统改进 - 句柄、调度、变更检测与查询编译 (#304)

新增功能:
- EntityHandle: 轻量级实体句柄 (28位索引 + 20位代数)
- SystemScheduler: 声明式系统调度,支持 @Stage/@Before/@After/@InSet 装饰器
- EpochManager: 帧级变更检测
- CompiledQuery: 预编译类型安全查询

API 改进:
- EntitySystem 添加 getBefore()/getAfter()/getSets() getter 方法
- Entity 添加 markDirty() 辅助方法
- IScene 添加 epochManager 属性
- CommandBuffer.pendingCount 修正为返回实际操作数

文档更新:
- 更新系统调度和查询相关文档
This commit is contained in:
YHH
2025-12-15 09:17:00 +08:00
committed by GitHub
parent b5158b6ac6
commit cd6ef222d1
27 changed files with 5233 additions and 43 deletions

View File

@@ -6,6 +6,7 @@ import { createLogger } from '../Utils/Logger';
import { getComponentInstanceTypeName, getComponentTypeName } from './Decorators';
import { generateGUID } from '../Utils/GUID';
import type { IScene } from './IScene';
import { EntityHandle, NULL_HANDLE } from './Core/EntityHandle';
/**
* 组件活跃状态变化接口
@@ -93,6 +94,19 @@ export class Entity {
*/
public readonly persistentId: string;
/**
* 轻量级实体句柄
*
* 数值类型的实体标识符,包含索引和代数信息。
* 用于高性能场景下替代对象引用,支持 Archetype 存储等优化。
*
* Lightweight entity handle.
* Numeric identifier containing index and generation.
* Used for high-performance scenarios instead of object references,
* supports Archetype storage optimizations.
*/
private _handle: EntityHandle = NULL_HANDLE;
/**
* 所属场景引用
*/
@@ -171,6 +185,34 @@ export class Entity {
return this._lifecyclePolicy === EEntityLifecyclePolicy.Persistent;
}
/**
* 获取实体句柄
*
* 返回轻量级数值句柄,用于高性能场景。
* 如果实体尚未分配句柄,返回 NULL_HANDLE。
*
* Get entity handle.
* Returns lightweight numeric handle for high-performance scenarios.
* Returns NULL_HANDLE if entity has no handle assigned.
*/
public get handle(): EntityHandle {
return this._handle;
}
/**
* 设置实体句柄(内部使用)
*
* 此方法供 Scene 在创建实体时调用。
*
* Set entity handle (internal use).
* Called by Scene when creating entities.
*
* @internal
*/
public setHandle(handle: EntityHandle): void {
this._handle = handle;
}
/**
* 设置实体为持久化(跨场景保留)
*
@@ -559,6 +601,39 @@ export class Entity {
return component;
}
/**
* 标记组件为已修改
*
* 便捷方法,自动从场景获取当前 epoch 并标记组件。
* 用于帧级变更检测系统。
*
* Mark component(s) as modified.
* Convenience method that auto-gets epoch from scene and marks components.
* Used for frame-level change detection system.
*
* @param components 要标记的组件 | Components to mark
*
* @example
* ```typescript
* const pos = entity.getComponent(Position)!;
* pos.x = 100;
* entity.markDirty(pos);
*
* // 或者标记多个组件
* entity.markDirty(pos, vel);
* ```
*/
public markDirty(...components: Component[]): void {
if (!this.scene) {
return;
}
const epoch = this.scene.epochManager.current;
for (const component of components) {
component.markDirty(epoch);
}
}
/**
* 移除指定的组件
*