diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-02 05:30:17 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-02 05:30:17 -0700 |
commit | cbb88091bdf69cc8752ef1cc3662dc0b99e3ead6 (patch) | |
tree | b10e56d8f5281a9e91db8e2a923f91b216129459 /src/engine/systems/Grid.ts | |
parent | 4b9349b3f8bee21eb086cfd6e7668532a50e6048 (diff) | |
download | the-abstraction-engine-cbb88091bdf69cc8752ef1cc3662dc0b99e3ead6.tar.gz the-abstraction-engine-cbb88091bdf69cc8752ef1cc3662dc0b99e3ead6.zip |
key lock / player curry collisions
Diffstat (limited to 'src/engine/systems/Grid.ts')
-rw-r--r-- | src/engine/systems/Grid.ts | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/src/engine/systems/Grid.ts b/src/engine/systems/Grid.ts index 8756320..915335b 100644 --- a/src/engine/systems/Grid.ts +++ b/src/engine/systems/Grid.ts @@ -1,4 +1,4 @@ -import { System, SystemNames } from "."; +import { Collision, System, SystemNames } from "."; import { Game } from ".."; import { Entity } from "../entities"; import { PhysicsConstants } from "../config"; @@ -13,8 +13,8 @@ import { Coord2D, Direction, Dimension2D } from "../interfaces"; import { clamp } from "../utils"; export class Grid extends System { - private dimension: Dimension2D; - private grid: Set<string>[][] = []; + public dimension: Dimension2D; + public grid: Set<string>[][] = []; constructor( { width: columns, height: rows }: Dimension2D, @@ -108,10 +108,10 @@ export class Grid extends System { grid.movingDirection = Direction.NONE; entity.addComponent(grid); // default to not moving - let nextGridPosition = this.getNewGridPosition( + let [currentPosition, nextGridPosition] = [ gridPosition, - movingDirection, - ); + this.getNewGridPosition(gridPosition, movingDirection), + ]; const moving = new Set<string>(); moving.add(entity.id); @@ -122,11 +122,24 @@ export class Grid extends System { (id) => game.getEntity(id)!, ); - if ( - entities.some((entity) => - entity.hasComponent(ComponentNames.Colliding), + const collidingEntities = entities.filter((entity) => + entity.hasComponent(ComponentNames.Colliding), + ); + + if (collidingEntities.length > 0) { + // i.e. key going into a door or function going into an application + const allEntitiesInPreviousCellCanCollide = Array.from( + this.grid[currentPosition.y][currentPosition.x], ) - ) { + .map((id) => game.getEntity(id)!) + .every((entity) => + collidingEntities.every((collidingEntity) => + Collision.canCollide(entity.name, collidingEntity.name), + ), + ); + if (allEntitiesInPreviousCellCanCollide) { + break; + } moving.clear(); break; } @@ -148,6 +161,7 @@ export class Grid extends System { moving.add(pushableEntity.id); } + currentPosition = nextGridPosition; nextGridPosition = this.getNewGridPosition( nextGridPosition, movingDirection, @@ -324,7 +338,7 @@ export class Grid extends System { this.grid.forEach((row) => row.forEach((cell) => { for (const id of cell) { - if (movedEntities.has(id)) { + if (movedEntities.has(id) || !game.getEntity(id)) { cell.delete(id); } } |