summaryrefslogtreecommitdiff
path: root/src/systems/gridSystem.js
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 /src/systems/gridSystem.js
parentcf4dbf91ddb980c051ee2905367143d433c376a5 (diff)
downloadbbiy-bb75e40de92fb7e0589410b67e21087f27f34f45.tar.gz
bbiy-bb75e40de92fb7e0589410b67e21087f27f34f45.zip
Movement bug with two+ pushable entities
Diffstat (limited to 'src/systems/gridSystem.js')
-rw-r--r--src/systems/gridSystem.js33
1 files changed, 23 insertions, 10 deletions
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,