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
|
import { Entity, EntityNames } from ".";
import { IMAGES, SPRITE_SPECS, Sprites, type SpriteSpec } from "../config";
import {
FacingDirection,
Sprite,
Grid,
BoundingBox,
Control,
Pushable,
} from "../components";
import { Coord2D, Direction } from "../interfaces/";
export class Player extends Entity {
private static spriteSpec: SpriteSpec = SPRITE_SPECS.get(
Sprites.PLAYER,
) as SpriteSpec;
constructor(gridPosition: Coord2D) {
super(EntityNames.Player);
this.addComponent(
new BoundingBox(
{
x: 0,
y: 0,
},
{ width: Player.spriteSpec.width, height: Player.spriteSpec.height },
0,
),
);
this.addComponent(new Pushable());
this.addComponent(new Control());
this.addComponent(new Grid(gridPosition));
this.addFacingDirectionComponents();
}
private addFacingDirectionComponents() {
const facingDirectionComponent = new FacingDirection();
[
Direction.NONE,
Direction.LEFT,
Direction.RIGHT,
Direction.UP,
Direction.DOWN,
].forEach((direction) => {
const sprite = new Sprite(
IMAGES.get(Player.spriteSpec.states!.get(direction)!.sheet!)!,
{ x: 0, y: 0 },
{ width: Player.spriteSpec.width, height: Player.spriteSpec.height },
Player.spriteSpec.msPerFrame,
Player.spriteSpec.frames,
);
facingDirectionComponent.directionSprites.set(direction, sprite);
});
this.addComponent(facingDirectionComponent);
this.addComponent(
facingDirectionComponent.directionSprites.get(Direction.NONE)!,
); // face no direction by default
}
}
|