diff options
Diffstat (limited to 'src/engine/systems/Grid.ts')
-rw-r--r-- | src/engine/systems/Grid.ts | 55 |
1 files changed, 42 insertions, 13 deletions
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<Highlight>( + ComponentNames.Highlight, + )!; + highlight.highlight(); } }); game.forEachEntityWithComponent(ComponentNames.Highlight, (entity) => { if (!highlightableEntities.has(entity.id)) { - entity.removeComponent(ComponentNames.Highlight); + const highlight = entity.getComponent<Highlight>( + 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<GridComponent>(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<string>(); + 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<GridComponent>(ComponentNames.Grid)!; + const { movingDirection } = entity.getComponent<GridComponent>( + 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<GridComponent>( - 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<GridComponent>(ComponentNames.Grid)!; + grid.movingDirection = movingDirection; + entity.addComponent(grid); + } } } |