summaryrefslogtreecommitdiff
path: root/src/engine/systems
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/systems')
-rw-r--r--src/engine/systems/FacingDirection.ts63
-rw-r--r--src/engine/systems/Input.ts63
-rw-r--r--src/engine/systems/SystemNames.ts1
-rw-r--r--src/engine/systems/index.ts2
4 files changed, 129 insertions, 0 deletions
diff --git a/src/engine/systems/FacingDirection.ts b/src/engine/systems/FacingDirection.ts
new file mode 100644
index 0000000..f831bf6
--- /dev/null
+++ b/src/engine/systems/FacingDirection.ts
@@ -0,0 +1,63 @@
+import {
+ ComponentNames,
+ FacingDirection as FacingDirectionComponent,
+ BoundingBox,
+ Sprite,
+} from "../components";
+import { Game } from "../Game";
+import { System, SystemNames, Input } from ".";
+import { Direction, angleToDirection } from "../interfaces";
+
+export class FacingDirection extends System {
+ private input: Input;
+
+ constructor(input: Input) {
+ super(SystemNames.FacingDirection);
+
+ this.input = input;
+ }
+
+ public update(_dt: number, game: Game) {
+ const mousePosition = this.input.getMousePosition();
+ const mouseBoundingBox = new BoundingBox(mousePosition, {
+ width: 0,
+ height: 0,
+ });
+
+ game.forEachEntityWithComponent(
+ ComponentNames.FacingDirection,
+ (entity) => {
+ if (!entity.hasComponent(ComponentNames.BoundingBox)) {
+ return;
+ }
+
+ const boundingBox = entity.getComponent<BoundingBox>(
+ ComponentNames.BoundingBox,
+ )!;
+ const facingDirection = entity.getComponent<FacingDirectionComponent>(
+ ComponentNames.FacingDirection,
+ );
+
+ const { center } = boundingBox;
+ const angle = Math.atan2(
+ mousePosition.y - center.y,
+ mousePosition.x - center.x,
+ );
+
+ const mouseInBoundingBox =
+ boundingBox.isCollidingWith(mouseBoundingBox);
+ const direction = mouseInBoundingBox
+ ? Direction.NONE
+ : angleToDirection(angle);
+
+ facingDirection.setDirection(direction);
+
+ const oldSprite = entity.getComponent<Sprite>(ComponentNames.Sprite);
+ const sprite = facingDirection.directionSprites.get(direction)!;
+ sprite.fillTimingsFromSprite(oldSprite);
+
+ entity.addComponent(sprite);
+ },
+ );
+ }
+}
diff --git a/src/engine/systems/Input.ts b/src/engine/systems/Input.ts
new file mode 100644
index 0000000..9b88378
--- /dev/null
+++ b/src/engine/systems/Input.ts
@@ -0,0 +1,63 @@
+import { SystemNames, System } from ".";
+import { Game } from "..";
+import { ComponentNames } from "../components";
+import { Control } from "../components/Control";
+import { Action, KeyConstants } from "../config";
+import { Entity } from "../entities";
+import { Coord2D } from "../interfaces";
+
+export class Input extends System {
+ private keys: Set<string>;
+ private mousePosition: Coord2D;
+
+ constructor() {
+ super(SystemNames.Input);
+
+ this.keys = new Set();
+ this.mousePosition = { x: 0, y: 0 };
+ }
+
+ public clearKeys() {
+ this.keys.clear();
+ }
+
+ public keyPressed(key: string) {
+ this.keys.add(key);
+ }
+
+ public keyReleased(key: string) {
+ this.keys.delete(key);
+ }
+
+ public update(_dt: number, game: Game) {
+ game.forEachEntityWithComponent(ComponentNames.Control, (entity) =>
+ this.handleInput(entity),
+ );
+ }
+
+ public handleInput(entity: Entity) {
+ const controlComponent = entity.getComponent<Control>(
+ ComponentNames.Control,
+ );
+ if (!controlComponent.isControllable) return;
+
+ if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.INTERACT))) {
+ console.log("interact");
+ }
+ }
+
+ private hasSomeKey(keys?: string[]): boolean {
+ if (keys) {
+ return keys.some((key) => this.keys.has(key));
+ }
+ return false;
+ }
+
+ public setMousePosition(mousePosition: Coord2D) {
+ this.mousePosition = mousePosition;
+ }
+
+ public getMousePosition(): Coord2D {
+ return this.mousePosition;
+ }
+}
diff --git a/src/engine/systems/SystemNames.ts b/src/engine/systems/SystemNames.ts
index 41207a4..1ed9894 100644
--- a/src/engine/systems/SystemNames.ts
+++ b/src/engine/systems/SystemNames.ts
@@ -4,4 +4,5 @@ export namespace SystemNames {
export const Input = "Input";
export const Collision = "Collision";
export const WallBounds = "WallBounds";
+ export const FacingDirection = "FacingDirection";
}
diff --git a/src/engine/systems/index.ts b/src/engine/systems/index.ts
index bb87060..31c98ac 100644
--- a/src/engine/systems/index.ts
+++ b/src/engine/systems/index.ts
@@ -1,3 +1,5 @@
export * from "./SystemNames";
export * from "./System";
export * from "./Render";
+export * from "./Input";
+export * from "./FacingDirection";