const L = 10;export let step: boolean = true;export const onInsNewWall: Action3<number, number, number> = new Action3();InputUtil.onKeyDown(Keys.O, () => { step = true;})export async function CreateMaze() { let Maze = []; for (let index = 0; index < L; index++) { Maze[index] = new Array(L).fill(0, 0, L); } //最外围设置为路,可以有效的保护里面一层墙体,并防止挖出界 for (let i = 0; i < L; i++) { Maze[0] = 1; Maze[0] = 1; Maze[L - 1] = 1; Maze[L - 1] = 1; } //任取初始值 let posArr: Vector2[] = []; posArr.push(new Vector2(2, 2)) //当墙队列为空时结束循环 while (posArr.length != 0) { //在墙队列中随机取一点 let r = MathUtil.randomInt(0, posArr.length); let x = posArr[r].x; let y = posArr[r].y; await new Promise((resolve) => { const interval = setInterval(() => { if (step) { resolve(0) clearInterval(interval); } }, 100); }); step = false; //判读上下左右四个方向是否为路 let count = 0; for (let i = x - 1; i < x + 2; i++) { for (let j = y - 1; j < y + 2; j++) { if (Math.abs(x - i) + Math.abs(y - j) == 1 && Maze[j] > 0) { ++count; } } } if (count <= 1) { Maze[x][y] = 1; onInsNewWall.call(x, y, 0); //在墙队列中插入新的墙 for (let i = x - 1; i < x + 2; i++) { for (let j = y - 1; j < y + 2; j++) { if (Math.abs(x - i) + Math.abs(y - j) == 1 && Maze[j] == 0) { posArr.push(new Vector2(i, j)); onInsNewWall.call(i, j, 1); } } } } console.log("curWay" + posArr.length); //删除当前墙 posArr.splice(r, 1); onInsNewWall.call(x, y, 2); } //设置迷宫进出口 Maze[2][1] = 1; onInsNewWall.call(2, 1, 0); for (let i = L - 3; i >= 0; i--) { if (Maze[L - 3] == 1) { Maze[L - 2] = 1; onInsNewWall.call(i, L - 2, 0); break; } } return Maze;}