62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
import { Entity, EntityNames } from ".";
|
|
import { IMAGES, SPRITE_SPECS, Sprites, type SpriteSpec } from "../config";
|
|
import {
|
|
FacingDirection,
|
|
Sprite,
|
|
Grid,
|
|
BoundingBox,
|
|
Control,
|
|
} from "../components";
|
|
import { Direction } from "../interfaces/";
|
|
|
|
export class Player extends Entity {
|
|
private static spriteSpec: SpriteSpec = SPRITE_SPECS.get(
|
|
Sprites.PLAYER,
|
|
) as SpriteSpec;
|
|
|
|
constructor() {
|
|
super(EntityNames.Player);
|
|
|
|
this.addComponent(
|
|
new BoundingBox(
|
|
{
|
|
x: 0,
|
|
y: 0,
|
|
},
|
|
{ width: Player.spriteSpec.width, height: Player.spriteSpec.height },
|
|
0,
|
|
),
|
|
);
|
|
|
|
this.addComponent(new Control());
|
|
|
|
this.addComponent(new Grid());
|
|
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
|
|
}
|
|
}
|