2024-03-07 22:49:43 -07:00

65 lines
1.6 KiB
TypeScript

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
}
}