summaryrefslogtreecommitdiff
path: root/engine/entities/Player.ts
blob: 0ba5a41aa5e774a31e5d1f7a96deadc58a40572f (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Entity } from ".";
import { IMAGES, SPRITE_SPECS, Sprites, type SpriteSpec } from "../config";
import {
  Jump,
  FacingDirection,
  BoundingBox,
  Sprite,
  Velocity,
  Gravity,
  WallBounded,
  Forces,
  Collide,
  Control,
  Mass,
  Moment,
} from "../components";
import { PhysicsConstants } from "../config";
import { Direction } from "../interfaces";

export class Player extends Entity {
  private static MASS: number = 10;
  private static MOI: number = 1000;

  private static spriteSpec: SpriteSpec = SPRITE_SPECS.get(Sprites.COFFEE);

  constructor() {
    super();

    this.addComponent(
      new BoundingBox(
        { x: 300, y: 100 },
        { width: Player.spriteSpec.width, height: Player.spriteSpec.height },
        0
      )
    );

    this.addComponent(new Velocity({ dx: 0, dy: 0 }, 0));

    this.addComponent(new Mass(Player.MASS));
    this.addComponent(new Moment(Player.MOI));
    this.addComponent(new Forces());
    this.addComponent(new Gravity());

    this.addComponent(new Jump());
    this.addComponent(new Control());

    this.addComponent(new Collide());
    this.addComponent(new WallBounded());

    this.addFacingDirectionComponents();
  }

  private addFacingDirectionComponents() {
    const [leftSprite, rightSprite] = [Direction.LEFT, Direction.RIGHT].map(
      (direction) =>
        new Sprite(
          IMAGES.get(Player.spriteSpec.states[direction].sheet),
          { x: 0, y: 0 },
          { width: Player.spriteSpec.width, height: Player.spriteSpec.height },
          Player.spriteSpec.msPerFrame,
          Player.spriteSpec.frames
        )
    );

    this.addComponent(new FacingDirection(leftSprite, rightSprite));
    this.addComponent(leftSprite); // face Left by default
  }
}