the mess that is pushable

This commit is contained in:
phat_sumo 2022-04-02 18:48:00 -06:00
parent aef3c05bc7
commit cf4dbf91dd
4 changed files with 15 additions and 2 deletions

2
src/bootstrap.js vendored
View File

@ -6,7 +6,7 @@ game.bootstrap = (() => {
{ {
src: [ src: [
'src/components/position.js', 'src/components/momentum.js', 'src/components/gridPosition.js', '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' id: 'components'
}, },

View File

@ -0,0 +1 @@
game.components.Pushable = () => game.Component('pushable');

View File

@ -29,6 +29,7 @@ game.initialize = () => {
Array(400).fill(null).forEach((_, i) => { Array(400).fill(null).forEach((_, i) => {
const bigBlue = game.createBigBlue(); 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.GridPosition({x: Math.floor(Math.random() * 15), y: Math.floor(Math.random() * 13)}));
bigBlue.addComponent(game.components.Pushable());
game.entities[bigBlue.id] = bigBlue; 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.Position({x: 200, y: 200}));
game.rock.addComponent(game.components.GridPosition({x: 0, y: 0})); 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.Controllable({controls: ['left', 'right', 'up', 'down']}));
game.rock.addComponent(game.components.Pushable());
game.entities[game.rock.id] = game.rock; game.entities[game.rock.id] = game.rock;
lastTimeStamp = performance.now() lastTimeStamp = performance.now()

View File

@ -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 // 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.x = entity.components.gridPosition.x + momentumVector.dx;
entity.components.gridPosition.y = entity.components.gridPosition.y + momentumVector.dy; 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 }; return { entitiesGrid, gameCoordsToGrid, gridCoordsToGame, update, gridWidth, gridHeight };
}; };