Skip to content
Snippets Groups Projects
Commit ad60b533 authored by ys678's avatar ys678
Browse files

Update game.js

parent 20d2b7a6
No related branches found
No related tags found
No related merge requests found
......@@ -58,11 +58,21 @@ function addFood(gameBoard) {
randomCell.type = cellTypes.BANANA;
}
// Add ENERGY if it doesn't already exist
let energyExists = gameBoard.some((row) =>
row.some((cell) => cell.type === cellTypes.ENERGY),
);
if (!energyExists && emptySpaces.length > 0) {
return gameBoard;
}
function addEnergyCell(gameBoard) {
let emptySpaces = [];
for (let row of gameBoard) {
for (let cell of row) {
if (cell.type === cellTypes.EMPTY) {
emptySpaces.push(cell);
}
}
}
if (emptySpaces.length > 0) {
let randomCell = emptySpaces[getRandomInt(emptySpaces.length)];
randomCell.type = cellTypes.ENERGY;
}
......@@ -96,9 +106,9 @@ function checkCollisions(newY, newX, gameBoard) {
console.log("Slip");
return COLLISIONTYPES.BANANA;
}
//Handle collision with energy
if (gameBoard[newY][newX].type == cellTypes.ENERGY) {
console.log("Player has collected ENERGY!");
if (gameBoard[newY][newX].type === cellTypes.ENERGY) {
console.log("Player has eaten the energy cell!");
return COLLISIONTYPES.ENERGY;
}
......@@ -195,17 +205,11 @@ module.exports = {
let collision = checkCollisions(newY, newX, gameBoard);
if (collision === COLLISIONTYPES.ENERGY) {
let playerId = cell.pid;
console.log(`Player ${playerId} gained speed boost!`);
// Double speed for 5 seconds
if (!speedBoostTimers[playerId]) {
speedBoostTimers[playerId] = setTimeout(() => {
clearTimeout(speedBoostTimers[playerId]);
speedBoostTimers[playerId] = null;
console.log(`Player ${playerId}'s speed boost ended.`);
}, 5000); // Boost lasts 5 seconds
}
let player = this.games[game].players.find(p => p.pid === cell.pid);
player.speed = 250; // Double speed
setTimeout(() => {
player.speed = 500; // Reset speed after 5 seconds
}, 5000);
}
if (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment