import { Direction, MazeMap, MazeTile, } from "./MazeMap"; // define a few shorthand aliases const t = Direction.top; const l = Direction.left; const b = Direction.bottom; const r = Direction.right; const n = Direction.none; function numberToDirections(a:number): Direction[] { if(a === (Direction.none as number)){ return [Direction.none]; } const output:Direction[] = []; if(a & Direction.top){ output.push(Direction.top); } if(a & Direction.left){ output.push(Direction.left); } if(a & Direction.bottom){ output.push(Direction.bottom); } if(a & Direction.right){ output.push(Direction.right); } return output; } function numberMapToTileMap(map:number[][]): MazeTile[][] { const output:MazeTile[][] = []; let i = 0; for (const row of map) { const newRow = []; for (const a of row) { const tile = new MazeTile(i+""); tile.paths = numberToDirections(a); newRow.push(tile); i++; } output.push(newRow); } return output; } function parseStringMap(map:string): MazeTile[][] { map = map.replace("\r", ""); const rows = map.split("\n"); const cells = rows.map(row => row.split("")); let idCounter = 0; const output:MazeTile[][] = []; for (let i = 0; i < (cells.length+1)/2; i++) { output.push([]); for (let j = 0; j < (cells[i].length+1)/2; j++) { output[i].push(new MazeTile(idCounter+"")); idCounter++; } } for (let i = 0; i < cells.length; i++) { const row = cells[i]; for (let j = 0; j < row.length; j++) { const cell = row[j]; if(i % 2 === 0) { // we're in a row with vertical pathways if(j % 2 === 1) { // we're in a cell with a connection definition if(cell === "-"){ output[i/2][(j-1)/2].paths.push(Direction.right); output[i/2][(j+1)/2].paths.push(Direction.left); } } } else { // we're in a row with horizontal pathways if(j % 2 === 0) { // we're in a cell with a connection definition if(cell === "|") { output[(i-1)/2][j/2].paths.push(Direction.bottom); output[(i+1)/2][j/2].paths.push(Direction.top); } } } } } // make sure every tile has at least one direction element for (const row of output) { for (const tile of row) { if(tile.paths.length === 0){ tile.paths.push(Direction.none); } } } return output; } function createLvl1(): MazeMap { const maze = new MazeMap("lvl1"); /* +-+ | | +-+ */ const map = [ [b|r, l|b], [t|r, t|l], ]; maze.layout = numberMapToTileMap(map); maze.startPosition = [0,0]; return maze; } function createLvl2(): MazeMap { const maze = new MazeMap("lvl2"); const map = "+-+-+\n"+ "| | |\n"+ "+ + +\n"+ "| |\n"+ "+-+-+"; maze.layout = parseStringMap(map); maze.startPosition = [0,0]; return maze; } export function createSampleMaps(): MazeMap[] { const output:MazeMap[] = []; output.push(createLvl1()); output.push(createLvl2()); return output; }