Teleporation works !

This commit is contained in:
Pierre HUBERT 2020-04-01 13:25:21 +02:00
parent 42c8da4578
commit 178cbd2492

View File

@ -216,7 +216,7 @@ async function startGame(gameID) {
if(level.hasOwnProperty("teleportation")) if(level.hasOwnProperty("teleportation"))
level.teleportation.forEach((f) => { level.teleportation.forEach((f) => {
map[f[0]-1][f[1]-1] = TELEPORTATION map[f[0]-1][f[1]-1] = TELEPORTATION
teleportationCells.push(JSON.stringify([f[0]-1,f[1]-1])); teleportationCells.push([f[0]-1,f[1]-1]);
}) })
@ -283,6 +283,15 @@ async function startGame(gameID) {
score++; score++;
break; break;
case TELEPORTATION:
// Teleport the snake to the other cell
const possibleDestinations = teleportationCells.filter((v) => v[0] != newHead[0] && v[1] != newHead[1]);
const chosenDestination = possibleDestinations[randInt(0, possibleDestinations.length)];
newHead[0] = chosenDestination[0]
newHead[1] = chosenDestination[1]
break;
case ICE_SNAKE: case ICE_SNAKE:
case SNAKE: case SNAKE:
case WALL: case WALL:
@ -301,7 +310,7 @@ async function startGame(gameID) {
map[oldPos[0]][oldPos[1]] = map[oldPos[0]][oldPos[1]] == ICE_SNAKE ? ICE : EMPTY map[oldPos[0]][oldPos[1]] = map[oldPos[0]][oldPos[1]] == ICE_SNAKE ? ICE : EMPTY
// If the cell was a teleportation cell, then revert it back to the right state // If the cell was a teleportation cell, then revert it back to the right state
if(teleportationCells.includes(JSON.stringify(oldPos))) if(teleportationCells.find((e) => e[0] === oldPos[0] && e[1] === oldPos[1]) != undefined)
map[oldPos[0]][oldPos[1]] = TELEPORTATION map[oldPos[0]][oldPos[1]] = TELEPORTATION
} }