From 06bb4177202b432d5f42141975ec82b5a8837f0e Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sat, 2 Mar 2024 02:22:46 -0700 Subject: slight refactor in collision behavior --- src/engine/systems/Grid.ts | 55 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 13 deletions(-) (limited to 'src/engine/systems/Grid.ts') diff --git a/src/engine/systems/Grid.ts b/src/engine/systems/Grid.ts index c9cab6b..28ca5ea 100644 --- a/src/engine/systems/Grid.ts +++ b/src/engine/systems/Grid.ts @@ -71,14 +71,20 @@ export class Grid extends System { highlightableEntities.forEach((id) => { const entity = game.getEntity(id)!; - if (!entity.hasComponent(ComponentNames.Highlight)) { - entity.addComponent(new Highlight()); + if (entity.hasComponent(ComponentNames.Highlight)) { + const highlight = entity.getComponent( + ComponentNames.Highlight, + )!; + highlight.highlight(); } }); game.forEachEntityWithComponent(ComponentNames.Highlight, (entity) => { if (!highlightableEntities.has(entity.id)) { - entity.removeComponent(ComponentNames.Highlight); + const highlight = entity.getComponent( + ComponentNames.Highlight, + )!; + highlight.unhighlight(); } }); } @@ -97,21 +103,41 @@ export class Grid extends System { // continue until no more pushable entities are found for (const entity of movingEntities) { const grid = entity.getComponent(ComponentNames.Grid)!; + const { gridPosition, movingDirection } = grid; + + grid.movingDirection = Direction.NONE; + entity.addComponent(grid); // default to not moving + let nextGridPosition = this.getNewGridPosition( - grid.gridPosition, - grid.movingDirection, + gridPosition, + movingDirection, ); + + const moving = new Set(); + moving.add(entity.id); + while (!this.isOutOfBounds(nextGridPosition)) { const { x, y } = nextGridPosition; const entities = Array.from(this.grid[y][x]).map( (id) => game.getEntity(id)!, ); + if ( + entities.some((entity) => + entity.hasComponent(ComponentNames.Colliding), + ) + ) { + moving.clear(); + break; + } + const pushableEntities = entities.filter((entity) => { if (!entity.hasComponent(ComponentNames.Grid)) return false; - const { pushable, movingDirection } = - entity.getComponent(ComponentNames.Grid)!; + const { movingDirection } = entity.getComponent( + ComponentNames.Grid, + )!; + const pushable = entity.hasComponent(ComponentNames.Pushable); return movingDirection === Direction.NONE && pushable; }); if (pushableEntities.length === 0) { @@ -119,18 +145,21 @@ export class Grid extends System { } for (const pushableEntity of pushableEntities) { - const pushableGrid = pushableEntity.getComponent( - ComponentNames.Grid, - )!; - pushableGrid.movingDirection = grid.movingDirection; - pushableEntity.addComponent(pushableEntity); + moving.add(pushableEntity.id); } nextGridPosition = this.getNewGridPosition( nextGridPosition, - grid.movingDirection, + movingDirection, ); } + + for (const id of moving) { + const entity = game.getEntity(id)!; + const grid = entity.getComponent(ComponentNames.Grid)!; + grid.movingDirection = movingDirection; + entity.addComponent(grid); + } } } -- cgit v1.2.3-70-g09d2