Files
esengine/packages/core/src/ECS/Scene.ts

694 lines
20 KiB
TypeScript
Raw Normal View History

import { Entity } from './Entity';
import { EntityList } from './Utils/EntityList';
import { EntityProcessorList } from './Utils/EntityProcessorList';
import { IdentifierPool } from './Utils/IdentifierPool';
import { EntitySystem } from './Systems/EntitySystem';
2025-10-09 12:30:04 +08:00
import { ComponentStorageManager, ComponentRegistry } from './Core/ComponentStorage';
import { QuerySystem } from './Core/QuerySystem';
import { TypeSafeEventSystem } from './Core/EventSystem';
import { EventBus } from './Core/EventBus';
2025-08-12 09:39:07 +08:00
import { IScene, ISceneConfig } from './IScene';
import { getComponentInstanceTypeName, getSystemInstanceTypeName } from './Decorators';
import { TypedQueryBuilder } from './Core/Query/TypedQuery';
2025-10-08 18:34:15 +08:00
import { SceneSerializer, SceneSerializationOptions, SceneDeserializationOptions } from './Serialization/SceneSerializer';
2025-10-09 12:30:04 +08:00
import { IncrementalSerializer, IncrementalSnapshot, IncrementalSerializationOptions } from './Serialization/IncrementalSerializer';
/**
2025-08-12 09:39:07 +08:00
*
*
2025-08-12 09:39:07 +08:00
* IScene接口
* 使
*/
2025-08-12 09:39:07 +08:00
export class Scene implements IScene {
/**
*
2025-10-08 18:34:15 +08:00
*
*
*/
public name: string = "";
2025-10-08 18:34:15 +08:00
/**
*
*
*
*/
public readonly sceneData: Map<string, any> = new Map();
/**
*
2025-10-08 18:34:15 +08:00
*
*
*/
public readonly entities: EntityList;
/**
*
*
*
*/
public readonly entityProcessors: EntityProcessorList;
/**
* ID池
*
*
*/
public readonly identifierPool: IdentifierPool;
/**
*
*
*
*/
public readonly componentStorageManager: ComponentStorageManager;
/**
*
*
*
*/
public readonly querySystem: QuerySystem;
/**
*
*
*
*/
public readonly eventSystem: TypeSafeEventSystem;
/**
*
*/
private _didSceneBegin: boolean = false;
/**
*
*/
public get systems(): EntitySystem[] {
return this.entityProcessors.processors;
}
2025-08-12 09:39:07 +08:00
/**
*
*/
2025-08-12 09:39:07 +08:00
constructor(config?: ISceneConfig) {
this.entities = new EntityList(this);
this.entityProcessors = new EntityProcessorList();
this.identifierPool = new IdentifierPool();
this.componentStorageManager = new ComponentStorageManager();
this.querySystem = new QuerySystem();
this.eventSystem = new TypeSafeEventSystem();
2025-08-12 09:39:07 +08:00
// 应用配置
if (config?.name) {
this.name = config.name;
}
if (!Entity.eventBus) {
Entity.eventBus = new EventBus(false);
}
if (Entity.eventBus) {
2025-08-12 09:39:07 +08:00
Entity.eventBus.onComponentAdded((data: unknown) => {
this.eventSystem.emitSync('component:added', data);
});
}
}
/**
*
*
*
*/
public initialize(): void {
}
/**
*
*
*
*/
public onStart(): void {
}
/**
*
*
*
*/
public unload(): void {
}
/**
*
*
* onStart方法
*/
public begin() {
// 启动实体处理器
if (this.entityProcessors != null)
this.entityProcessors.begin();
// 标记场景已开始运行并调用onStart方法
this._didSceneBegin = true;
this.onStart();
}
/**
*
*
* unload方法
*/
public end() {
// 标记场景已结束运行
this._didSceneBegin = false;
// 移除所有实体
this.entities.removeAllEntities();
// 清理查询系统中的实体引用和缓存
this.querySystem.setEntities([]);
// 清空组件存储
this.componentStorageManager.clear();
// 结束实体处理器
if (this.entityProcessors)
this.entityProcessors.end();
// 调用卸载方法
this.unload();
}
/**
2025-09-26 09:38:51 +08:00
*
*/
public update() {
// 更新实体列表(处理延迟操作)
this.entities.updateLists();
// 更新实体处理器
if (this.entityProcessors != null)
this.entityProcessors.update();
2025-09-26 09:38:51 +08:00
// 更新实体处理器后处理
if (this.entityProcessors != null)
this.entityProcessors.lateUpdate();
}
/**
*
* @param name
*/
public createEntity(name: string) {
let entity = new Entity(name, this.identifierPool.checkOut());
this.eventSystem.emitSync('entity:created', { entityName: name, entity, scene: this });
return this.addEntity(entity);
}
/**
* EntitySystem的实体缓存
*
*/
public clearSystemEntityCaches(): void {
for (const system of this.entityProcessors.processors) {
system.clearEntityCache();
}
}
/**
*
* @param entity
* @param deferCacheClear
*/
public addEntity(entity: Entity, deferCacheClear: boolean = false) {
this.entities.add(entity);
entity.scene = this;
// 将实体添加到查询系统(可延迟缓存清理)
this.querySystem.addEntity(entity, deferCacheClear);
// 清除系统缓存以确保系统能及时发现新实体
if (!deferCacheClear) {
this.clearSystemEntityCaches();
}
// 触发实体添加事件
this.eventSystem.emitSync('entity:added', { entity, scene: this });
return entity;
}
/**
*
* @param count
* @param namePrefix
* @returns
*/
public createEntities(count: number, namePrefix: string = "Entity"): Entity[] {
const entities: Entity[] = [];
// 批量创建实体对象,不立即添加到系统
for (let i = 0; i < count; i++) {
const entity = new Entity(`${namePrefix}_${i}`, this.identifierPool.checkOut());
entity.scene = this;
entities.push(entity);
}
// 批量添加到实体列表
for (const entity of entities) {
this.entities.add(entity);
}
// 批量添加到查询系统(无重复检查,性能最优)
this.querySystem.addEntitiesUnchecked(entities);
// 批量触发事件(可选,减少事件开销)
this.eventSystem.emitSync('entities:batch_added', { entities, scene: this, count });
return entities;
}
/**
*
*/
public destroyAllEntities() {
this.entities.removeAllEntities();
// 清理查询系统中的实体引用和缓存
this.querySystem.setEntities([]);
}
/**
*
* @param name
*/
public findEntity(name: string): Entity | null {
return this.entities.findEntity(name);
}
/**
* ID查找实体
* @param id ID
*/
public findEntityById(id: number): Entity | null {
return this.entities.findEntityById(id);
}
/**
*
* @param tag
*/
public findEntitiesByTag(tag: number): Entity[] {
const result: Entity[] = [];
for (const entity of this.entities.buffer) {
if (entity.tag === tag) {
result.push(entity);
}
}
return result;
}
/**
*
* @param name
*/
public getEntityByName(name: string): Entity | null {
return this.findEntity(name);
}
/**
*
* @param tag
*/
public getEntitiesByTag(tag: number): Entity[] {
return this.findEntitiesByTag(tag);
}
/**
*
*
* @param componentTypes -
* @returns
*
* @example
* ```typescript
* const result = scene.queryAll(Position, Velocity);
* for (const entity of result.entities) {
* const pos = entity.getComponent(Position);
* const vel = entity.getComponent(Velocity);
* }
* ```
*/
public queryAll(...componentTypes: any[]): { entities: readonly Entity[] } {
return this.querySystem.queryAll(...componentTypes);
}
/**
*
*
* @param componentTypes -
* @returns
*/
public queryAny(...componentTypes: any[]): { entities: readonly Entity[] } {
return this.querySystem.queryAny(...componentTypes);
}
/**
*
*
* @param componentTypes -
* @returns
*/
public queryNone(...componentTypes: any[]): { entities: readonly Entity[] } {
return this.querySystem.queryNone(...componentTypes);
}
/**
*
*
* @returns
*
* @example
* ```typescript
* // 使用查询构建器
* const matcher = scene.query()
* .withAll(Position, Velocity)
* .withNone(Disabled)
* .buildMatcher();
*
* // 在System中使用
* class MovementSystem extends EntitySystem {
* constructor() {
* super(matcher);
* }
* }
* ```
*/
public query(): TypedQueryBuilder {
return new TypedQueryBuilder();
}
/**
* EntitySystem处理器
* @param processor
*/
public addEntityProcessor(processor: EntitySystem) {
if (this.entityProcessors.processors.includes(processor)) {
return processor;
}
processor.scene = this;
this.entityProcessors.add(processor);
processor.initialize();
processor.setUpdateOrder(this.entityProcessors.count - 1);
return processor;
}
/**
* addEntityProcessor的别名
* @param system
*/
public addSystem(system: EntitySystem) {
return this.addEntityProcessor(system);
}
/**
* EntitySystem处理器
* @param processor
*/
public removeEntityProcessor(processor: EntitySystem) {
this.entityProcessors.remove(processor);
processor.reset();
2025-09-24 18:14:22 +08:00
}
/**
* removeEntityProcessor的别名
* @param system
*/
public removeSystem(system: EntitySystem) {
this.removeEntityProcessor(system);
}
/**
* EntitySystem处理器
* @param type
*/
public getEntityProcessor<T extends EntitySystem>(type: new (...args: unknown[]) => T): T | null {
return this.entityProcessors.getProcessor(type);
}
/**
*
*/
public getStats(): {
entityCount: number;
processorCount: number;
componentStorageStats: Map<string, any>;
} {
return {
entityCount: this.entities.count,
processorCount: this.entityProcessors.count,
componentStorageStats: this.componentStorageManager.getAllStats()
};
}
/**
*
*/
public getDebugInfo(): {
name: string;
entityCount: number;
processorCount: number;
isRunning: boolean;
entities: Array<{
name: string;
id: number;
componentCount: number;
componentTypes: string[];
}>;
processors: Array<{
name: string;
updateOrder: number;
entityCount: number;
}>;
componentStats: Map<string, any>;
} {
return {
name: this.name || this.constructor.name,
entityCount: this.entities.count,
processorCount: this.entityProcessors.count,
isRunning: this._didSceneBegin,
entities: this.entities.buffer.map(entity => ({
name: entity.name,
id: entity.id,
componentCount: entity.components.length,
componentTypes: entity.components.map(c => getComponentInstanceTypeName(c))
})),
processors: this.entityProcessors.processors.map(processor => ({
name: getSystemInstanceTypeName(processor),
updateOrder: processor.updateOrder,
entityCount: (processor as any)._entities?.length || 0
})),
componentStats: this.componentStorageManager.getAllStats()
};
}
2025-10-08 18:34:15 +08:00
/**
*
*
2025-10-08 20:42:55 +08:00
* JSON字符串或二进制Buffer
2025-10-08 18:34:15 +08:00
*
* @param options
2025-10-08 20:42:55 +08:00
* @returns JSON字符串或二进制Buffer
2025-10-08 18:34:15 +08:00
*
* @example
* ```typescript
2025-10-08 20:42:55 +08:00
* // JSON格式
* const jsonData = scene.serialize({
2025-10-08 18:34:15 +08:00
* format: 'json',
* pretty: true
* });
2025-10-08 20:42:55 +08:00
*
* // 二进制格式(更小、更快)
* const binaryData = scene.serialize({
* format: 'binary'
* });
2025-10-08 18:34:15 +08:00
* ```
*/
2025-10-08 20:42:55 +08:00
public serialize(options?: SceneSerializationOptions): string | Buffer {
2025-10-08 18:34:15 +08:00
return SceneSerializer.serialize(this, options);
}
/**
*
*
*
*
2025-10-08 20:42:55 +08:00
* @param saveData JSON字符串或二进制Buffer
2025-10-08 18:34:15 +08:00
* @param options
*
* @example
* ```typescript
2025-10-08 20:42:55 +08:00
* // 从JSON恢复自动从ComponentRegistry获取组件类型
* scene.deserialize(jsonData, {
* strategy: 'replace'
* });
*
* // 从二进制恢复
* scene.deserialize(binaryData, {
* strategy: 'replace'
2025-10-08 18:34:15 +08:00
* });
* ```
*/
2025-10-08 20:42:55 +08:00
public deserialize(saveData: string | Buffer, options?: SceneDeserializationOptions): void {
2025-10-08 18:34:15 +08:00
SceneSerializer.deserialize(this, saveData, options);
}
2025-10-09 12:30:04 +08:00
// ==================== 增量序列化 API ====================
/** 增量序列化的基础快照 */
private _incrementalBaseSnapshot?: any;
/**
*
*
*
*
* @param options
*
* @example
* ```typescript
* // 创建基础快照
* scene.createIncrementalSnapshot();
*
* // 进行一些修改...
* entity.addComponent(new PositionComponent(100, 200));
*
* // 计算增量变更
* const incremental = scene.serializeIncremental();
* ```
*/
public createIncrementalSnapshot(options?: IncrementalSerializationOptions): void {
this._incrementalBaseSnapshot = IncrementalSerializer.createSnapshot(this, options);
}
/**
*
*
*
*
* @param options
* @returns
*
* @example
* ```typescript
* // 创建基础快照
* scene.createIncrementalSnapshot();
*
* // 修改场景
* const entity = scene.createEntity('NewEntity');
* entity.addComponent(new PositionComponent(50, 100));
*
* // 获取增量变更
* const incremental = scene.serializeIncremental();
* console.log(`变更数量: ${incremental.entityChanges.length}`);
*
* // 序列化为JSON
* const json = IncrementalSerializer.serializeIncremental(incremental);
* ```
*/
public serializeIncremental(options?: IncrementalSerializationOptions): IncrementalSnapshot {
if (!this._incrementalBaseSnapshot) {
throw new Error('必须先调用 createIncrementalSnapshot() 创建基础快照');
}
return IncrementalSerializer.computeIncremental(
this,
this._incrementalBaseSnapshot,
options
);
}
/**
*
*
2025-10-09 17:14:18 +08:00
* @param incremental IncrementalSnapshot对象JSON字符串或二进制Buffer
2025-10-09 12:30:04 +08:00
* @param componentRegistry 使
*
* @example
* ```typescript
2025-10-09 17:14:18 +08:00
* // 应用增量变更对象
2025-10-09 12:30:04 +08:00
* scene.applyIncremental(incrementalSnapshot);
*
2025-10-09 17:14:18 +08:00
* // 从JSON字符串应用
* const jsonData = IncrementalSerializer.serializeIncremental(snapshot, { format: 'json' });
* scene.applyIncremental(jsonData);
*
* // 从二进制Buffer应用
* const binaryData = IncrementalSerializer.serializeIncremental(snapshot, { format: 'binary' });
* scene.applyIncremental(binaryData);
2025-10-09 12:30:04 +08:00
* ```
*/
public applyIncremental(
2025-10-09 17:14:18 +08:00
incremental: IncrementalSnapshot | string | Buffer,
2025-10-09 12:30:04 +08:00
componentRegistry?: Map<string, any>
): void {
2025-10-09 17:14:18 +08:00
const snapshot = (typeof incremental === 'string' || Buffer.isBuffer(incremental))
2025-10-09 12:30:04 +08:00
? IncrementalSerializer.deserializeIncremental(incremental)
: incremental;
const registry = componentRegistry || ComponentRegistry.getAllComponentNames() as Map<string, any>;
IncrementalSerializer.applyIncremental(this, snapshot, registry);
}
/**
*
*
*
*
* @param options
*
* @example
* ```typescript
* // 创建初始快照
* scene.createIncrementalSnapshot();
*
* // 进行一些修改并序列化
* const incremental1 = scene.serializeIncremental();
*
* // 更新基准,之后的增量将基于当前状态
* scene.updateIncrementalSnapshot();
*
* // 继续修改
* const incremental2 = scene.serializeIncremental();
* ```
*/
public updateIncrementalSnapshot(options?: IncrementalSerializationOptions): void {
this.createIncrementalSnapshot(options);
}
/**
*
*
*
*/
public clearIncrementalSnapshot(): void {
this._incrementalBaseSnapshot = undefined;
}
/**
*
*
* @returns true
*/
public hasIncrementalSnapshot(): boolean {
return this._incrementalBaseSnapshot !== undefined;
}
2020-06-08 11:49:45 +08:00
}