From 178cbd249291978535f2ca16eac367c456df96bf Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Wed, 1 Apr 2020 13:25:21 +0200 Subject: [PATCH] Teleporation works ! --- script.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index 420c860..48208fc 100644 --- a/script.js +++ b/script.js @@ -216,7 +216,7 @@ async function startGame(gameID) { if(level.hasOwnProperty("teleportation")) level.teleportation.forEach((f) => { 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++; 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 SNAKE: case WALL: @@ -301,7 +310,7 @@ async function startGame(gameID) { 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(teleportationCells.includes(JSON.stringify(oldPos))) + if(teleportationCells.find((e) => e[0] === oldPos[0] && e[1] === oldPos[1]) != undefined) map[oldPos[0]][oldPos[1]] = TELEPORTATION }