Files
esengine/packages/framework/world-streaming/src/WorldStreamingModule.ts

101 lines
2.8 KiB
TypeScript
Raw Normal View History

import type { IScene, ServiceContainer, IComponentRegistry } from '@esengine/ecs-framework';
import { ChunkComponent } from './components/ChunkComponent';
import { StreamingAnchorComponent } from './components/StreamingAnchorComponent';
import { ChunkLoaderComponent } from './components/ChunkLoaderComponent';
import { ChunkStreamingSystem } from './systems/ChunkStreamingSystem';
import { ChunkCullingSystem } from './systems/ChunkCullingSystem';
import { ChunkManager } from './services/ChunkManager';
/**
*
*
* Configuration for world streaming setup.
*/
export interface IWorldStreamingSetupOptions {
/**
*
*
* Chunk size in world units.
*/
chunkSize?: number;
/**
* Culling
*
* Whether to add the culling system.
*/
bEnableCulling?: boolean;
}
/**
*
*
* Helper class for setting up world streaming functionality.
*
*
*/
export class WorldStreamingModule {
private _chunkManager: ChunkManager | null = null;
get chunkManager(): ChunkManager | null {
return this._chunkManager;
}
/**
*
*
* Register streaming components to registry.
*/
registerComponents(registry: IComponentRegistry): void {
registry.register(ChunkComponent);
registry.register(StreamingAnchorComponent);
registry.register(ChunkLoaderComponent);
}
/**
*
*
* Register streaming services to container.
*/
registerServices(services: ServiceContainer, chunkSize?: number): void {
this._chunkManager = new ChunkManager(chunkSize);
services.registerInstance(ChunkManager, this._chunkManager);
}
/**
*
*
* Create and add streaming systems to scene.
*/
createSystems(scene: IScene, options?: IWorldStreamingSetupOptions): void {
const streamingSystem = new ChunkStreamingSystem();
if (this._chunkManager) {
streamingSystem.setChunkManager(this._chunkManager);
}
scene.addSystem(streamingSystem);
if (options?.bEnableCulling !== false) {
scene.addSystem(new ChunkCullingSystem());
}
}
/**
*
*
* Setup world streaming in one call.
*/
setup(
scene: IScene,
services: ServiceContainer,
registry: IComponentRegistry,
options?: IWorldStreamingSetupOptions
): ChunkManager {
this.registerComponents(registry);
this.registerServices(services, options?.chunkSize);
this.createSystems(scene, options);
return this._chunkManager!;
}
}
export const worldStreamingModule = new WorldStreamingModule();