From 1436edfd1590984cc1f1e61cf95bf4bdea7a3a11 Mon Sep 17 00:00:00 2001 From: Bye <bye@byecorps.com> Date: Tue, 22 Aug 2023 12:46:04 +0100 Subject: [PATCH] bye bye physics! --- src/js/game.js | 154 +++++++------------------------------------------ 1 file changed, 21 insertions(+), 133 deletions(-) diff --git a/src/js/game.js b/src/js/game.js index 9031c01..e9f400d 100644 --- a/src/js/game.js +++ b/src/js/game.js @@ -2,7 +2,7 @@ import { WIDTH, HEIGHT, GAME_TITLE } from "./config.js"; import { Canvas } from "./canvas.js"; import { TextRenderer } from "./text.js"; -import { Room, Object } from "./objects.js"; +import { Room, GameObject } from "./objects.js"; import { getParameter } from "./utils.js"; import { getMousePos } from "./inputs/mouse.js"; import { isKeyUp, whichKeyDown } from "./inputs/keyboard.js"; @@ -14,7 +14,6 @@ let assets = { images: { splash: "splash1.webp", font: "hampsterfont.webp", - selector: "selector.webp", catapult: "catapult.webp", debug_ball: "ball.webp", }, @@ -55,12 +54,14 @@ splash.onload = () => { } // Entity class is here because otherwise every entity would need the canvas passed into it -class Entity extends Object { +class Entity extends GameObject { constructor(x=0, y=0, spritesheet=null, sprite=null) { super(); this.x = x; this.y = y; this.sprite = sprite; + this.physics = 0; + this.hitbox = {x: 0, y: 0, w: 0, h: 0}; } draw() { @@ -91,6 +92,8 @@ loadingRoom.updateStatus = (status) => { canvas.fill("#222034"); canvas.drawImage(splash, canvas.width / 2 - splash.width / 2, canvas.height / 2 - splash.height / 2); text.render(status, 0, 0); + + } const debugRoom = new Room("debug"); @@ -174,141 +177,12 @@ menuRoom.keyDown = (key) => { if (menuRoom.index < 0) menuRoom.index = menuRoom.options.length-1; } -// The following rooms should be removed before submitting. -const testing_graphing = new Room("testing_graphing"); -testing_graphing.yScale = 10; -testing_graphing.a = 0; -testing_graphing.b = 0; -testing_graphing.c = 0; - -testing_graphing.init = () => { - // canvas.setDimensions(window.innerWidth, window.innerHeight); -} - -testing_graphing.keyDown = (key) => { - if (pressedLastFrame.includes(key)) return; - - const keyActions = { - ArrowUp: () => testing_graphing.a++, - ArrowDown: () => testing_graphing.a--, - ArrowLeft: () => testing_graphing.b --, - ArrowRight: () => testing_graphing.b ++, - KeyA: () => testing_graphing.c++, - KeyD: () => testing_graphing.c--, - }; - - const action = keyActions[key]; - if (action) action(); -} - -testing_graphing.draw = () => { - // draws a quadratic graph - // y = ax^2 + bx + c - canvas.drawLine(0, canvas.height/2, canvas.width, canvas.height/2, "white"); - canvas.drawLine(canvas.width/2, 0, canvas.width/2, canvas.height, "white"); - for (let i = 0; i < canvas.width; i++) { - let x = i - canvas.width/2; - let y = (testing_graphing.a * (x**2) + testing_graphing.b * x + testing_graphing.c) / testing_graphing.yScale; - canvas.drawRect(i, canvas.height/2 - y, 1, 1, "white"); - } -} - -testing_graphing.drawGUI = () => { - text.render(`y = ${testing_graphing.a}x^2 + ${testing_graphing.b}x + ${testing_graphing.c}`, 0, 0); -} - -const testing_physics = new Room("testing_physics"); -testing_physics.init = () => { - canvas.setDimensions(1000, 500) -} -testing_physics.gravity = 0.00005; // m/s^2 -testing_physics.rho = 1; - -let testing_ball = new Entity(canvas.width/2, 0); -testing_physics.objects.push(testing_ball); - -testing_ball.velocity = { x: 10, y: 0 }; -testing_ball.mass = 70; // kg -testing_ball.radius = 4; // pixels -testing_ball.restitution = 0.2; // 0-1, 1 being perfectly elastic -testing_ball.Cd = 0.47; // dimensionless - -testing_ball.step = () => { - // Calculate forces on the x and y axis - var Fx = -0.5 * testing_ball.Cd * testing_ball.A * testing_physics.rho * testing_ball.velocity.x * testing_ball.velocity.x * testing_ball.velocity.x / Math.abs(testing_ball.velocity.x); - var Fy = -0.5 * testing_ball.Cd * testing_ball.A * testing_physics.rho * testing_ball.velocity.y * testing_ball.velocity.y * testing_ball.velocity.y / Math.abs(testing_ball.velocity.y); - - Fx = (isNaN(Fx) ? 0 : Fx); - Fy = (isNaN(Fy) ? 0 : Fy); - - // Acceleration - var ax = (Fx / testing_ball.mass); - var ay = testing_physics.gravity + (Fy / testing_ball.mass); - - testing_ball.velocity.x += ax * targetFrames; - testing_ball.velocity.y += ay * targetFrames; - - // Position - testing_ball.x += testing_ball.velocity.x * targetFrames; - testing_ball.y += testing_ball.velocity.y * targetFrames; - - if (testing_ball.y > canvas.height - testing_ball.radius) { - testing_ball.velocity.y *= -testing_ball.restitution; - testing_ball.y = canvas.height - testing_ball.radius; - } - - if (testing_ball.x > canvas.width - testing_ball.radius) { - testing_ball.velocity.x *= -testing_ball.restitution; - testing_ball.x = canvas.width - testing_ball.radius; - } - - if (testing_ball.x < testing_ball.radius) { - testing_ball.velocity.x *= -testing_ball.restitution; - testing_ball.x = testing_ball.radius; - } - - debugStatuses.push(`acceleration: ${ax}, ${ay}`); - debugStatuses.push(`force: ${Fx}, ${Fy}`); -} - -testing_ball.draw = () => { - canvas.drawImage(assets.images.debug_ball, testing_ball.x - 4, testing_ball.y - 4); -} - -testing_physics.keyDown = (key) => { - if (pressedLastFrame.includes(key)) return; - - const keyActions = { - KeyL: _=> { - testing_ball.velocity = {x: 20, y: 100} - }, - KeyS: _=> { - testing_ball.velocity = {x: 0, y: 0} - } - }; - - const action = keyActions[key]; - if (action) action(); -} - -testing_physics.drawGUI = () => { - debugStatuses.push(`position: ${testing_ball.x}, ${testing_ball.y}`); - debugStatuses.push(`velocity: ${testing_ball.velocity.x}, ${testing_ball.velocity.y}`); - -} -testing_physics.onclick = (pos={x:0,y:0}) => { - testing_ball.x = pos.x; - testing_ball.y = pos.y; -} rooms.push(loadingRoom); rooms.push(menuRoom); rooms.push(debugRoom); -// REMOVE THESE -rooms.push(testing_graphing); -rooms.push(testing_physics); currentRoom = rooms[roomIndex]; @@ -331,7 +205,7 @@ let main = () => { // main game loop currentRoom.step(); - // canvas.fill(currentRoom.background); + canvas.fill(currentRoom.background); currentRoom.drawGUI(); @@ -355,6 +229,7 @@ let main = () => { // main game loop } } + console.debug(debugStatuses) for (let i = 0; i < debugStatuses.length; i++) { switch (typeof (debugStatuses[i])) { case "string": @@ -379,22 +254,35 @@ let main = () => { // main game loop let init = () => { // begin loading all the assets. currentRoom.updateStatus("Loading images..."); + let errors = []; for (let image in assets.images) { currentRoom.updateStatus("Loading image " + image); let img = new Image(); img.src = imgPrefix + assets.images[image]; + img.onerror = (err) => { + errors.push(err); + console.error(err); + } img.onload = () => { assets.images[image] = img; } } console.log("Images loaded.") currentRoom.updateStatus("Loading complete!"); + + setTimeout(() => { + if (errors.length > 0) { + for (let i = 0; i < errors.length; i++) { + text.render(errors[i], 0, 5*i+1); + } + } (getParameter("room") ? changeRoom(searchForRoom(getParameter("room"))) : changeRoom(searchForRoom("menu"))); main(); }, 1000); } + window.onload = () => { document.title = GAME_TITLE; init(); -- GitLab