2025-11-27 20:42:46 +08:00
|
|
|
import type { IScene } from '@esengine/ecs-framework';
|
|
|
|
|
import { ComponentRegistry } from '@esengine/ecs-framework';
|
2025-12-03 22:15:22 +08:00
|
|
|
import type { IRuntimeModule, IPlugin, ModuleManifest, 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
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const TilemapPlugin: IPlugin = {
|
2025-12-03 22:15:22 +08:00
|
|
|
manifest,
|
2025-12-01 22:28:51 +08:00
|
|
|
runtimeModule: new TilemapRuntimeModule()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { TilemapRuntimeModule };
|