import { Vec2 } from "cc"; export default class map { // private _grid: (number | null)[][]; private _rows: number = 10; private _cols: number = 10; private _cw: number = 0; private _ch: number = 0; private _width: number = 0; private _height: number = 0; private _wx: number = 0; private _wy: number = 0; public get rows(): number { return this._rows; } public get cols(): number { return this._cols; } public get widht(): number { return this._width; } public get height(): number { return this._height; } public get max_count(): number { return this._rows * this.cols }; public get grid(): (number | null)[][] { return this._grid }; public init(rows?: number, cols?: number): void { if (rows) this._rows = rows; if (cols) this._cols = cols; this._grid = Array.from({ length: this._rows }, () => Array.from({ length: this._cols }, () => null)); for (let i = 0; i < this._rows; i++) { for (let j = 0; j < this._cols; j++) { this._grid[i][j] = null; } } } /**空位数量*/ public countNulls(): number { return this._grid.reduce((acc, row) => { return acc + row.reduce((rowAcc, cell) => rowAcc + (cell === null ? 1 : 0), 0); }, 0); } public dispose(): void { this.init(); } /**设置格子宽高和起始世界坐标*/ public setWH(w: number, h: number, wx: number, wy: number): void { this._cw = w; this._ch = h; this._wx = wx; this._wy = wy; this._width = w * this._rows; this._height = h * this._cols; } /**格子坐标转世界坐标*/ public gridToPosV2(rows: number, cols: number): Vec2 { return new Vec2((rows + 0.5) * this._cw + this._wx, -(cols + 0.5) * this._ch + this._wy); } //占格 public set_id(x: number, y: number, id: number) { this._grid[x][y] = id; } /**清空格子*/ public set_empty(x: number, y: number) { this._grid[x][y] = null; } public lock(x: number, y: number) { this._grid[x][y] = -1; } public unlock(x: number, y: number) { this._grid[x][y] = null; } /**是否在格子里*/ private isInBounds(x: number, y: number, bounds: number = 0): boolean { return x >= bounds && x < this._rows - bounds && y >= bounds && y < this._cols - bounds; } /**检测某个格子是否为空 bounds边框宽度一般为0*/ public check_empty(x: number, y: number, bounds: number = 0): boolean { if (!this.isInBounds(x, y, bounds)) return false; return this._grid[x][y] == null; } public get_id(x: number, y: number, bounds: number = 0): number | null { if (!this.isInBounds(x, y, bounds)) return null; return this._grid[x][y]; } /**检测某个格子是否为锁*/ public check_lock(x: number, y: number, bounds: number = 0): boolean { if (!this.isInBounds(x, y, bounds)) return false; return this._grid[x][y] != null && this._grid[x][y] < 0; } /**找出有邻居的组合*/ public get_neighbor_list(): number[][] { let list = []; for (let i = 0; i < this._rows; i++) { for (let j = 0; j < this._cols; j++) { let t = this._grid[i][j]; if (t && t >= 0) { let neighbors: number[] = this.getNeighbors(i, j); if (neighbors.length > 0) { neighbors.push(t);//加入自己 list.push(neighbors);//加到列表 } } } } return list; } //4方法 private readonly directions4 = [ { dx: -1, dy: 0 }, // 上 { dx: 1, dy: 0 }, // 下 { dx: 0, dy: -1 }, // 左 { dx: 0, dy: 1 } // 右 ]; //8方向 private readonly directions8 = [ { dx: -1, dy: 0 }, // 上 { dx: 1, dy: 0 }, // 下 { dx: 0, dy: -1 }, // 左 { dx: 0, dy: 1 }, // 右 { dx: -1, dy: -1 }, // 左上 { dx: -1, dy: 1 }, // 右上 { dx: 1, dy: -1 }, // 左下 { dx: 1, dy: 1 } // 右下 ]; /**邻居是否为空*/ public hasEmptyNeighbor(x: number, y: number): boolean { //检查上下左右的边界 for (const { dx, dy } of this.directions4) { const newX = x + dx; const newY = y + dy; if (this.check_empty(newX, newY)) return true; } return false; //没有空的邻居 } /**获取所有在存的邻居*/ public getNeighbors(x: number, y: number): number[] { let list: number[] = [] for (const { dx, dy } of this.directions4) { const newX = x + dx; const newY = y + dy; if (this.isInBounds(newX, newY)) { let id = this._grid[newX][newY]; if (id && id >= 0) list.push(id); } } return list; } }