summaryrefslogtreecommitdiff
path: root/client/lib/systems/FacingDirection.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/lib/systems/FacingDirection.ts')
-rw-r--r--client/lib/systems/FacingDirection.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/client/lib/systems/FacingDirection.ts b/client/lib/systems/FacingDirection.ts
new file mode 100644
index 0000000..fbb4c7c
--- /dev/null
+++ b/client/lib/systems/FacingDirection.ts
@@ -0,0 +1,39 @@
+import {
+ ComponentNames,
+ Velocity,
+ FacingDirection as FacingDirectionComponent,
+} from "../components";
+import type { Entity } from "../entities";
+import { System, SystemNames } from "./";
+
+export class FacingDirection extends System {
+ constructor() {
+ super(SystemNames.FacingDirection);
+ }
+
+ public update(
+ _dt: number,
+ entityMap: Map<number, Entity>,
+ componentEntities: Map<string, Set<number>>
+ ) {
+ componentEntities
+ .get(ComponentNames.FacingDirection)
+ ?.forEach((entityId) => {
+ const entity = entityMap.get(entityId);
+ if (!entity.hasComponent(ComponentNames.Velocity)) {
+ return;
+ }
+
+ const velocity = entity.getComponent<Velocity>(ComponentNames.Velocity);
+ const facingDirection = entity.getComponent<FacingDirectionComponent>(
+ ComponentNames.FacingDirection
+ );
+
+ if (velocity.dCartesian.dx > 0) {
+ entity.addComponent(facingDirection.facingRightSprite);
+ } else if (velocity.dCartesian.dx < 0) {
+ entity.addComponent(facingDirection.facingLeftSprite);
+ }
+ });
+ }
+}