You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AdLike2048/assets/script/battle/map/MapGridSpecReader.ts

64 lines
2.2 KiB

3 weeks ago
import { BattleMapSpec } from './BattleMapSpec';
import type { MapManager } from './legacy/MapManager';
import { GBundle } from '../../game/ConfigRes';
/** 地图格子规格(以预制体 MapManager 为准) */
export interface MapGridSpec {
/** 地图行数 */
rows: number;
/** 地图列数 */
cols: number;
/** 单格像素边长 */
cellSize: number;
}
/** 从 MapManager 读取格子规格 */
export function readMapGridSpec(map: Pick<MapManager, 'mapRows' | 'mapCols' | 'mapGridSize'>): MapGridSpec {
return {
rows: map.mapRows,
cols: map.mapCols,
cellSize: map.mapGridSize,
};
}
/** 校验 MapManager 格子参数是否合法 */
export function isValidMapGridSpec(spec: MapGridSpec): boolean {
return spec.rows > 0 && spec.cols > 0 && spec.cellSize > 0
&& Number.isFinite(spec.rows) && Number.isFinite(spec.cols) && Number.isFinite(spec.cellSize);
}
/**
* 从已预载的 mapLayer 预制体探测格子规格(不挂场景,探测后立即归还节点池)。
* 须在 battleMap bundle 预载完成后调用。
*/
export function probeMapGridSpecFromPrefab(mapPrefabName: string): MapGridSpec | null {
const name = String(mapPrefabName ?? '').trim();
if (!name) {
console.log('[MapGridSpecReader] 地图预制体名为空');
return null;
}
if (typeof gg === 'undefined' || !gg.res?.getNode) {
console.log('[MapGridSpecReader] gg.res 未就绪,无法探测地图规格', name);
return null;
}
const path = BattleMapSpec.PREFAB_DIR;
const mapNode = gg.res.getNode(path, name, GBundle.BattleMap);
if (!mapNode?.isValid) {
console.log('[MapGridSpecReader] 地图预制体未在资源池', name);
return null;
}
const mapMgr = mapNode.getComponent('MapManager') as MapManager | null;
if (!mapMgr) {
console.error('[MapGridSpecReader] 预制体缺少 MapManager', name);
gg.res.putNode(mapNode);
return null;
}
const spec = readMapGridSpec(mapMgr);
gg.res.putNode(mapNode);
if (!isValidMapGridSpec(spec)) {
console.error('[MapGridSpecReader] MapManager 格子参数非法', name);
return null;
}
return spec;
}