fixed bouncing error

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-27 18:01:28 +09:00
parent d79726cae4
commit 81a5d890ec
2 changed files with 27 additions and 22 deletions

View File

@@ -2,9 +2,10 @@
let player; let player;
let platform; let platform;
let platforms; let platforms;
let pendingWorldMove = 0;
import { updatePlayerPosition } from './gameUtils.js'; import { updatePlayerPosition } from './gameUtils.js';
import { initPlatforms, updatePlatforms } from './platforms.js'; import { initPlatforms, moveWorld, PLATFROMS_GAP } from './platforms.js';
function createPlayer() { function createPlayer() {
player = new Sprite(); player = new Sprite();
@@ -28,10 +29,13 @@ export function updateGame() {
clear(); clear();
background(200); background(200);
const elevationGain = updatePlayerPosition(player, platforms); pendingWorldMove += updatePlayerPosition(player, platforms);
if (elevationGain > 0) { if (pendingWorldMove > PLATFROMS_GAP) {
platforms = updatePlatforms(platforms, player.elevation, elevationGain); const move = Math.min(pendingWorldMove, 5);
player.y += elevationGain; pendingWorldMove -= move;
platforms = moveWorld(platforms, player.elevation, move);
player.y += move;
player.bounciness = 0; // needed becouse bug in p5.play that rests bounciness
} }

View File

@@ -1,5 +1,5 @@
const SPACE_BETWEEN_PLATFORMS = 80; export const PLATFROMS_GAP = 80;
const PLATFORM_COLORS = [ const PLATFORM_COLORS = [
'#c44545', '#c44545',
'#c98a1f', '#c98a1f',
@@ -37,41 +37,42 @@ function createPlatform(x, y, elevation, width = 70) {
return platform; return platform;
} }
function generatePlatforms(platforms, currentLowestPlat) { function generateNewPlatforms(platforms, currentHighestPlat) {
let spawnY = currentLowestPlat.y - SPACE_BETWEEN_PLATFORMS; let spawnY = currentHighestPlat.y - PLATFROMS_GAP;
let i = 0; let i = 0;
while (spawnY >= SPACE_BETWEEN_PLATFORMS) { while (spawnY > 0) {
i++; i++;
const newPlat = createPlatform( const newPlat = createPlatform(
random(40, width - 40), random(40, width - 40),
spawnY, spawnY,
currentLowestPlat.elevation + i * SPACE_BETWEEN_PLATFORMS currentHighestPlat.elevation + i * PLATFROMS_GAP
); );
platforms.add(newPlat); platforms.add(newPlat);
spawnY -= SPACE_BETWEEN_PLATFORMS; spawnY -= PLATFROMS_GAP;
} }
platforms.bounciness = 0; // needed becouse bug in p5.play that rests bounciness
return platforms; return platforms;
} }
export function updatePlatforms(platforms, elevation, elGain) { export function moveWorld(platforms, elevation, move) {
if (elGain <= 0) let highestPlat = null;
return platforms;
let lowestPlat = platforms[0];
for (let i = platforms.length - 1; i >= 0; i--) { for (let i = platforms.length - 1; i >= 0; i--) {
const plat = platforms[i]; const plat = platforms[i];
plat.y += elGain; plat.y += move;
if (plat.y >= height) { if (plat.y >= height) {
platforms.remove(plat); plat.remove();
continue; continue;
} }
if (plat.y < lowestPlat.y) { if (!highestPlat || plat.y < highestPlat.y) {
lowestPlat = plat; 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); const basePlatform = createPlatform(width / 2, height - 10, 0, width);
platforms.add(basePlatform); platforms.add(basePlatform);
generatePlatforms(platforms, basePlatform); generateNewPlatforms(platforms, basePlatform);
return platforms; return platforms;
} }