From cf4dbf91ddb980c051ee2905367143d433c376a5 Mon Sep 17 00:00:00 2001 From: phat_sumo Date: Sat, 2 Apr 2022 18:48:00 -0600 Subject: [PATCH] the mess that is pushable --- src/bootstrap.js | 2 +- src/components/pushable.js | 1 + src/game.js | 2 ++ src/systems/gridSystem.js | 12 +++++++++++- 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/components/pushable.js diff --git a/src/bootstrap.js b/src/bootstrap.js index 93cb8f5..7606939 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -6,7 +6,7 @@ game.bootstrap = (() => { { src: [ 'src/components/position.js', 'src/components/momentum.js', 'src/components/gridPosition.js', - 'src/components/appearence.js', 'src/components/controllable.js' + 'src/components/appearence.js', 'src/components/controllable.js', 'src/components/pushable.js', ], id: 'components' }, diff --git a/src/components/pushable.js b/src/components/pushable.js new file mode 100644 index 0000000..85087ea --- /dev/null +++ b/src/components/pushable.js @@ -0,0 +1 @@ +game.components.Pushable = () => game.Component('pushable'); diff --git a/src/game.js b/src/game.js index 04aa4fc..07ea67b 100644 --- a/src/game.js +++ b/src/game.js @@ -29,6 +29,7 @@ game.initialize = () => { Array(400).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()); game.entities[bigBlue.id] = bigBlue; }); @@ -36,6 +37,7 @@ game.initialize = () => { game.rock.addComponent(game.components.Position({x: 200, y: 200})); game.rock.addComponent(game.components.GridPosition({x: 0, y: 0})); game.rock.addComponent(game.components.Controllable({controls: ['left', 'right', 'up', 'down']})); + game.rock.addComponent(game.components.Pushable()); game.entities[game.rock.id] = game.rock; lastTimeStamp = performance.now() diff --git a/src/systems/gridSystem.js b/src/systems/gridSystem.js index 05ce17e..c80fe1a 100644 --- a/src/systems/gridSystem.js +++ b/src/systems/gridSystem.js @@ -52,6 +52,16 @@ 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 entitiesInCell = entitiesGrid[proposed.x][proposed.y]; + + for (let id in entitiesInCell) { + if (entitiesInCell[id].hasComponent("pushable")) { + entitiesInCell[id].addComponent(game.components.Momentum({...momentumVector})); + } + } + entity.components.gridPosition.x = entity.components.gridPosition.x + momentumVector.dx; entity.components.gridPosition.y = entity.components.gridPosition.y + momentumVector.dy; @@ -70,4 +80,4 @@ game.system.GridSystem = ({ xDim, yDim, canvasWidth, canvasHeight }) => { }; return { entitiesGrid, gameCoordsToGrid, gridCoordsToGame, update, gridWidth, gridHeight }; -}; \ No newline at end of file +};