summaryrefslogtreecommitdiff
path: root/engine/systems/FacingDirection.ts
blob: c6513ac7529fad26fb64f17180581406d20fb5a8 (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
import {
  ComponentNames,
  Velocity,
  FacingDirection as FacingDirectionComponent,
} from "../components";
import { Game } from "../Game";
import type { Entity } from "../entities";
import { System, SystemNames } from "./";

export class FacingDirection extends System {
  constructor() {
    super(SystemNames.FacingDirection);
  }

  public update(_dt: number, game: Game) {
    game.componentEntities
      .get(ComponentNames.FacingDirection)
      ?.forEach((entityId) => {
        const entity = game.entities.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);
        }
      });
  }
}