46 lines
959 B
JavaScript
46 lines
959 B
JavaScript
|
|
export function updatePlayerPosition(player, platforms) {
|
|
let elevationGain = 0;
|
|
// Controls
|
|
player.vel.x = 0;
|
|
if (keyIsDown(LEFT_ARROW)) {
|
|
player.vel.x = -5;
|
|
}
|
|
if (keyIsDown(RIGHT_ARROW)) {
|
|
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) {
|
|
plat.collider = 'none';
|
|
} else {
|
|
plat.collider = 'static';
|
|
}
|
|
}
|
|
|
|
// Automatic jumping
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
return elevationGain;
|
|
}
|