upgraded movement and platforms generation
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -30,9 +30,10 @@ export function updateGame() {
|
||||
|
||||
const elevationGain = updatePlayerPosition(player, platforms);
|
||||
if (elevationGain > 0) {
|
||||
//debugger;
|
||||
platforms = updatePlatforms(platforms, player.elevation, elevationGain);
|
||||
player.y += elevationGain;
|
||||
}
|
||||
platforms = updatePlatforms(platforms, player.elevation, elevationGain);
|
||||
|
||||
|
||||
|
||||
// UI
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
// Game utilities for player controls and movement
|
||||
|
||||
|
||||
export function updatePlayerPosition(player, platforms) {
|
||||
let elevationGain = 0;
|
||||
|
||||
// Controls
|
||||
player.vel.x = 0;
|
||||
if (keyIsDown(LEFT_ARROW)) {
|
||||
@@ -13,6 +10,13 @@ export function updatePlayerPosition(player, platforms) {
|
||||
player.vel.x = 5;
|
||||
}
|
||||
|
||||
// Wrap horizontally around canvas edges
|
||||
if (player.x > width) {
|
||||
player.x = 0;
|
||||
} else if (player.x < 0) {
|
||||
player.x = width;
|
||||
}
|
||||
|
||||
// Jumping thrue platforms
|
||||
for (let plat of platforms) {
|
||||
if (player.vel.y <= 0 && player.y > plat.y) {
|
||||
@@ -22,26 +26,20 @@ export function updatePlayerPosition(player, platforms) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Automatic jumping
|
||||
if (player.vel.y >= 0) {
|
||||
player.collides(platforms, (player, platform) => {
|
||||
player.vel.y = -7.5;
|
||||
|
||||
if (player.elevation < platform.elevation) {
|
||||
elevationGain = platform.elevation - player.elevation;
|
||||
player.elevation = platform.elevation;
|
||||
if(player.vel.y >= 0) {
|
||||
for (let plat of platforms) {
|
||||
if (player.colliding(plat)) {
|
||||
player.vel.y = -7.5;
|
||||
|
||||
if (player.elevation < plat.elevation) {
|
||||
elevationGain = plat.elevation - player.elevation;
|
||||
player.elevation = plat.elevation;
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Wrap horizontally around canvas edges
|
||||
if (player.x > width) {
|
||||
player.x = 0;
|
||||
} else if (player.x < 0) {
|
||||
player.x = width;
|
||||
}
|
||||
|
||||
|
||||
return elevationGain;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,40 +1,52 @@
|
||||
|
||||
const SPACE_BETWEEN_PLATFORMS = 80;
|
||||
const PLATFORM_COLORS = [
|
||||
'#c44545',
|
||||
'#c98a1f',
|
||||
'#1f8f86',
|
||||
'#2c3e5e',
|
||||
'#9c2f2f',
|
||||
'#b36f14'
|
||||
];
|
||||
|
||||
function createPlatform(x, y, elevation, width = 60) {
|
||||
function createPlatform(x, y, elevation, width = 70) {
|
||||
const platform = new Sprite();
|
||||
platform.x = x;
|
||||
platform.y = y;
|
||||
platform.elevation = elevation;
|
||||
platform.w = width;
|
||||
platform.h = 15;
|
||||
platform.physics = 'static';
|
||||
platform.collider = 'static';
|
||||
platform.bounciness = 0;
|
||||
platform.color = random(PLATFORM_COLORS);
|
||||
platform.draw = function () {
|
||||
fill(this.color);
|
||||
stroke(0);
|
||||
strokeWeight(1);
|
||||
rect(0, 0, this.w, this.h, 15);
|
||||
};
|
||||
/* platform debug TODO remove
|
||||
platform.draw = function() {
|
||||
fill(200);
|
||||
rect(0, 0, this.w, this.h);
|
||||
fill(0);
|
||||
textAlign(CENTER, CENTER);
|
||||
text(this.elevation, this.w / 2, this.h / 2);
|
||||
};
|
||||
*/
|
||||
return platform;
|
||||
}
|
||||
|
||||
|
||||
export function updatePlatforms(platforms, elevation, elGain) {
|
||||
if (elGain <= 0)
|
||||
return platforms;
|
||||
|
||||
let minY = height;
|
||||
for (let i = platforms.length - 1; i >= 0; i--) {
|
||||
const plat = platforms[i];
|
||||
plat.y += elGain;
|
||||
if (plat.y > height) {
|
||||
platforms.remove(plat);
|
||||
continue;
|
||||
}
|
||||
if (plat.y < minY) {
|
||||
minY = plat.y;
|
||||
}
|
||||
}
|
||||
|
||||
// Add new platforms at the top
|
||||
let spawnY = minY - SPACE_BETWEEN_PLATFORMS;
|
||||
function generatePlatforms(platforms, currentLowestPlat) {
|
||||
let spawnY = currentLowestPlat.y - SPACE_BETWEEN_PLATFORMS;
|
||||
let i = 0;
|
||||
while (spawnY >= SPACE_BETWEEN_PLATFORMS) {
|
||||
const newPlat = createPlatform(random(40, width - 40), spawnY, elevation + minY - spawnY);
|
||||
i++;
|
||||
const newPlat = createPlatform(
|
||||
random(40, width - 40),
|
||||
spawnY,
|
||||
currentLowestPlat.elevation + i * SPACE_BETWEEN_PLATFORMS
|
||||
);
|
||||
platforms.add(newPlat);
|
||||
spawnY -= SPACE_BETWEEN_PLATFORMS;
|
||||
}
|
||||
@@ -42,11 +54,32 @@ export function updatePlatforms(platforms, elevation, elGain) {
|
||||
}
|
||||
|
||||
|
||||
export function updatePlatforms(platforms, elevation, elGain) {
|
||||
if (elGain <= 0)
|
||||
return platforms;
|
||||
|
||||
let lowestPlat = platforms[0];
|
||||
for (let i = platforms.length - 1; i >= 0; i--) {
|
||||
const plat = platforms[i];
|
||||
plat.y += elGain;
|
||||
if (plat.y >= height) {
|
||||
platforms.remove(plat);
|
||||
continue;
|
||||
}
|
||||
if (plat.y < lowestPlat.y) {
|
||||
lowestPlat = plat;
|
||||
}
|
||||
}
|
||||
|
||||
return generatePlatforms(platforms, lowestPlat);
|
||||
}
|
||||
|
||||
|
||||
export function initPlatforms() {
|
||||
let platforms = new Group();
|
||||
updatePlatforms(platforms, 10, 10);
|
||||
|
||||
const basePlatform = createPlatform(width / 2, height - 10, 0, width);
|
||||
platforms.add(basePlatform);
|
||||
|
||||
generatePlatforms(platforms, basePlatform);
|
||||
return platforms;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user