summaryrefslogtreecommitdiff
path: root/src/engine/systems
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-01 21:29:40 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-01 21:29:40 -0700
commitc3242b171cdbb36a26fda04c7148b9b40a5f5c33 (patch)
tree5060cb6d34e01f36687c0ce79e5ae0b1b8767e63 /src/engine/systems
parentd08e0105cbc59c6cc804f04aaf1e4e625a13960c (diff)
downloadthe-abstraction-engine-c3242b171cdbb36a26fda04c7148b9b40a5f5c33.tar.gz
the-abstraction-engine-c3242b171cdbb36a26fda04c7148b9b40a5f5c33.zip
player movement
Diffstat (limited to 'src/engine/systems')
-rw-r--r--src/engine/systems/Grid.ts199
-rw-r--r--src/engine/systems/Input.ts42
-rw-r--r--src/engine/systems/SystemNames.ts4
3 files changed, 239 insertions, 6 deletions
diff --git a/src/engine/systems/Grid.ts b/src/engine/systems/Grid.ts
new file mode 100644
index 0000000..e4e4bb8
--- /dev/null
+++ b/src/engine/systems/Grid.ts
@@ -0,0 +1,199 @@
+import { System, SystemNames } from ".";
+import { Game } from "..";
+import { PhysicsConstants } from "../config";
+import {
+ BoundingBox,
+ ComponentNames,
+ Grid as GridComponent,
+} from "../components";
+import { Coord2D, Direction, Dimension2D } from "../interfaces";
+import { clamp } from "../utils";
+
+export class Grid extends System {
+ private dimension: Dimension2D;
+ private grid: Set<string>[][] = [];
+
+ constructor(
+ { width: columns, height: rows }: Dimension2D,
+ dimension: Dimension2D,
+ ) {
+ super(SystemNames.Grid);
+
+ this.dimension = dimension;
+ this.grid = new Array(rows)
+ .fill(null)
+ .map(() => new Array(columns).fill(null).map(() => new Set()));
+ }
+
+ public update(dt: number, game: Game): void {
+ this.putUninitializedEntitiesInGrid(game);
+ this.rebuildGrid(game);
+ this.updateMovingEntities(dt, game);
+ }
+
+ private putUninitializedEntitiesInGrid(game: Game) {
+ game.forEachEntityWithComponent(ComponentNames.Grid, (entity) => {
+ const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
+
+ if (grid.initialized) {
+ return;
+ }
+
+ const hasBoundingBox = entity.hasComponent(ComponentNames.BoundingBox);
+ if (!hasBoundingBox) {
+ return;
+ }
+
+ const boundingBox = entity.getComponent<BoundingBox>(
+ ComponentNames.BoundingBox,
+ )!;
+ boundingBox.center = this.gridToScreenPosition(grid.gridPosition);
+ entity.addComponent(boundingBox);
+
+ grid.initialized = true;
+ entity.addComponent(grid);
+ });
+ }
+
+ private updateMovingEntities(
+ dt: number,
+ game: Game,
+ velocity = PhysicsConstants.GRID_MOVEMENT_VELOCITY,
+ ) {
+ game.forEachEntityWithComponent(ComponentNames.Grid, (entity) => {
+ const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
+ if (grid.movingDirection === Direction.NONE) {
+ return;
+ }
+
+ const boundingBox = entity.getComponent<BoundingBox>(
+ ComponentNames.BoundingBox,
+ )!;
+
+ let { x: newX, y: newY } = grid.gridPosition;
+ switch (grid.movingDirection) {
+ case Direction.LEFT:
+ newX -= 1;
+ break;
+ case Direction.UP:
+ newY -= 1;
+ break;
+ case Direction.DOWN:
+ newY += 1;
+ break;
+ case Direction.RIGHT:
+ newX += 1;
+ break;
+ }
+
+ const newGridPosition = { x: newX, y: newY };
+ if (this.isOutOfBounds(newGridPosition)) {
+ grid.movingDirection = Direction.NONE;
+ entity.addComponent(grid);
+ return;
+ }
+
+ let { dx, dy } = { dx: 0, dy: 0 };
+ switch (grid.movingDirection) {
+ case Direction.LEFT:
+ dx = -velocity * dt;
+ break;
+ case Direction.RIGHT:
+ dx = velocity * dt;
+ break;
+ case Direction.UP:
+ dy = -velocity * dt;
+ break;
+ case Direction.DOWN:
+ dy = velocity * dt;
+ break;
+ }
+
+ const { x, y } = boundingBox.center;
+ const nextPosition = { x: x + dx, y: y + dy };
+ const passedCenter = this.isEntityPastCenterWhenMoving(
+ grid.movingDirection,
+ newGridPosition,
+ nextPosition,
+ );
+
+ if (passedCenter) {
+ // re-align the entity to its new grid position
+ this.grid[grid.gridPosition.y][grid.gridPosition.x].delete(entity.id);
+
+ grid.gridPosition = newGridPosition;
+ grid.movingDirection = Direction.NONE;
+ this.grid[grid.gridPosition.y][grid.gridPosition.x].add(entity.id);
+ entity.addComponent(grid);
+
+ boundingBox.center = this.gridToScreenPosition(grid.gridPosition);
+ entity.addComponent(boundingBox);
+ return;
+ }
+
+ boundingBox.center = nextPosition;
+ entity.addComponent(boundingBox);
+ });
+ }
+
+ private isEntityPastCenterWhenMoving(
+ direction: Direction,
+ gridPosition: Coord2D,
+ entityPosition: Coord2D,
+ ) {
+ const { x, y } = this.gridToScreenPosition(gridPosition);
+ switch (direction) {
+ case Direction.LEFT:
+ return entityPosition.x <= x;
+ case Direction.RIGHT:
+ return entityPosition.x >= x;
+ case Direction.UP:
+ return entityPosition.y <= y;
+ case Direction.DOWN:
+ return entityPosition.y >= y;
+ }
+ return false;
+ }
+
+ private gridToScreenPosition(gridPosition: Coord2D) {
+ const { width, height } = this.dimension;
+ return {
+ x: gridPosition.x * width + width / 2,
+ y: gridPosition.y * height + height / 2,
+ };
+ }
+
+ private isOutOfBounds(position: Coord2D) {
+ const isOutOfBoundsX =
+ clamp(position.x, 0, this.grid[0].length - 1) !== position.x;
+ const isOutOfBoundsY =
+ clamp(position.y, 0, this.grid.length - 1) !== position.y;
+ return isOutOfBoundsX || isOutOfBoundsY;
+ }
+
+ private rebuildGrid(game: Game) {
+ const movedEntities = new Set<string>();
+ game.forEachEntityWithComponent(ComponentNames.Grid, (entity) => {
+ const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
+ const { x, y } = grid.gridPosition;
+ if (!this.grid[y][x].has(entity.id)) {
+ movedEntities.add(entity.id);
+ this.grid[y][x].add(entity.id);
+ }
+ });
+ this.grid.forEach((row) =>
+ row.forEach((cell) => {
+ for (const id of cell) {
+ if (!movedEntities.has(id)) {
+ cell.delete(id);
+ }
+ }
+ }),
+ );
+ movedEntities.forEach((id) => {
+ const entity = game.getEntity(id)!;
+ const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
+ this.grid[grid.gridPosition.y][grid.gridPosition.x].add(id);
+ });
+ }
+}
diff --git a/src/engine/systems/Input.ts b/src/engine/systems/Input.ts
index 9b88378..e9691e0 100644
--- a/src/engine/systems/Input.ts
+++ b/src/engine/systems/Input.ts
@@ -4,7 +4,7 @@ import { ComponentNames } from "../components";
import { Control } from "../components/Control";
import { Action, KeyConstants } from "../config";
import { Entity } from "../entities";
-import { Coord2D } from "../interfaces";
+import { Coord2D, Direction } from "../interfaces";
export class Input extends System {
private keys: Set<string>;
@@ -41,8 +41,44 @@ export class Input extends System {
);
if (!controlComponent.isControllable) return;
- if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.INTERACT))) {
- console.log("interact");
+ const hasGrid = entity.hasComponent(ComponentNames.Grid);
+
+ // TODO: check grid via controlComponent to notify entity of interaction
+ const [moveUp, moveLeft, moveRight, moveDown] = [
+ Action.MOVE_UP,
+ Action.MOVE_LEFT,
+ Action.MOVE_RIGHT,
+ Action.MOVE_DOWN,
+ ].map((action) => this.hasSomeKey(KeyConstants.ActionKeys.get(action)));
+ if (hasGrid) {
+ const gridComponent = entity.getComponent(ComponentNames.Grid);
+ if (gridComponent.movingDirection !== Direction.NONE) {
+ return;
+ }
+
+ if (moveUp) {
+ gridComponent.movingDirection = Direction.UP;
+ KeyConstants.ActionKeys.get(Action.MOVE_UP)!.forEach((key) =>
+ this.keyReleased(key),
+ );
+ } else if (moveLeft) {
+ gridComponent.movingDirection = Direction.LEFT;
+ KeyConstants.ActionKeys.get(Action.MOVE_LEFT)!.forEach((key) =>
+ this.keyReleased(key),
+ );
+ } else if (moveRight) {
+ gridComponent.movingDirection = Direction.RIGHT;
+ KeyConstants.ActionKeys.get(Action.MOVE_RIGHT)!.forEach((key) =>
+ this.keyReleased(key),
+ );
+ } else if (moveDown) {
+ gridComponent.movingDirection = Direction.DOWN;
+ KeyConstants.ActionKeys.get(Action.MOVE_DOWN)!.forEach((key) =>
+ this.keyReleased(key),
+ );
+ }
+
+ entity.addComponent(gridComponent);
}
}
diff --git a/src/engine/systems/SystemNames.ts b/src/engine/systems/SystemNames.ts
index 1ed9894..0de5857 100644
--- a/src/engine/systems/SystemNames.ts
+++ b/src/engine/systems/SystemNames.ts
@@ -1,8 +1,6 @@
export namespace SystemNames {
export const Render = "Render";
- export const Physics = "Physics";
export const Input = "Input";
- export const Collision = "Collision";
- export const WallBounds = "WallBounds";
export const FacingDirection = "FacingDirection";
+ export const Grid = "Grid";
}