2025-11-27 20:42:46 +08:00
|
|
|
import type { IScene } from '@esengine/ecs-framework';
|
|
|
|
|
import { ComponentRegistry } from '@esengine/ecs-framework';
|
2025-12-01 22:28:51 +08:00
|
|
|
import type { IRuntimeModule, IPlugin, PluginDescriptor, SystemContext } from '@esengine/engine-core';
|
2025-11-29 23:00:48 +08:00
|
|
|
import type { AssetManager } from '@esengine/asset-system';
|
2025-11-27 20:42:46 +08:00
|
|
|
|
|
|
|
|
import { TilemapComponent } from './TilemapComponent';
|
|
|
|
|
import { TilemapRenderingSystem } from './systems/TilemapRenderingSystem';
|
2025-11-29 23:00:48 +08:00
|
|
|
import { TilemapCollider2DComponent } from './physics/TilemapCollider2DComponent';
|
|
|
|
|
import { TilemapPhysicsSystem, type IPhysicsWorld } from './physics/TilemapPhysicsSystem';
|
|
|
|
|
import { TilemapLoader } from './loaders/TilemapLoader';
|
2025-12-01 22:28:51 +08:00
|
|
|
import { TilemapAssetType } from './constants';
|
|
|
|
|
|
|
|
|
|
export interface TilemapSystemContext extends SystemContext {
|
|
|
|
|
tilemapSystem?: TilemapRenderingSystem;
|
|
|
|
|
tilemapPhysicsSystem?: TilemapPhysicsSystem;
|
|
|
|
|
physics2DWorld?: IPhysicsWorld;
|
|
|
|
|
assetManager?: AssetManager;
|
|
|
|
|
renderSystem?: any;
|
|
|
|
|
}
|
2025-11-27 20:42:46 +08:00
|
|
|
|
2025-12-01 22:28:51 +08:00
|
|
|
class TilemapRuntimeModule implements IRuntimeModule {
|
2025-11-29 23:00:48 +08:00
|
|
|
private _tilemapPhysicsSystem: TilemapPhysicsSystem | null = null;
|
|
|
|
|
private _loaderRegistered = false;
|
|
|
|
|
|
2025-11-27 20:42:46 +08:00
|
|
|
registerComponents(registry: typeof ComponentRegistry): void {
|
|
|
|
|
registry.register(TilemapComponent);
|
2025-11-29 23:00:48 +08:00
|
|
|
registry.register(TilemapCollider2DComponent);
|
2025-11-27 20:42:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createSystems(scene: IScene, context: SystemContext): void {
|
2025-12-01 22:28:51 +08:00
|
|
|
const tilemapContext = context as TilemapSystemContext;
|
|
|
|
|
|
|
|
|
|
if (!this._loaderRegistered && tilemapContext.assetManager) {
|
|
|
|
|
tilemapContext.assetManager.registerLoader(TilemapAssetType, new TilemapLoader());
|
2025-11-29 23:00:48 +08:00
|
|
|
this._loaderRegistered = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-27 20:42:46 +08:00
|
|
|
const tilemapSystem = new TilemapRenderingSystem();
|
|
|
|
|
scene.addSystem(tilemapSystem);
|
|
|
|
|
|
2025-12-01 22:28:51 +08:00
|
|
|
if (tilemapContext.renderSystem) {
|
|
|
|
|
tilemapContext.renderSystem.addRenderDataProvider(tilemapSystem);
|
2025-11-27 20:42:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-01 22:28:51 +08:00
|
|
|
tilemapContext.tilemapSystem = tilemapSystem;
|
2025-11-29 23:00:48 +08:00
|
|
|
|
|
|
|
|
this._tilemapPhysicsSystem = new TilemapPhysicsSystem();
|
|
|
|
|
scene.addSystem(this._tilemapPhysicsSystem);
|
|
|
|
|
|
2025-12-01 22:28:51 +08:00
|
|
|
tilemapContext.tilemapPhysicsSystem = this._tilemapPhysicsSystem;
|
2025-11-29 23:00:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onSystemsCreated(_scene: IScene, context: SystemContext): void {
|
2025-12-01 22:28:51 +08:00
|
|
|
const tilemapContext = context as TilemapSystemContext;
|
|
|
|
|
|
|
|
|
|
if (this._tilemapPhysicsSystem && tilemapContext.physics2DWorld) {
|
|
|
|
|
this._tilemapPhysicsSystem.setPhysicsWorld(tilemapContext.physics2DWorld);
|
2025-11-29 23:00:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get tilemapPhysicsSystem(): TilemapPhysicsSystem | null {
|
|
|
|
|
return this._tilemapPhysicsSystem;
|
2025-11-27 20:42:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-01 22:28:51 +08:00
|
|
|
|
|
|
|
|
const descriptor: PluginDescriptor = {
|
|
|
|
|
id: '@esengine/tilemap',
|
|
|
|
|
name: 'Tilemap',
|
|
|
|
|
version: '1.0.0',
|
|
|
|
|
description: 'Tilemap system with Tiled editor support',
|
|
|
|
|
category: 'tilemap',
|
|
|
|
|
enabledByDefault: false,
|
|
|
|
|
isEnginePlugin: true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const TilemapPlugin: IPlugin = {
|
|
|
|
|
descriptor,
|
|
|
|
|
runtimeModule: new TilemapRuntimeModule()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { TilemapRuntimeModule };
|