first version with up movement working
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { initializeGame, updateGame } from './game.js';
|
import { initializeGame, updateGame } from './game/game.js';
|
||||||
|
|
||||||
let id; // the div in the HTML
|
let id; // the div in the HTML
|
||||||
|
|
||||||
|
|||||||
@@ -2,52 +2,37 @@
|
|||||||
let player;
|
let player;
|
||||||
let platform;
|
let platform;
|
||||||
let platforms;
|
let platforms;
|
||||||
let cameraOffset = 0;
|
|
||||||
|
|
||||||
import { updatePlayerPosition, updateCamera } from '../gameUtils.js';
|
import { updatePlayerPosition } from './gameUtils.js';
|
||||||
|
import { initPlatforms, updatePlatforms } from './platforms.js';
|
||||||
export function initializeGame() {
|
|
||||||
world.gravity.y = 10;
|
|
||||||
|
|
||||||
|
function createPlayer() {
|
||||||
player = new Sprite();
|
player = new Sprite();
|
||||||
player.diameter = 100;
|
player.diameter = 100;
|
||||||
player.scale = 0.3;
|
player.scale = 0.3;
|
||||||
player.x = width / 2;
|
player.x = width / 2;
|
||||||
player.y = 500;
|
player.y = 700;
|
||||||
player.bounciness = 0;
|
player.bounciness = 0;
|
||||||
player.elevation = 0;
|
player.elevation = 0;
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
|
||||||
// Create platform at the bottom
|
export function initializeGame() {
|
||||||
platform = new Sprite();
|
world.debug = true; //TODO remove
|
||||||
platform.x = width / 2;
|
world.gravity.y = 10;
|
||||||
platform.y = height - 10;
|
platforms = initPlatforms();
|
||||||
platform.w = width;
|
player = createPlayer();
|
||||||
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() {
|
export function updateGame() {
|
||||||
clear();
|
clear();
|
||||||
background(200);
|
background(200);
|
||||||
|
|
||||||
const elGain = updatePlayerPosition(player, platforms);
|
const elevationGain = updatePlayerPosition(player, platforms);
|
||||||
|
if (elevationGain > 0) {
|
||||||
|
//debugger;
|
||||||
|
}
|
||||||
|
platforms = updatePlatforms(platforms, player.elevation, elevationGain);
|
||||||
|
|
||||||
|
|
||||||
// UI
|
// UI
|
||||||
|
|||||||
@@ -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