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.
 
 
 
 

45 lines
1.3 KiB

import type { MapRegionConfig } from '../config/BattleConfigTypes';
import type { Cell } from './Cell';
/** 地图区域(全图 / 子区域) */
export class MapRegion {
/**
* @param config 区域矩形配置
*/
constructor(public config: MapRegionConfig) {}
/**
* @param cell 格子坐标
* @returns 是否在区域内
*/
contains(cell: Cell): boolean {
const { startRow, rowCount, startCol, colCount } = this.config;
return (
cell.row >= startRow &&
cell.row < startRow + rowCount &&
cell.col >= startCol &&
cell.col < startCol + colCount
);
}
/**
* @param anchor 锚点格子
* @returns 钳制到区域内的锚点
*/
clampAnchor(anchor: Cell): Cell {
const { startRow, rowCount, startCol, colCount } = this.config;
return {
row: Math.min(Math.max(anchor.row, startRow), startRow + rowCount - 1),
col: Math.min(Math.max(anchor.col, startCol), startCol + colCount - 1),
};
}
}
/**
* @param rows 行数,默认 12
* @param cols 列数,默认 10
* @returns 全图部署区域
*/
export function createSoloRegion(rows = 12, cols = 10): MapRegion {
return new MapRegion({ startRow: 0, rowCount: rows, startCol: 0, colCount: cols });
}