diff options
author | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-05 19:45:09 -0600 |
---|---|---|
committer | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-05 19:45:09 -0600 |
commit | 558e91a4420bb52e9b8dad624cff3859ec64ea43 (patch) | |
tree | e0fdac613ebd8f01f03fe0c0d202833c908f5acd /src/systems/collision.js | |
parent | 69596ba244d6959dfe188ce958542c062b475f75 (diff) | |
download | bbiy-558e91a4420bb52e9b8dad624cff3859ec64ea43.tar.gz bbiy-558e91a4420bb52e9b8dad624cff3859ec64ea43.zip |
Added collision system, added changedId's which doesn't do anything rn
Diffstat (limited to 'src/systems/collision.js')
-rw-r--r-- | src/systems/collision.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/systems/collision.js b/src/systems/collision.js new file mode 100644 index 0000000..7739bae --- /dev/null +++ b/src/systems/collision.js @@ -0,0 +1,49 @@ +game.system.Collision = (entitiesGrid) => { + const update = (elapsedTime, entities, changedIds) => { + for (let entity of Object.keys(entities).map((id) => entities[id])) { + if (entity.hasComponent("controllable") && entity.hasComponent("gridPosition") && entity.hasComponent("momentum")) { + const momentum = unitize(entity.components.momentum); + + let found; + const proposed = {x: entity.components.gridPosition.x + momentum.dx, y: entity.components.gridPosition.y + momentum.dy}; + const entitiesToPush = []; + let wall = false; + do { + const proposedClampedInBounds = clamp(proposed, game.config.xDim-1, game.config.yDim-1); + if (!equivalence(proposed, proposedClampedInBounds)) { + break; + } + + found = false; + + const entitiesInCell = entitiesGrid[proposed.y][proposed.x]; + + for (let next of entitiesInCell.values()) { + if (next.hasComponent("stop")) { + console.log("WALL FOUND") + wall = next; + found = false; + break; + } + if (next.hasComponent("pushable")) { + entitiesToPush.push(next); + found = true; + } + } + + proposed.x += momentum.dx; + proposed.y += momentum.dy; + } while(found); + + if (wall) { + console.log("WALL") + entity.removeComponent("momentum"); + } else { + entitiesToPush.map((e) => e.addComponent(game.components.Momentum({...momentum}))); + } + } + } + return new Set(); + }; + return { update }; +}; |