Files
cocos/assets/scripts/level/LevelTileLayout.ts
刘宇飞 d393302388 Complete Cocos Creator port with level bundles, themes, and tooling.
Adds level prefabs, theme assets, audio, extensions, and deployment scripts for the Unity WebGL migration.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-16 15:30:58 +08:00

54 lines
1.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { _decorator, Component } from 'cc';
import { LevelMapData } from './LevelMapData';
import { layoutLevelTiles } from './TileLayout';
import { LevelConfig } from './LevelTypes';
const { ccclass, executionOrder } = _decorator;
/**
* 挂在 LevelN 根节点:运行时首帧按 levels-database 重新对齐砖块。
* 解决「编辑器 JSON 正确、运行预览叠在一起 / 只显示一块」。
*/
@ccclass('LevelTileLayout')
@executionOrder(-50)
export class LevelTileLayout extends Component {
/** 运行时权威配置(来自 levels-database.json优先于预制体 LevelMapData */
runtimeConfig: LevelConfig | null = null;
start() {
this.applyLayout();
this.scheduleOnce(() => this.applyLayout(), 0);
}
setRuntimeConfig(config: LevelConfig) {
this.runtimeConfig = config;
}
applyLayout() {
const config = this.runtimeConfig ?? this.configFromMapData();
if (!config) return;
layoutLevelTiles(this.node, config);
}
private configFromMapData(): LevelConfig | null {
const md = this.getComponent(LevelMapData);
if (!md) return null;
let ground: LevelConfig['ground'];
let border: LevelConfig['border'];
try {
ground = JSON.parse(md.groundJson || '{}');
border = JSON.parse(md.borderJson || '{}');
} catch {
return null;
}
return {
levelID: md.levelID,
boundary: { x: 0, y: 0 },
spawns: [],
theme: md.theme || 'silu',
ground,
border,
};
}
}