basic version without world movement

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-25 14:23:45 +09:00
parent 3b0d5b4864
commit 5620f1860f
4 changed files with 114 additions and 16 deletions

59
src/game/game.js Normal file
View File

@@ -0,0 +1,59 @@
// Game state
let player;
let platform;
let platforms;
let cameraOffset = 0;
import { updatePlayerPosition, updateCamera } from '../gameUtils.js';
export function initializeGame() {
world.gravity.y = 10;
player = new Sprite();
player.diameter = 100;
player.scale = 0.3;
player.x = width / 2;
player.y = 500;
player.bounciness = 0;
player.elevation = 0;
// Create platform at the bottom
platform = new Sprite();
platform.x = width / 2;
platform.y = height - 10;
platform.w = width;
platform.h = 20;
platform.collider = 'static';
platform.elevation = 0;
platforms = new Group();
platforms.bounciness = 0;
platforms.add(platform);
for (let i = 1; i < 10; i++) {
let p = new Sprite();
p.x = (80 * i) % width + 30;
p.y = height - 80 * i;
p.elevation = 80 * i;
p.w = 100;
p.h = 20;
p.collider = 'static';
p.bounciness = 0;
platforms.add(p);
}
}
export function updateGame() {
clear();
background(200);
const elGain = updatePlayerPosition(player, platforms);
// UI
fill(0);
textSize(24);
textAlign(RIGHT);
text(`${Math.floor(player.elevation)}`, width - 20, 30);
}

47
src/game/gameUtils.js Normal file
View File

@@ -0,0 +1,47 @@
// Game utilities for player controls and movement
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;
}
// 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) {
player.collides(platforms, (player, platform) => {
player.vel.y = -7.5;
if (player.elevation < platform.elevation) {
elevationGain = platform.elevation - player.elevation;
player.elevation = platform.elevation;
}
});
}
// Wrap horizontally around canvas edges
if (player.x > width) {
player.x = 0;
} else if (player.x < 0) {
player.x = width;
}
return elevationGain;
}

0
src/game/platforms.js Normal file
View File