From ac89bd1bed71aa473b50717ce682e9d7f06837bb Mon Sep 17 00:00:00 2001 From: Logan Hunt Date: Sat, 2 Apr 2022 16:13:23 -0600 Subject: [PATCH] Fix out-of-bounds issues --- src/systems/physics.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/physics.js b/src/systems/physics.js index c9b6cf7..0da8b9d 100644 --- a/src/systems/physics.js +++ b/src/systems/physics.js @@ -2,10 +2,12 @@ game.system.Physics = () => { const update = (elapsedTime) => { for (let id in game.entities) { const entity = game.entities[id]; - if (entity.hasComponent("momentum")) { + if (entity.hasComponent("momentum") && entity.hasComponent("appearance")) { const {dx, dy} = entity.components.momentum; entity.components.position.x += dx * elapsedTime; entity.components.position.y += dy * elapsedTime; + entity.components.position.x = Math.max(0, Math.min(game.canvas.width - entity.components.appearance.width, entity.components.position.x)); + entity.components.position.y = Math.max(0, Math.min(game.canvas.height - entity.components.appearance.height, entity.components.position.y)); } } }