Position is updated on all clients

This commit is contained in:
martin 2023-06-08 19:48:27 +02:00
parent 58ab8d68ee
commit ce611447ce
4 changed files with 46 additions and 34 deletions

View File

@ -9,7 +9,7 @@ import Pellet from "../game/pellet";
interface BoardProps extends ComponentProps {
characters: Character[],
selectedDice?: SelectedDice,
onMove?: BiAction<Character, Position[]>,
onMove?: Action<Position[]>,
map: GameMap
}
@ -45,7 +45,7 @@ const Board: Component<BoardProps> = (
selectedCharacter.follow(destination);
const positions = pickUpPellets(destination);
onMove?.(selectedCharacter, positions);
onMove?.(positions);
setSelectedCharacter(undefined);
}
}
@ -54,7 +54,6 @@ const Board: Component<BoardProps> = (
const takenChar = characters.find(c => c.isPacMan() && c.isAt(destination.end));
if (takenChar) {
takenChar.moveToSpawn();
// TODO send message to other client
// TODO steal from player
}
}

View File

@ -50,7 +50,7 @@ export const GameComponent: Component = () => {
break;
case GameAction.moveCharacter:
setDice(parsed.Data?.dice as number[]);
updateCharacter(parsed);
updateCharacters(parsed);
removeEatenPellets(parsed);
break;
}
@ -59,25 +59,28 @@ export const GameComponent: Component = () => {
function removeEatenPellets(parsed: ActionMessage): void {
const pellets = parsed.Data?.eatenPellets as Position[];
for (const pellet of pellets){
testMap[pellet.y][pellet.x] = TileType.empty;
}
}
function updateCharacter(parsed: ActionMessage): void {
const updatedCharacter = parsed.Data?.character satisfies Ghost | PacMan;
const characterIndex = characters.findIndex(c => c.colour === updatedCharacter.colour);
if (characters[characterIndex]) {
if (updatedCharacter["box"] !== undefined) { // If Pac-Man
(characters[characterIndex] as PacMan) = new PacMan(updatedCharacter);
} else if (updatedCharacter satisfies CharacterProps) {
(characters[characterIndex] as Ghost) = new Ghost(updatedCharacter);
}
for (const pellet of pellets) {
testMap[pellet.y][pellet.x] = TileType.empty;
}
}
function onCharacterMove(character: Character, eatenPellets: Position[]): void {
function updateCharacters(parsed: ActionMessage): void {
const updatedCharacters = parsed.Data?.characters as (PacManProps | CharacterProps)[] | undefined;
if (updatedCharacters) {
const newList: Character[] = [];
for (const character of updatedCharacters) {
if ("box" in character) { // If Pac-Man
newList.push(new PacMan(character));
} else if (character satisfies CharacterProps) {
newList.push(new Ghost(character));
}
}
setCharacters(newList);
}
}
function onCharacterMove(eatenPellets: Position[]): void {
if (dice && selectedDice) {
dice.splice(selectedDice.index, 1);
}
@ -86,7 +89,7 @@ export const GameComponent: Component = () => {
Action: GameAction.moveCharacter,
Data: {
dice: dice?.length ?? 0 > 0 ? dice : null,
character: character,
characters: characters,
eatenPellets: eatenPellets
}
};

View File

@ -41,7 +41,7 @@ function findPossibleRecursive(board: GameMap, currentPath: Path, steps: number,
paths.push(currentPath);
}
} else if (character.isGhost() && characters.find(c => c.isPacMan() && c.isAt(currentPath.end))) {
} else if (ghostHitsPacMan(character, currentPath, characters)) {
paths.push(currentPath);
} else {
@ -56,6 +56,16 @@ function findPossibleRecursive(board: GameMap, currentPath: Path, steps: number,
return paths;
}
/**
* Checks if the current character is a ghost, and Pac-Man is on the same tile
* @param character The current character
* @param currentPath The current path the character is on
* @param characters All the characters on the board
*/
function ghostHitsPacMan(character: Character, currentPath: Path, characters: Character[]): boolean {
return character.isGhost() && characters.find(c => c.isPacMan() && c.isAt(currentPath.end)) !== undefined;
}
/**
* Adds the current position to the path, if it's not on the spawn and it's not already in the path
* @param currentPos The current path the character is on

View File

@ -13,28 +13,28 @@ beforeEach(() => {
});
test("Pac-Man rolls one from start, should return one position", () => {
const result = possibleMovesAlgorithm(testMap, pacMan, 1);
const result = possibleMovesAlgorithm(testMap, pacMan, 1, []);
expect(result.length).toBe(1);
expect(result[0].path?.length).toBe(0);
expect(result).toEqual([{end: {x: 3, y: 2}, direction: Direction.up, path: []}]);
});
test("Pac-Man rolls two from start, should return one position", () => {
const result = possibleMovesAlgorithm(testMap, pacMan, 2);
const result = possibleMovesAlgorithm(testMap, pacMan, 2, []);
expect(result.length).toBe(1);
expect(result[0].path?.length).toBe(1);
expect(result).toEqual([{end: {x: 3, y: 1}, direction: Direction.up, path: [{x: 3, y: 2}]}]);
});
test("Pac-Man rolls three from start, should return two positions", () => {
const result = possibleMovesAlgorithm(testMap, pacMan, 3);
const result = possibleMovesAlgorithm(testMap, pacMan, 3, []);
expect(result.length).toBe(2);
arrayEquals(result, [{end: {x: 2, y: 1}, direction: Direction.left, path: [{x: 3, y: 2}, {x: 3, y: 1}]},
{end: {x: 4, y: 1}, direction: Direction.right, path: [{x: 3, y: 2}, {x: 3, y: 1}]}]);
});
test("Pac-Man rolls four from start, should return two positions", () => {
const result = possibleMovesAlgorithm(testMap, pacMan, 4);
const result = possibleMovesAlgorithm(testMap, pacMan, 4, []);
expect(result.length).toBe(2);
arrayEquals(result, [{
end: {x: 1, y: 1},
@ -48,7 +48,7 @@ test("Pac-Man rolls four from start, should return two positions", () => {
});
test("Pac-Man rolls five from start, should return four positions", () => {
const result = possibleMovesAlgorithm(testMap, pacMan, 5);
const result = possibleMovesAlgorithm(testMap, pacMan, 5, []);
expect(result.length).toBe(4);
arrayEquals(result, [{
end: {x: 5, y: 0},
@ -71,7 +71,7 @@ test("Pac-Man rolls five from start, should return four positions", () => {
});
test("Pac-Man rolls six from start, should return six positions", () => {
const result = possibleMovesAlgorithm(testMap, pacMan, 6);
const result = possibleMovesAlgorithm(testMap, pacMan, 6, []);
expect(result.length).toBe(6);
arrayEquals(result, [
{
@ -104,19 +104,19 @@ test("Pac-Man rolls six from start, should return six positions", () => {
test("Pac-Man rolls four from position [5,1] (right), should return 11", () => {
pacMan.follow({end: {x: 5, y: 1}, direction: Direction.right});
const result = possibleMovesAlgorithm(testMap, pacMan, 4);
const result = possibleMovesAlgorithm(testMap, pacMan, 4, []);
expect(result.length).toBe(11);
});
test("Pac-Man rolls four from position [5,1] (left), should return 12", () => {
pacMan.follow({end: {x: 5, y: 1}, direction: Direction.left});
const result = possibleMovesAlgorithm(testMap, pacMan, 4);
const result = possibleMovesAlgorithm(testMap, pacMan, 4, []);
expect(result.length).toBe(12);
});
test("Pac-Man rolls three from position [1,5] (left), should return 5", () => {
pacMan.follow({end: {x: 1, y: 5}, direction: Direction.left});
const result = possibleMovesAlgorithm(testMap, pacMan, 3);
const result = possibleMovesAlgorithm(testMap, pacMan, 3, []);
arrayEquals(result, [
{end: {x: 1, y: 2}, direction: Direction.up, path: [{x: 1, y: 4}, {x: 1, y: 3}]},
{end: {x: 1, y: 8}, direction: Direction.down, path: [{x: 1, y: 6}, {x: 1, y: 7}]},
@ -129,19 +129,19 @@ test("Pac-Man rolls three from position [1,5] (left), should return 5", () => {
test("Pac-Man rolls six from position [1,5] (down), should return 17", () => {
pacMan.follow({end: {x: 1, y: 5}, direction: Direction.down});
const result = possibleMovesAlgorithm(testMap, pacMan, 6);
const result = possibleMovesAlgorithm(testMap, pacMan, 6, []);
expect(result.length).toBe(17);
});
test("Pac-Man rolls six from position [7,1] (right), path to [9,5] should be five tiles long", () => {
pacMan.follow({end: {x: 7, y: 1}, direction: Direction.right});
const result = possibleMovesAlgorithm(testMap, pacMan, 6);
const result = possibleMovesAlgorithm(testMap, pacMan, 6, []);
expect(result[0].path?.length).toBe(5);
});
test("Pac-Man rolls 5 from position [9,3] (down), should return 5", () => {
pacMan.follow({end: {x: 9, y: 3}, direction: Direction.down});
const result = possibleMovesAlgorithm(testMap, pacMan, 5);
const result = possibleMovesAlgorithm(testMap, pacMan, 5, []);
expect(result.length).toBe(5);
});