add platform types

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-28 22:25:20 +09:00
parent 81a5d890ec
commit 7a0bca9938
7 changed files with 201 additions and 60 deletions

View File

@@ -1,6 +1,31 @@
import { PLAT_TYPE } from './constants.js';
function handleJump(player, platform) {
let elevationGain = 0;
if (player.elevation < platform.elevation) {
elevationGain = platform.elevation - player.elevation;
player.elevation = platform.elevation;
}
switch (platform.type) {
case PLAT_TYPE.ONE_TIME:
player.vel.y = -7.5;
platform.remove();
break;
case PLAT_TYPE.SPRING:
player.vel.y = -13;
break;
default:
player.vel.y = -7.5;
}
return elevationGain;
}
export function updatePlayerPosition(player, platforms) {
let elevationGain = 0;
// Controls
player.vel.x = 0;
if (keyIsDown(LEFT_ARROW)) {
@@ -10,7 +35,7 @@ export function updatePlayerPosition(player, platforms) {
player.vel.x = 5;
}
// Wrap horizontally around canvas edges
// Wrap horizontally
if (player.x > width) {
player.x = 0;
} else if (player.x < 0) {
@@ -19,7 +44,7 @@ export function updatePlayerPosition(player, platforms) {
// Jumping thrue platforms
for (let plat of platforms) {
if (player.vel.y <= 0 && player.y > plat.y) {
if (player.vel.y <= 0 && player.y + player.offset.y > plat.y) {
plat.collider = 'none';
} else {
plat.collider = 'static';
@@ -27,19 +52,14 @@ export function updatePlayerPosition(player, platforms) {
}
// 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;
if (player.vel.y >= 0) {
for (let i = 0; i < platforms.length; i++) {
if (player.colliding(platforms[i])) {
return handleJump(player, platforms[i]);
}
}
}
return elevationGain;
return 0;
}