From 5620f1860febea48a7fea3323a2658c17f59fed0 Mon Sep 17 00:00:00 2001 From: Tomas Horsky Date: Sat, 25 Apr 2026 14:23:45 +0900 Subject: [PATCH] basic version without world movement Co-authored-by: Copilot --- src/App.svelte | 24 ++++++------------ src/game/game.js | 59 +++++++++++++++++++++++++++++++++++++++++++ src/game/gameUtils.js | 47 ++++++++++++++++++++++++++++++++++ src/game/platforms.js | 0 4 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 src/game/game.js create mode 100644 src/game/gameUtils.js create mode 100644 src/game/platforms.js diff --git a/src/App.svelte b/src/App.svelte index 54c82a2..8de3049 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -1,29 +1,21 @@ diff --git a/src/game/game.js b/src/game/game.js new file mode 100644 index 0000000..908edd0 --- /dev/null +++ b/src/game/game.js @@ -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); +} + diff --git a/src/game/gameUtils.js b/src/game/gameUtils.js new file mode 100644 index 0000000..bdf9d94 --- /dev/null +++ b/src/game/gameUtils.js @@ -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; +} + diff --git a/src/game/platforms.js b/src/game/platforms.js new file mode 100644 index 0000000..e69de29