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

1038 lines
31 KiB
TypeScript
Raw Normal View History

import { Entity } from './Entity';
import { EntityList } from './Utils/EntityList';
import { IdentifierPool } from './Utils/IdentifierPool';
import { EntitySystem } from './Systems/EntitySystem';
import { ComponentStorageManager, ComponentRegistry, ComponentType } from './Core/ComponentStorage';
import { QuerySystem } from './Core/QuerySystem';
import { TypeSafeEventSystem } from './Core/EventSystem';
import { ReferenceTracker } from './Core/ReferenceTracker';
2025-08-12 09:39:07 +08:00
import { IScene, ISceneConfig } from './IScene';
import { getComponentInstanceTypeName, getSystemInstanceTypeName, getSystemMetadata } from './Decorators';
import { TypedQueryBuilder } from './Core/Query/TypedQuery';
import {
SceneSerializer,
SceneSerializationOptions,
SceneDeserializationOptions
} from './Serialization/SceneSerializer';
import {
IncrementalSerializer,
IncrementalSnapshot,
IncrementalSerializationOptions
} from './Serialization/IncrementalSerializer';
2025-10-10 10:16:44 +08:00
import { ComponentPoolManager } from './Core/ComponentPool';
import { PerformanceMonitor } from '../Utils/PerformanceMonitor';
import { ServiceContainer, type ServiceType, type IService } from '../Core/ServiceContainer';
import { createInstance, isInjectable, injectProperties } from '../Core/DI';
2025-10-10 21:52:43 +08:00
import { createLogger } from '../Utils/Logger';
/**
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
/**
*
*
*
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
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;
/**
* ID池
*
*
*/
public readonly identifierPool: IdentifierPool;
/**
*
*
*
*/
public readonly componentStorageManager: ComponentStorageManager;
/**
*
*
*
*/
public readonly querySystem: QuerySystem;
/**
*
2025-10-10 21:52:43 +08:00
*
*
*/
public readonly eventSystem: TypeSafeEventSystem;
2025-10-10 21:52:43 +08:00
/**
*
*
* Component中对Entity的引用Entity销毁时自动清理引用
*/
public readonly referenceTracker: ReferenceTracker;
2025-10-10 21:52:43 +08:00
/**
*
*
* EntitySystem和其他服务的生命周期
* Scene拥有独立的服务容器
*/
private readonly _services: ServiceContainer;
/**
*
*/
private readonly logger: ReturnType<typeof createLogger>;
/**
*
*
* ServiceContainer
*/
private _performanceMonitor: PerformanceMonitor | null = null;
/**
*
*/
private _didSceneBegin: boolean = false;
/**
*
*/
private _cachedSystems: EntitySystem[] | null = null;
/**
*
*
* updateOrder true访 systems
*/
private _systemsOrderDirty: boolean = true;
/**
*
*
*
*/
private _systemErrorCount: Map<EntitySystem, number> = new Map();
/**
*
*
*
*/
private _maxErrorCount: number = 10;
/**
2025-10-10 21:52:43 +08:00
* EntitySystem
*
* updateOrder排序使
2025-10-10 21:52:43 +08:00
*
* @returns
*/
public get systems(): EntitySystem[] {
if (!this._systemsOrderDirty && this._cachedSystems) {
return this._cachedSystems;
}
this._cachedSystems = this._rebuildSystemsCache();
this._systemsOrderDirty = false;
2025-10-10 21:52:43 +08:00
return this._cachedSystems;
}
2025-10-10 21:52:43 +08:00
/**
*
*
* EntitySystem并排序
*/
private _rebuildSystemsCache(): EntitySystem[] {
const allServices = this._services.getAll();
const systems = this._filterEntitySystems(allServices);
return this._sortSystemsByUpdateOrder(systems);
}
2025-10-10 21:52:43 +08:00
/**
* EntitySystem实例
*/
private _filterEntitySystems(services: IService[]): EntitySystem[] {
return services.filter((service): service is EntitySystem => service instanceof EntitySystem);
}
/**
* updateOrder排序系统
*/
private _sortSystemsByUpdateOrder(systems: EntitySystem[]): EntitySystem[] {
return systems.sort((a, b) => a.updateOrder - b.updateOrder);
}
2025-10-10 21:52:43 +08:00
/**
* System实例
*
* @param systemType System类型
* @returns System实例null
*
* @example
* ```typescript
* const physics = scene.getSystem(PhysicsSystem);
* if (physics) {
* physics.doSomething();
* }
* ```
*/
public getSystem<T extends EntitySystem>(systemType: ServiceType<T>): T | null {
return this._services.tryResolve(systemType) as T | null;
}
/**
*
*
* 使
*/
public markSystemsOrderDirty(): void {
this._systemsOrderDirty = true;
}
2025-10-10 21:52:43 +08:00
/**
*
*
* EntitySystem
*
* @example
* ```typescript
* // 注册服务
* scene.services.registerSingleton(PhysicsSystem);
*
* // 解析服务
* const physics = scene.services.resolve(PhysicsSystem);
* ```
*/
public get services(): ServiceContainer {
return this._services;
}
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.identifierPool = new IdentifierPool();
this.componentStorageManager = new ComponentStorageManager();
this.querySystem = new QuerySystem();
this.eventSystem = new TypeSafeEventSystem();
this.referenceTracker = new ReferenceTracker();
2025-10-10 21:52:43 +08:00
this._services = new ServiceContainer();
this.logger = createLogger('Scene');
2025-08-12 09:39:07 +08:00
if (config?.name) {
this.name = config.name;
}
}
/**
*
*
* ServiceContainer
*/
private get performanceMonitor(): PerformanceMonitor {
if (!this._performanceMonitor) {
this._performanceMonitor = this._services.tryResolve(PerformanceMonitor) ?? new PerformanceMonitor();
}
return this._performanceMonitor;
}
/**
*
*
*
*/
public initialize(): void {}
/**
*
*
*
*/
public onStart(): void {}
/**
*
*
*
*/
public unload(): void {}
/**
*
*
* onStart方法
*/
public begin() {
// 标记场景已开始运行并调用onStart方法
this._didSceneBegin = true;
this.onStart();
}
/**
*
*
* unload方法
*/
public end() {
// 标记场景已结束运行
this._didSceneBegin = false;
// 移除所有实体
this.entities.removeAllEntities();
// 清理查询系统中的实体引用和缓存
this.querySystem.setEntities([]);
// 清空组件存储
this.componentStorageManager.clear();
2025-10-10 21:52:43 +08:00
// 清空服务容器会调用所有服务的dispose方法包括所有EntitySystem
this._services.clear();
// 清空系统缓存
this._cachedSystems = null;
this._systemsOrderDirty = true;
// 调用卸载方法
this.unload();
}
/**
2025-09-26 09:38:51 +08:00
*
*/
public update() {
2025-10-10 10:16:44 +08:00
ComponentPoolManager.getInstance().update();
this.entities.updateLists();
2025-10-10 21:52:43 +08:00
const systems = this.systems;
2025-10-10 21:52:43 +08:00
for (const system of systems) {
if (system.enabled) {
try {
system.update();
} catch (error) {
this._handleSystemError(system, 'update', error);
2025-10-10 21:52:43 +08:00
}
}
}
2025-10-10 21:52:43 +08:00
for (const system of systems) {
if (system.enabled) {
try {
system.lateUpdate();
} catch (error) {
this._handleSystemError(system, 'lateUpdate', error);
2025-10-10 21:52:43 +08:00
}
}
}
}
/**
*
*
*
*
* @param system
* @param phase update lateUpdate
* @param error
*/
private _handleSystemError(system: EntitySystem, phase: 'update' | 'lateUpdate', error: unknown): void {
const errorCount = (this._systemErrorCount.get(system) || 0) + 1;
this._systemErrorCount.set(system, errorCount);
this.logger.error(
`Error in system ${system.constructor.name}.${phase}() [${errorCount}/${this._maxErrorCount}]:`,
error
);
if (errorCount >= this._maxErrorCount) {
system.enabled = false;
this.logger.error(
`System ${system.constructor.name} has been disabled due to excessive errors (${errorCount} errors)`
);
}
}
/**
*
* @param name
*/
public createEntity(name: string) {
const 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 {
2025-10-10 21:52:43 +08:00
for (const system of this.systems) {
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;
}
2025-10-10 10:16:44 +08:00
/**
*
*/
public destroyEntities(entities: Entity[]): void {
if (entities.length === 0) return;
for (const entity of entities) {
entity.setDestroyedState(true);
2025-10-10 10:16:44 +08:00
}
for (const entity of entities) {
entity.removeAllComponents();
}
for (const entity of entities) {
this.entities.remove(entity);
this.querySystem.removeEntity(entity);
}
this.querySystem.clearCache();
this.clearSystemEntityCaches();
}
/**
*
*/
public destroyAllEntities() {
this.entities.removeAllEntities();
2025-10-10 10:16:44 +08:00
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
* @deprecated 使 findEntity()
*/
public getEntityByName(name: string): Entity | null {
return this.findEntity(name);
}
/**
*
*
* @param tag
* @deprecated 使 findEntitiesByTag()
*/
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: ComponentType[]): { entities: readonly Entity[] } {
return this.querySystem.queryAll(...componentTypes);
}
/**
*
*
* @param componentTypes -
* @returns
*/
public queryAny(...componentTypes: ComponentType[]): { entities: readonly Entity[] } {
return this.querySystem.queryAny(...componentTypes);
}
/**
*
*
* @param componentTypes -
* @returns
*/
public queryNone(...componentTypes: ComponentType[]): { 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处理器
2025-10-10 21:52:43 +08:00
*
* 使
* 1. 使DI创建实例@Injectable和@Inject装饰器
* 2. 使
*
* @param systemTypeOrInstance
* @returns
*
* @example
* ```typescript
* // 方式1传入类型自动DI推荐
* @Injectable()
* class PhysicsSystem extends EntitySystem {
* constructor(@Inject(CollisionSystem) private collision: CollisionSystem) {
2025-10-14 13:30:48 +08:00
* super(Matcher.empty().all(Transform));
2025-10-10 21:52:43 +08:00
* }
* }
* scene.addEntityProcessor(PhysicsSystem);
*
* // 方式2传入实例
* const system = new MySystem();
* scene.addEntityProcessor(system);
* ```
*/
public addEntityProcessor<T extends EntitySystem>(systemTypeOrInstance: ServiceType<T> | T): T {
2025-10-10 21:52:43 +08:00
let system: T;
let constructor: ServiceType<T>;
2025-10-10 21:52:43 +08:00
if (typeof systemTypeOrInstance === 'function') {
constructor = systemTypeOrInstance;
if (this._services.isRegistered(constructor)) {
const existingSystem = this._services.resolve(constructor) as T;
this.logger.debug(`System ${constructor.name} already registered, returning existing instance`);
return existingSystem;
2025-10-10 21:52:43 +08:00
}
if (isInjectable(constructor)) {
system = createInstance(constructor, this._services) as T;
} else {
system = new constructor() as T;
2025-10-10 21:52:43 +08:00
}
} else {
system = systemTypeOrInstance;
constructor = system.constructor as ServiceType<T>;
2025-10-10 21:52:43 +08:00
if (this._services.isRegistered(constructor)) {
const existingSystem = this._services.resolve(constructor);
if (existingSystem === system) {
this.logger.debug(`System ${constructor.name} instance already registered, returning it`);
return system;
} else {
this.logger.warn(
`Attempting to register a different instance of ${constructor.name}, ` +
'but type is already registered. Returning existing instance.'
);
return existingSystem as T;
}
2025-10-10 21:52:43 +08:00
}
}
2025-10-10 21:52:43 +08:00
system.scene = this;
system.setPerformanceMonitor(this.performanceMonitor);
2025-10-10 21:52:43 +08:00
const metadata = getSystemMetadata(constructor);
if (metadata?.updateOrder !== undefined) {
system.setUpdateOrder(metadata.updateOrder);
}
if (metadata?.enabled !== undefined) {
system.enabled = metadata.enabled;
}
this._services.registerInstance(constructor, system);
// 标记系统列表已变化
this.markSystemsOrderDirty();
injectProperties(system, this._services);
2025-10-10 21:52:43 +08:00
system.initialize();
this.logger.debug(`System ${constructor.name} registered and initialized`);
2025-10-10 21:52:43 +08:00
return system;
}
/**
* EntitySystem到场景使DI
*
* System
* System必须使用@Injectable装饰器标记
*
* @param systemTypes System类型数组
* @returns System实例数组
*
* @example
* ```typescript
* @Injectable()
* @ECSSystem('Collision', { updateOrder: 5 })
* class CollisionSystem extends EntitySystem implements IService {
2025-10-14 13:30:48 +08:00
* constructor() { super(Matcher.empty().all(Collider)); }
2025-10-10 21:52:43 +08:00
* dispose() {}
* }
*
* @Injectable()
* @ECSSystem('Physics', { updateOrder: 10 })
* class PhysicsSystem extends EntitySystem implements IService {
* constructor(@Inject(CollisionSystem) private collision: CollisionSystem) {
2025-10-14 13:30:48 +08:00
* super(Matcher.empty().all(Transform, RigidBody));
2025-10-10 21:52:43 +08:00
* }
* dispose() {}
* }
*
* // 批量注册(自动解析依赖顺序)
* scene.registerSystems([
* CollisionSystem,
* PhysicsSystem, // 自动注入CollisionSystem
* RenderSystem
* ]);
* ```
*/
public registerSystems(systemTypes: Array<ServiceType<EntitySystem>>): EntitySystem[] {
const registeredSystems: EntitySystem[] = [];
2025-10-10 21:52:43 +08:00
for (const systemType of systemTypes) {
const system = this.addEntityProcessor(systemType);
registeredSystems.push(system);
}
return registeredSystems;
}
/**
* addEntityProcessor的别名
* @param system
*/
public addSystem(system: EntitySystem) {
return this.addEntityProcessor(system);
}
/**
* EntitySystem处理器
* @param processor
*/
2025-10-10 21:52:43 +08:00
public removeEntityProcessor(processor: EntitySystem): void {
const constructor = processor.constructor as ServiceType<EntitySystem>;
2025-10-10 21:52:43 +08:00
// 从ServiceContainer移除
this._services.unregister(constructor);
// 标记系统列表已变化
this.markSystemsOrderDirty();
2025-10-10 21:52:43 +08:00
// 重置System状态
processor.reset();
2025-09-24 18:14:22 +08:00
}
/**
* removeEntityProcessor的别名
* @param system
*/
public removeSystem(system: EntitySystem) {
this.removeEntityProcessor(system);
}
/**
* EntitySystem处理器
2025-10-10 21:52:43 +08:00
*
* @deprecated 使使 `scene.services.resolve(SystemType)` System构造函数中使用 `@Inject(SystemType)`
*
* @param type
2025-10-10 21:52:43 +08:00
* @returns null
*
* @example
* ```typescript
* @Injectable()
* class MySystem extends EntitySystem {
* constructor(@Inject(PhysicsSystem) private physics: PhysicsSystem) {
* super();
* }
* }
* ```
*/
public getEntityProcessor<T extends EntitySystem>(type: new (...args: unknown[]) => T): T | null {
return this._services.tryResolve(type as ServiceType<T>) as T | null;
}
/**
*
*/
public getStats(): {
entityCount: number;
processorCount: number;
componentStorageStats: Map<string, { totalSlots: number; usedSlots: number; freeSlots: number; fragmentation: number }>;
} {
return {
entityCount: this.entities.count,
2025-10-10 21:52:43 +08:00
processorCount: this.systems.length,
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, { totalSlots: number; usedSlots: number; freeSlots: number; fragmentation: number }>;
} {
2025-10-10 21:52:43 +08:00
const systems = this.systems;
return {
name: this.name || this.constructor.name,
entityCount: this.entities.count,
2025-10-10 21:52:43 +08:00
processorCount: systems.length,
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: systems.map((processor) => ({
name: getSystemInstanceTypeName(processor),
updateOrder: processor.updateOrder,
entityCount: processor.entities.length
})),
componentStats: this.componentStorageManager.getAllStats()
};
}
2025-10-08 18:34:15 +08:00
/**
*
*
* JSON字符串或二进制Uint8Array
2025-10-08 18:34:15 +08:00
*
* @param options
* @returns JSON字符串或二进制Uint8Array
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
* ```
*/
public serialize(options?: SceneSerializationOptions): string | Uint8Array {
2025-10-08 18:34:15 +08:00
return SceneSerializer.serialize(this, options);
}
/**
*
*
*
*
* @param saveData JSON字符串或二进制Uint8Array
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
* });
* ```
*/
public deserialize(saveData: string | Uint8Array, 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?: unknown;
2025-10-09 12:30:04 +08:00
/**
*
*
*
*
* @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 as Parameters<typeof IncrementalSerializer.computeIncremental>[1], options);
2025-10-09 12:30:04 +08:00
}
/**
*
*
* @param incremental IncrementalSnapshot对象JSON字符串或二进制Uint8Array
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);
*
* // 从二进制Uint8Array应用
2025-10-09 17:14:18 +08:00
* const binaryData = IncrementalSerializer.serializeIncremental(snapshot, { format: 'binary' });
* scene.applyIncremental(binaryData);
2025-10-09 12:30:04 +08:00
* ```
*/
public applyIncremental(
incremental: IncrementalSnapshot | string | Uint8Array,
componentRegistry?: Map<string, ComponentType>
2025-10-09 12:30:04 +08:00
): void {
const isSerializedData = typeof incremental === 'string' || incremental instanceof Uint8Array;
const snapshot = isSerializedData
? IncrementalSerializer.deserializeIncremental(incremental as string | Uint8Array)
: (incremental as IncrementalSnapshot);
2025-10-09 12:30:04 +08:00
const registry = componentRegistry || (ComponentRegistry.getAllComponentNames() as Map<string, ComponentType>);
2025-10-09 12:30:04 +08:00
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;
}
}