Fix out-of-bounds issues

This commit is contained in:
Logan Hunt 2022-04-02 16:13:23 -06:00
parent 5a55b5fa0c
commit ac89bd1bed

View File

@ -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));
}
}
}