Select Git revision
game.js 3.84 KiB
import {Entity, Room} from "../../../hampsterengine/src/things";
import Player from "../objects/player";
import {clone, clonePlayer, abs, roundToRatio} from "../extras";
import Ground from "../objects/ground";
export const rm_game = new Room();
const GRAVITY_X = 0; // I don't think we're going to use X gravity but i'm going to keep in the source in case i do
const GRAVITY_Y = 600; // Per second
const entities = rm_game.entities;
rm_game.width = 2560;
rm_game.height = 500;
rm_game.start = _=>{
canvas.setScale(2);
engine.running = true;
}
rm_game.stop = _=>{
engine.running = false;
}
rm_game.step = _=>{
canvas.camera.step();
const elapsed = 1 / 60;
const player = rm_game.get('plr');
let friction = 0.95;
const boost = keyboard.keys.includes("Shift") ? 40 : 0;
for (const key of keyboard.keys) {
switch (key) {
case "ArrowLeft":
player.vx = -50-boost;
break;
case "ArrowRight":
player.vx = 50+boost;
break;
case " ":
if (!player.jumping) {
player.jumping = true;
player.vy -= 150;
}
}
}
const entitiesWithoutThePlayer = [...entities].toSpliced(entities.indexOf(player), 1);
// console.debug(entitiesWithoutThePlayer);
player.vx = Math.min(400, player.vx + (player.ax * elapsed + GRAVITY_X*elapsed));
player.vy = Math.min(400, player.vy + (player.ay * elapsed + GRAVITY_Y*elapsed));
player.x += player.vx * elapsed;
player.y += player.vy * elapsed;
// Make acceleration decay
player.ax *= 0.1;
player.ay *= 0.1;
if (abs(player.ax) < 0.01) player.ax = 0;
if (abs(player.ay) < 0.01) player.ay = 0;
rm_game.x = player.x;
rm_game.y = player.y;
for (const entity of entitiesWithoutThePlayer) {
if (player.checkCollision(entity)) {
friction = 0.8;
let side = player.resolveCollision(entity);