summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLogan Hunt <loganhunt@simponic.xyz>2022-04-02 23:27:07 -0600
committerLogan Hunt <loganhunt@simponic.xyz>2022-04-02 23:27:07 -0600
commitbb75e40de92fb7e0589410b67e21087f27f34f45 (patch)
tree9c884185bd5feb9d505c4c0ca9a35b31ffb90696
parentcf4dbf91ddb980c051ee2905367143d433c376a5 (diff)
downloadbbiy-bb75e40de92fb7e0589410b67e21087f27f34f45.tar.gz
bbiy-bb75e40de92fb7e0589410b67e21087f27f34f45.zip
Movement bug with two+ pushable entities
-rw-r--r--src/bootstrap.js2
-rw-r--r--src/game.js2
-rw-r--r--src/systems/gridSystem.js33
-rw-r--r--src/systems/physics.js3
-rw-r--r--src/utils/clamp.js6
5 files changed, 32 insertions, 14 deletions
diff --git a/src/bootstrap.js b/src/bootstrap.js
index 7606939..65afc17 100644
--- a/src/bootstrap.js
+++ b/src/bootstrap.js
@@ -1,6 +1,6 @@
game.bootstrap = (() => {
const scripts = [
- { src: ['src/utils/objectEquivalence.js', 'src/utils/unitizeVector.js'], id: 'utils'},
+ { src: ['src/utils/objectEquivalence.js', 'src/utils/unitizeVector.js', 'src/utils/clamp.js'], id: 'utils'},
{ src: ['src/render/graphics.js'], id: 'graphics' },
{ src: ['src/components/component.js'], id: 'component' },
{
diff --git a/src/game.js b/src/game.js
index 07ea67b..7301f73 100644
--- a/src/game.js
+++ b/src/game.js
@@ -26,7 +26,7 @@ game.initialize = () => {
game.entities = {};
- Array(400).fill(null).forEach((_, i) => {
+ Array(10).fill(null).forEach((_, i) => {
const bigBlue = game.createBigBlue();
bigBlue.addComponent(game.components.GridPosition({x: Math.floor(Math.random() * 15), y: Math.floor(Math.random() * 13)}));
bigBlue.addComponent(game.components.Pushable());
diff --git a/src/systems/gridSystem.js b/src/systems/gridSystem.js
index c80fe1a..bc67bbb 100644
--- a/src/systems/gridSystem.js
+++ b/src/systems/gridSystem.js
@@ -52,18 +52,31 @@ game.system.GridSystem = ({ xDim, yDim, canvasWidth, canvasHeight }) => {
});
// TODO: Loop in momentum direction until we find an entity that does not have "push" component
- const proposed = {x: entity.components.gridPosition.x + momentumVector.dx, y: entity.components.gridPosition.y + momentumVector.dy}
+ const proposed = {
+ x: entity.components.gridPosition.x + momentumVector.dx,
+ y: entity.components.gridPosition.y + momentumVector.dy
+ };
- const entitiesInCell = entitiesGrid[proposed.x][proposed.y];
-
- for (let id in entitiesInCell) {
- if (entitiesInCell[id].hasComponent("pushable")) {
- entitiesInCell[id].addComponent(game.components.Momentum({...momentumVector}));
- }
- }
+ const proposedCopy = {...proposed};
+ let found = false;
+ do {
+ found = false;
+ const entitiesInCell = entitiesGrid[proposedCopy.y][proposedCopy.x];
+ entitiesInCell.forEach((entity) => {
+ if (entity.hasComponent("pushable")) {
+ entity.addComponent(game.components.Momentum({...momentumVector}));
+ found = true;
+ }
+ });
+ proposedCopy.x += momentumVector.dx;
+ proposedCopy.y += momentumVector.dy;
+ const proposedCopyInBounds = clamp(proposedCopy, xDim, yDim);
+ if (!equivalence(proposedCopyInBounds, proposedCopy)) {
+ found = false;
+ }
+ } while (found);
- entity.components.gridPosition.x = entity.components.gridPosition.x + momentumVector.dx;
- entity.components.gridPosition.y = entity.components.gridPosition.y + momentumVector.dy;
+ entity.components.gridPosition = {...entity.components.gridPosition, ...proposed};
entity.components.position = {
...entity.components.position,
diff --git a/src/systems/physics.js b/src/systems/physics.js
index 0da8b9d..a49edca 100644
--- a/src/systems/physics.js
+++ b/src/systems/physics.js
@@ -6,8 +6,7 @@ game.system.Physics = () => {
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));
+ entity.components.position = clamp(entity.components.position, game.canvas.width - entity.components.appearance.width, game.canvas.height - entity.components.appearance.height);
}
}
}
diff --git a/src/utils/clamp.js b/src/utils/clamp.js
new file mode 100644
index 0000000..b709ee5
--- /dev/null
+++ b/src/utils/clamp.js
@@ -0,0 +1,6 @@
+const clamp = (vector, maxX, maxY) => {
+ const newVector = {...vector};
+ newVector.x = Math.max(0, Math.min(maxX, vector.x));
+ newVector.y = Math.max(0, Math.min(maxY, vector.y));
+ return newVector;
+} \ No newline at end of file