summaryrefslogtreecommitdiff
path: root/client/lib/systems/FacingDirection.ts
blob: fbb4c7c83e470f5c1394f019e1fdb28d4f66b331 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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);
        }
      });
  }
}