first version with up movement working
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { initializeGame, updateGame } from './game.js';
|
||||
import { initializeGame, updateGame } from './game/game.js';
|
||||
|
||||
let id; // the div in the HTML
|
||||
|
||||
|
||||
@@ -2,52 +2,37 @@
|
||||
let player;
|
||||
let platform;
|
||||
let platforms;
|
||||
let cameraOffset = 0;
|
||||
|
||||
import { updatePlayerPosition, updateCamera } from '../gameUtils.js';
|
||||
|
||||
export function initializeGame() {
|
||||
world.gravity.y = 10;
|
||||
import { updatePlayerPosition } from './gameUtils.js';
|
||||
import { initPlatforms, updatePlatforms } from './platforms.js';
|
||||
|
||||
function createPlayer() {
|
||||
player = new Sprite();
|
||||
player.diameter = 100;
|
||||
player.scale = 0.3;
|
||||
player.x = width / 2;
|
||||
player.y = 500;
|
||||
player.y = 700;
|
||||
player.bounciness = 0;
|
||||
player.elevation = 0;
|
||||
return player;
|
||||
}
|
||||
|
||||
// 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 initializeGame() {
|
||||
world.debug = true; //TODO remove
|
||||
world.gravity.y = 10;
|
||||
platforms = initPlatforms();
|
||||
player = createPlayer();
|
||||
}
|
||||
|
||||
export function updateGame() {
|
||||
clear();
|
||||
background(200);
|
||||
|
||||
const elGain = updatePlayerPosition(player, platforms);
|
||||
const elevationGain = updatePlayerPosition(player, platforms);
|
||||
if (elevationGain > 0) {
|
||||
//debugger;
|
||||
}
|
||||
platforms = updatePlatforms(platforms, player.elevation, elevationGain);
|
||||
|
||||
|
||||
// UI
|
||||
|
||||
@@ -41,7 +41,7 @@ export function updatePlayerPosition(player, platforms) {
|
||||
} else if (player.x < 0) {
|
||||
player.x = width;
|
||||
}
|
||||
|
||||
|
||||
return elevationGain;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
|
||||
const SPACE_BETWEEN_PLATFORMS = 80;
|
||||
|
||||
function createPlatform(x, y, elevation, width = 60) {
|
||||
const platform = new Sprite();
|
||||
platform.x = x;
|
||||
platform.y = y;
|
||||
platform.elevation = elevation;
|
||||
platform.w = width;
|
||||
platform.h = 15;
|
||||
platform.physics = 'static';
|
||||
platform.bounciness = 0;
|
||||
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;
|
||||
while (spawnY >= SPACE_BETWEEN_PLATFORMS) {
|
||||
const newPlat = createPlatform(random(40, width - 40), spawnY, elevation + minY - spawnY);
|
||||
platforms.add(newPlat);
|
||||
spawnY -= SPACE_BETWEEN_PLATFORMS;
|
||||
}
|
||||
return platforms;
|
||||
}
|
||||
|
||||
|
||||
export function initPlatforms() {
|
||||
let platforms = new Group();
|
||||
updatePlatforms(platforms, 10, 10);
|
||||
|
||||
const basePlatform = createPlatform(width / 2, height - 10, 0, width);
|
||||
platforms.add(basePlatform);
|
||||
return platforms;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user