2025-12-08 21:26:35 +08:00
|
|
|
import type { IScene } from '@esengine/ecs-framework';
|
|
|
|
|
import { ComponentRegistry } from '@esengine/ecs-framework';
|
2025-12-13 19:44:08 +08:00
|
|
|
import type { IRuntimeModule, IRuntimePlugin, ModuleManifest, SystemContext } from '@esengine/engine-core';
|
2025-12-08 21:10:57 +08:00
|
|
|
import { AssetManagerToken } from '@esengine/asset-system';
|
|
|
|
|
import { RenderSystemToken } from '@esengine/ecs-engine-bindgen';
|
|
|
|
|
import { Physics2DWorldToken } from '@esengine/physics-rapier2d';
|
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';
|
2025-12-08 21:10:57 +08:00
|
|
|
import { TilemapPhysicsSystem } from './physics/TilemapPhysicsSystem';
|
2025-11-29 23:00:48 +08:00
|
|
|
import { TilemapLoader } from './loaders/TilemapLoader';
|
2025-12-01 22:28:51 +08:00
|
|
|
import { TilemapAssetType } from './constants';
|
2025-12-08 21:10:57 +08:00
|
|
|
import {
|
|
|
|
|
TilemapSystemToken,
|
|
|
|
|
TilemapPhysicsSystemToken
|
|
|
|
|
} from './tokens';
|
2025-12-01 22:28:51 +08:00
|
|
|
|
2025-12-08 21:10:57 +08:00
|
|
|
// 重新导出 tokens | Re-export tokens
|
|
|
|
|
export {
|
|
|
|
|
TilemapSystemToken,
|
|
|
|
|
TilemapPhysicsSystemToken
|
|
|
|
|
} from './tokens';
|
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-08 21:10:57 +08:00
|
|
|
// 从服务注册表获取依赖 | Get dependencies from service registry
|
|
|
|
|
const assetManager = context.services.get(AssetManagerToken);
|
|
|
|
|
const renderSystem = context.services.get(RenderSystemToken);
|
2025-12-01 22:28:51 +08:00
|
|
|
|
2025-12-08 21:10:57 +08:00
|
|
|
if (!this._loaderRegistered && assetManager) {
|
|
|
|
|
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-08 21:10:57 +08:00
|
|
|
if (renderSystem) {
|
|
|
|
|
renderSystem.addRenderDataProvider(tilemapSystem);
|
2025-11-27 20:42:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-29 23:00:48 +08:00
|
|
|
this._tilemapPhysicsSystem = new TilemapPhysicsSystem();
|
|
|
|
|
scene.addSystem(this._tilemapPhysicsSystem);
|
|
|
|
|
|
2025-12-08 21:10:57 +08:00
|
|
|
// 注册服务到服务注册表 | Register services to service registry
|
|
|
|
|
context.services.register(TilemapSystemToken, tilemapSystem);
|
|
|
|
|
context.services.register(TilemapPhysicsSystemToken, this._tilemapPhysicsSystem);
|
2025-11-29 23:00:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onSystemsCreated(_scene: IScene, context: SystemContext): void {
|
2025-12-08 21:10:57 +08:00
|
|
|
// 从服务注册表获取物理世界 | Get physics world from service registry
|
|
|
|
|
const physics2DWorld = context.services.get(Physics2DWorldToken);
|
2025-12-01 22:28:51 +08:00
|
|
|
|
2025-12-08 21:10:57 +08:00
|
|
|
if (this._tilemapPhysicsSystem && physics2DWorld) {
|
|
|
|
|
this._tilemapPhysicsSystem.setPhysicsWorld(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
|
|
|
|
2025-12-03 22:15:22 +08:00
|
|
|
const manifest: ModuleManifest = {
|
|
|
|
|
id: 'tilemap',
|
|
|
|
|
name: '@esengine/tilemap',
|
|
|
|
|
displayName: 'Tilemap 2D',
|
2025-12-01 22:28:51 +08:00
|
|
|
version: '1.0.0',
|
|
|
|
|
description: 'Tilemap system with Tiled editor support',
|
2025-12-03 22:15:22 +08:00
|
|
|
category: 'Rendering',
|
|
|
|
|
icon: 'Grid3X3',
|
|
|
|
|
isCore: false,
|
|
|
|
|
defaultEnabled: false,
|
|
|
|
|
isEngineModule: true,
|
|
|
|
|
canContainContent: true,
|
|
|
|
|
dependencies: ['core', 'math', 'sprite', 'asset-system'],
|
|
|
|
|
exports: { components: ['TilemapComponent', 'TilemapCollider2DComponent'] },
|
|
|
|
|
editorPackage: '@esengine/tilemap-editor'
|
2025-12-01 22:28:51 +08:00
|
|
|
};
|
|
|
|
|
|
2025-12-13 19:44:08 +08:00
|
|
|
export const TilemapPlugin: IRuntimePlugin = {
|
2025-12-03 22:15:22 +08:00
|
|
|
manifest,
|
2025-12-01 22:28:51 +08:00
|
|
|
runtimeModule: new TilemapRuntimeModule()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { TilemapRuntimeModule };
|