Ghosts can't teleport

This commit is contained in:
Martin Berg Alstad 2023-05-24 18:11:41 +02:00
parent 76eca2d9ca
commit dc0e5a342e

View File

@ -1,5 +1,5 @@
import {TileType} from "./tileType"; import {TileType} from "./tileType";
import {Character} from "./character"; import {Character, PacMan} from "./character";
/** /**
* Finds all the possible positions for the character to move to * Finds all the possible positions for the character to move to
@ -9,14 +9,15 @@ import {Character} from "./character";
*/ */
export default function findPossiblePositions(board: GameMap, character: Character, steps: number): Position[] { export default function findPossiblePositions(board: GameMap, character: Character, steps: number): Position[] {
const possiblePositions: Position[] = []; const possiblePositions: Position[] = [];
findPossibleRecursive(board, character.position, steps, possiblePositions, []); findPossibleRecursive(board, character.position, steps, character instanceof PacMan, possiblePositions, []);
return possiblePositions; return possiblePositions;
} }
function findPossibleRecursive(board: GameMap, currentPos: Position, steps: number, function findPossibleRecursive(board: GameMap, currentPos: Position, steps: number, isPacMan: boolean,
possibleList: Position[], visitedTiles: Position[]): Position | null { possibleList: Position[], visitedTiles: Position[]): Position | null {
if (isOutsideBoard(currentPos, board.length)) {
addTeleportationTiles(board, currentPos, steps, possibleList, visitedTiles); if (isPacMan && isOutsideBoard(currentPos, board.length)) {
addTeleportationTiles(board, currentPos, steps, isPacMan, possibleList, visitedTiles);
} else if (visitedTiles.find(tile => tile.x === currentPos.x && tile.y === currentPos.y)) { // TODO might be true when teleporting, when it shouldn't (1,5) and 6 steps } else if (visitedTiles.find(tile => tile.x === currentPos.x && tile.y === currentPos.y)) { // TODO might be true when teleporting, when it shouldn't (1,5) and 6 steps
return null; return null;
} else if (isWall(board, currentPos)) { } else if (isWall(board, currentPos)) {
@ -27,23 +28,35 @@ function findPossibleRecursive(board: GameMap, currentPos: Position, steps: numb
const nextStep = steps - 1; const nextStep = steps - 1;
const result = { const result = {
up: findPossibleRecursive(board, {x: currentPos.x, y: currentPos.y + 1}, nextStep, possibleList, visitedTiles), up: findPossibleRecursive(board, {
right: findPossibleRecursive(board, {x: currentPos.x + 1, y: currentPos.y}, nextStep, possibleList, visitedTiles), x: currentPos.x,
down: findPossibleRecursive(board, {x: currentPos.x, y: currentPos.y - 1}, nextStep, possibleList, visitedTiles), y: currentPos.y + 1
left: findPossibleRecursive(board, {x: currentPos.x - 1, y: currentPos.y}, nextStep, possibleList, visitedTiles), }, nextStep, isPacMan, possibleList, visitedTiles),
right: findPossibleRecursive(board, {
x: currentPos.x + 1,
y: currentPos.y
}, nextStep, isPacMan, possibleList, visitedTiles),
down: findPossibleRecursive(board, {
x: currentPos.x,
y: currentPos.y - 1
}, nextStep, isPacMan, possibleList, visitedTiles),
left: findPossibleRecursive(board, {
x: currentPos.x - 1,
y: currentPos.y
}, nextStep, isPacMan, possibleList, visitedTiles),
}; };
pushToList(board, possibleList, Object.values(result)); pushToList(board, possibleList, Object.values(result));
return null; return null;
} }
function addTeleportationTiles(board: number[][], currentPos: Position, steps: number, function addTeleportationTiles(board: number[][], currentPos: Position, steps: number, isPacMan: boolean,
possibleList: Position[], visitedTiles: Position[]): void { possibleList: Position[], visitedTiles: Position[]): void {
const newPositons: (Position | null)[] = []; const newPositons: (Position | null)[] = [];
const possiblePositions = findTeleportationTiles(board); const possiblePositions = findTeleportationTiles(board);
for (const pos of possiblePositions) { for (const pos of possiblePositions) {
if (pos.x !== Math.max(currentPos.x, 0) || pos.y !== Math.max(currentPos.y, 0)) { if (pos.x !== Math.max(currentPos.x, 0) || pos.y !== Math.max(currentPos.y, 0)) {
newPositons.push(findPossibleRecursive(board, pos, steps, possibleList, visitedTiles)); newPositons.push(findPossibleRecursive(board, pos, steps, isPacMan, possibleList, visitedTiles));
} }
} }
pushToList(board, possibleList, newPositons); pushToList(board, possibleList, newPositons);