diff --git a/src/game/game.js b/src/game/game.js index aba895c..2a1832b 100644 --- a/src/game/game.js +++ b/src/game/game.js @@ -2,9 +2,10 @@ let player; let platform; let platforms; +let pendingWorldMove = 0; import { updatePlayerPosition } from './gameUtils.js'; -import { initPlatforms, updatePlatforms } from './platforms.js'; +import { initPlatforms, moveWorld, PLATFROMS_GAP } from './platforms.js'; function createPlayer() { player = new Sprite(); @@ -28,10 +29,13 @@ export function updateGame() { clear(); background(200); - const elevationGain = updatePlayerPosition(player, platforms); - if (elevationGain > 0) { - platforms = updatePlatforms(platforms, player.elevation, elevationGain); - player.y += elevationGain; + pendingWorldMove += updatePlayerPosition(player, platforms); + if (pendingWorldMove > PLATFROMS_GAP) { + const move = Math.min(pendingWorldMove, 5); + pendingWorldMove -= move; + platforms = moveWorld(platforms, player.elevation, move); + player.y += move; + player.bounciness = 0; // needed becouse bug in p5.play that rests bounciness } diff --git a/src/game/platforms.js b/src/game/platforms.js index a4099c5..f691908 100644 --- a/src/game/platforms.js +++ b/src/game/platforms.js @@ -1,5 +1,5 @@ -const SPACE_BETWEEN_PLATFORMS = 80; +export const PLATFROMS_GAP = 80; const PLATFORM_COLORS = [ '#c44545', '#c98a1f', @@ -37,41 +37,42 @@ function createPlatform(x, y, elevation, width = 70) { return platform; } -function generatePlatforms(platforms, currentLowestPlat) { - let spawnY = currentLowestPlat.y - SPACE_BETWEEN_PLATFORMS; +function generateNewPlatforms(platforms, currentHighestPlat) { + let spawnY = currentHighestPlat.y - PLATFROMS_GAP; let i = 0; - while (spawnY >= SPACE_BETWEEN_PLATFORMS) { + while (spawnY > 0) { i++; const newPlat = createPlatform( random(40, width - 40), spawnY, - currentLowestPlat.elevation + i * SPACE_BETWEEN_PLATFORMS + currentHighestPlat.elevation + i * PLATFROMS_GAP ); platforms.add(newPlat); - spawnY -= SPACE_BETWEEN_PLATFORMS; + spawnY -= PLATFROMS_GAP; } + platforms.bounciness = 0; // needed becouse bug in p5.play that rests bounciness return platforms; } -export function updatePlatforms(platforms, elevation, elGain) { - if (elGain <= 0) - return platforms; - - let lowestPlat = platforms[0]; +export function moveWorld(platforms, elevation, move) { + let highestPlat = null; for (let i = platforms.length - 1; i >= 0; i--) { const plat = platforms[i]; - plat.y += elGain; + plat.y += move; if (plat.y >= height) { - platforms.remove(plat); + plat.remove(); continue; } - if (plat.y < lowestPlat.y) { - lowestPlat = plat; + if (!highestPlat || plat.y < highestPlat.y) { + highestPlat = plat; } } - return generatePlatforms(platforms, lowestPlat); + if (highestPlat.y > 0) { + platforms = generateNewPlatforms(platforms, highestPlat); + } + return platforms; } @@ -80,6 +81,6 @@ export function initPlatforms() { const basePlatform = createPlatform(width / 2, height - 10, 0, width); platforms.add(basePlatform); - generatePlatforms(platforms, basePlatform); + generateNewPlatforms(platforms, basePlatform); return platforms; }