From d7371f9eeb43accdda073914a5f901b6a7f2de55 Mon Sep 17 00:00:00 2001
From: bye <bye@byecorps.com>
Date: Sun, 1 Sep 2024 12:01:51 +0100
Subject: [PATCH] WOAH

---
 hampsterengine           |  2 +-
 src/js/main.js           | 14 ++++-----
 src/js/objects/ground.js |  2 +-
 src/js/objects/player.js |  2 ++
 src/js/rooms/game.js     | 63 +++++++++++++++++++++++++---------------
 5 files changed, 51 insertions(+), 32 deletions(-)

diff --git a/hampsterengine b/hampsterengine
index 04ed5f0..dd760ba 160000
--- a/hampsterengine
+++ b/hampsterengine
@@ -1 +1 @@
-Subproject commit 04ed5f0a25ca36462ce82704d2312881ba099c79
+Subproject commit dd760ba404d7b679b0fb46aa0abaaa05f40b329c
diff --git a/src/js/main.js b/src/js/main.js
index 6a16d54..ededfba 100644
--- a/src/js/main.js
+++ b/src/js/main.js
@@ -14,7 +14,7 @@ import SoundBox from "./sb-player-small";
 // import {mus_DEMOSONG} from "./songs/DEMOSONGDONOTCOMMIT";
 
 // dependencies used for debugging. comment out code that uses this and they won't be included
-import Stats from "stats.js";
+// import Stats from "stats.js";
 
 // Images
 import font from "../img/font.webp";
@@ -98,9 +98,9 @@ engine.registerRoom(rm_DEBUG_stars, 'debug_stars');
 engine.registerRoom(rm_DEBUG_sprites, 'debug_sprites');
 
 // Init stats.js
-const stats = new Stats();
-stats.showPanel(0);
-document.body.appendChild( stats.dom );
+// const stats = new Stats();
+// stats.showPanel(0);
+// document.body.appendChild( stats.dom );
 
 let physicsFrame=0;
 window.physicsFrame = physicsFrame;
@@ -111,7 +111,7 @@ function main() {
     // Draw things. no user interaction or physics here.
 
     try {
-        stats.begin();
+        // stats.begin();
         engine.frames++;
         canvas.fill(engine.room.bgColor ?? 'black');
 
@@ -120,11 +120,11 @@ function main() {
 
         // engine.drawCursor();
 
-        stats.end();
+        // stats.end();
 
         if (debug) {
             canvas.drawText(`physics ticks: ${engine.physicsFrames} (~${(engine.physicsFrames/60).toFixed(1)}sec)`, 0, 0,{textBaseline:'top'})
-            canvas.drawText(`frames: ${engine.frames}`, 0, 8,{textBaseline:'top'})
+            canvas.drawText(`camera pos: ${canvas.camera.x},${canvas.camera.y}`, 0, 8,{textBaseline:'top'})
             canvas.drawText(`run time: ${((performance.now()-readyTime)/1000).toFixed(1)}sec`, 0, 16, {textBaseline:'top'})
             canvas.drawText(`keys: ${JSON.stringify(keyboard.keys)}`, 0, 24, {textBaseline:'top'})
         }
diff --git a/src/js/objects/ground.js b/src/js/objects/ground.js
index c1f5696..2115881 100644
--- a/src/js/objects/ground.js
+++ b/src/js/objects/ground.js
@@ -19,7 +19,7 @@ export default class Ground extends Entity {
 
     draw() {
         if (!(this.surface || this.below)) return;
-        canvas.tileImage(this.surface, round(this.x), round     (this.y), this.width, 8*this.scale, 8*this.scale, 8*this.scale);
+        canvas.tileImage(this.surface, round(this.x), round(this.y), this.width, 8*this.scale, 8*this.scale, 8*this.scale);
         canvas.tileImage(this.below, round(this.x), round(this.y+8*this.scale), this.width, this.height-8*this.scale, 8*this.scale, 8*this.scale);
         // canvas.fillRect(this.x, this.y, this.width, this.height);
     }
diff --git a/src/js/objects/player.js b/src/js/objects/player.js
index 5949055..2e09fc5 100644
--- a/src/js/objects/player.js
+++ b/src/js/objects/player.js
@@ -6,6 +6,8 @@ export default class Player extends Entity {
         super(props);
 
         this.jumping = false;
+
+        this.lastFramePos = {x: 0, y: 0};
     }
 
     draw() {
diff --git a/src/js/rooms/game.js b/src/js/rooms/game.js
index eefa66a..68dccdd 100644
--- a/src/js/rooms/game.js
+++ b/src/js/rooms/game.js
@@ -6,13 +6,14 @@ 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 = 300; // Per second
+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;
 }
 
@@ -21,19 +22,21 @@ rm_game.stop = _=>{
 }
 
 rm_game.step = _=>{
+    canvas.camera.step();
+
     const elapsed = 1 / 60;
     const player = rm_game.get('plr');
 
-    let friction = 0.9;
+    let friction = 0.95;
     const boost = keyboard.keys.includes("Shift") ? 40 : 0;
 
     for (const key of keyboard.keys) {
         switch (key) {
             case "ArrowLeft":
-                player.vx = -80-boost;
+                player.vx = -50-boost;
                 break;
             case "ArrowRight":
-                player.vx = 80+boost;
+                player.vx = 50+boost;
                 break;
             case " ":
                 if (!player.jumping) {
@@ -46,8 +49,8 @@ rm_game.step = _=>{
     const entitiesWithoutThePlayer = [...entities].toSpliced(entities.indexOf(player), 1);
     // console.debug(entitiesWithoutThePlayer);
 
-    player.vx = Math.min(500, player.vx + (player.ax * elapsed + GRAVITY_X*elapsed));
-    player.vy = Math.min(500, player.vy + (player.ay * elapsed + GRAVITY_Y*elapsed));
+    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;
 
@@ -76,28 +79,42 @@ rm_game.step = _=>{
     if (abs(player.vy) < 1) player.vy = 0;
     if (abs(player.vx) < 1) player.vx = 0;
 
-    // Update the camera
-    canvas.camera.goTo(
-        Math.min(Math.max(player.x+(player.width/2)-canvas.width/2, canvas.width/8), rm_game.width-canvas.width),
-        Math.min(Math.max(player.y-canvas.height/8, canvas.height/8), rm_game.height-canvas.height)
-    );
+    if ({x: player.x, y: player.y} !== player.lastFramePos) {
+        player.lastFramePos = {x: player.x, y: player.y};
+        // Update the camera
+        canvas.camera.goTo(
+            Math.min(Math.max(player.x+(player.width/2)-canvas.width/2, canvas.width/8), rm_game.width-canvas.width),
+            Math.min(Math.max(player.y-canvas.height/8, canvas.height/8), rm_game.height-canvas.height),
+            100
+        );
+    }
+}
+
+rm_game.draw = _ => {
+    canvas.ctx.save();
+    canvas.ctx.translate(-canvas.camera.x, -canvas.camera.y);
+    for (let thing of rm_game.entities) {
+        thing.draw();
+    }
+    canvas.strokeRect(rm_game.x, rm_game.y, player.width, player.height);
+    canvas.ctx.restore();
 }
 
 rm_game.drawGui = _ => {
     // Draw the player's position
     canvas.setFillColor('black');
-    canvas.drawText(`Position: (${player.x},${player.y})`, 10, canvas.height-10, {
-        maxWidth: canvas.width-20
+    canvas.drawText(`Position: (${player.x},${player.y})`, 5, canvas.height-5, {
+        maxWidth: canvas.width-10,
+        size: 4
     });
-    canvas.drawText(`Velocity: (${player.vx},${player.vy})`, 10, canvas.height-18, {
-        maxWidth: canvas.width-20
+    canvas.drawText(`Velocity: (${player.vx},${player.vy})`, 5, canvas.height-9, {
+        maxWidth: canvas.width-10,
+        size: 4
     });
-    canvas.drawText(`Acceleration: (${player.ax},${player.ay})`, 10, canvas.height-26, {
-        maxWidth: canvas.width-20
+    canvas.drawText(`Acceleration: (${player.ax},${player.ay})`, 5, canvas.height-13, {
+        maxWidth: canvas.width-10,
+        size: 4
     });
-
-    // Draw the player's position
-    // canvas.strokeRect(rm_game.x, rm_game.y, 10, 16);
 }
 
 rm_game.init = _=>{
@@ -105,12 +122,12 @@ rm_game.init = _=>{
 
     player.x = 40;
     player.y = 40;
-    player.width = 16;
-    player.height = 26;
+    player.width = 8;
+    player.height = 8*(8/5);
     window.player = player;
     rm_game.push(player, 'plr');
 
-    let ground = new Ground(2);
+    let ground = new Ground(1);
     ground.width = rm_game.width;
     ground.y = rm_game.height - ground.height;
 
-- 
GitLab