A realtime multiplayer version of the boardgame Ricochet Robots
at master 3.6 kB view raw
1import type { MoveDirection } from "./game"; 2import type { GridType, N2, Rocket, Rockets } from "./grid"; 3 4export const getMovementSquaresUp = ( 5 board: GridType, 6 rockets: Rockets, 7 position: N2, 8): N2[] => { 9 const movementSquares: N2[] = []; 10 for (let y = position.y - 1; y >= 0; y--) { 11 if ( 12 y !== position.y && 13 y < board.length - 1 && 14 board[y + 1]![position.x]!.wallY 15 ) { 16 break; 17 } 18 19 if (Object.values(rockets).find((p) => p.x === position.x && p.y === y)) 20 break; 21 22 movementSquares.push({ 23 x: position.x, 24 y, 25 }); 26 } 27 28 return movementSquares; 29}; 30 31export const getMovementSquaresDown = ( 32 board: GridType, 33 rockets: Rockets, 34 position: N2, 35): N2[] => { 36 const movementSquares: N2[] = []; 37 for (let y = position.y + 1; y < board.length; y++) { 38 if (y !== position.y && board[y]![position.x]!.wallY) { 39 break; 40 } 41 42 if (Object.values(rockets).find((p) => p.x === position.x && p.y === y)) 43 break; 44 45 movementSquares.push({ 46 x: position.x, 47 y, 48 }); 49 } 50 51 return movementSquares; 52}; 53 54export const getMovementSquaresLeft = ( 55 board: GridType, 56 rockets: Rockets, 57 position: N2, 58): N2[] => { 59 const movementSquares: N2[] = []; 60 for (let x = position.x - 1; x >= 0; x--) { 61 if ( 62 x !== position.x && 63 x < board[0]!.length - 1 && 64 board[position.y]![x + 1]!.wallX 65 ) { 66 break; 67 } 68 69 if (Object.values(rockets).find((p) => p.x === x && p.y === position.y)) 70 break; 71 72 movementSquares.push({ 73 x, 74 y: position.y, 75 }); 76 } 77 78 return movementSquares; 79}; 80 81export const getMovementSquaresRight = ( 82 board: GridType, 83 rockets: Rockets, 84 position: N2, 85): N2[] => { 86 const movementSquares: N2[] = []; 87 for (let x = position.x + 1; x < board.length; x++) { 88 if (x !== position.x && board[position.y]![x]!.wallX) { 89 break; 90 } 91 92 if (Object.values(rockets).find((p) => p.x === x && p.y === position.y)) 93 break; 94 95 movementSquares.push({ 96 x, 97 y: position.y, 98 }); 99 } 100 101 return movementSquares; 102}; 103 104export const getCompletedMove = ( 105 board: GridType, 106 rockets: Rockets, 107 rocket: Rocket, 108 direction: MoveDirection, 109) => { 110 let movedTo: N2; 111 112 let squares: N2[] = []; 113 114 console.log(rocket, direction, rockets); 115 116 switch (direction) { 117 case "up": 118 squares = getMovementSquaresUp(board, rockets, rockets[rocket]); 119 console.log("Squares", squares); 120 movedTo = squares[squares.length - 1]!; 121 break; 122 case "down": 123 squares = getMovementSquaresDown(board, rockets, rockets[rocket]); 124 console.log("Squares", squares); 125 movedTo = squares[squares.length - 1]!; 126 break; 127 case "left": 128 squares = getMovementSquaresLeft(board, rockets, rockets[rocket]); 129 console.log("Squares", squares); 130 movedTo = squares[squares.length - 1]!; 131 break; 132 case "right": 133 squares = getMovementSquaresRight(board, rockets, rockets[rocket]); 134 console.log("Squares", squares); 135 movedTo = squares[squares.length - 1]!; 136 break; 137 } 138 return movedTo; 139}; 140 141export const calculateMovementSquares = ( 142 board: GridType, 143 rockets: Rockets, 144 position: N2, 145): N2[] => { 146 const movementSquares: N2[] = []; 147 148 movementSquares.push(...getMovementSquaresUp(board, rockets, position)); 149 movementSquares.push(...getMovementSquaresDown(board, rockets, position)); 150 movementSquares.push(...getMovementSquaresLeft(board, rockets, position)); 151 movementSquares.push(...getMovementSquaresRight(board, rockets, position)); 152 153 return movementSquares; 154};