jumpstorm/engine/entities/Player.ts

72 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-07-19 23:38:24 -04:00
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 { Direction } from "../interfaces";
export class Player extends Entity {
private static MASS: number = 10;
private static MOI: number = 1000;
2023-08-12 15:49:16 -04:00
private static spriteSpec: SpriteSpec = SPRITE_SPECS.get(
Sprites.COFFEE,
) as SpriteSpec;
2023-07-19 23:38:24 -04:00
constructor() {
super();
this.addComponent(
new BoundingBox(
{ x: 300, y: 100 },
{ width: Player.spriteSpec.width, height: Player.spriteSpec.height },
2023-08-12 15:49:16 -04:00
0,
),
2023-07-19 23:38:24 -04:00
);
this.addComponent(
new Velocity({ dCartesian: { dx: 0, dy: 0 }, dTheta: 0 }),
);
2023-07-19 23:38:24 -04:00
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(
2023-08-12 15:49:16 -04:00
IMAGES.get(Player.spriteSpec.states?.get(direction)?.sheet as string),
2023-07-19 23:38:24 -04:00
{ x: 0, y: 0 },
{ width: Player.spriteSpec.width, height: Player.spriteSpec.height },
Player.spriteSpec.msPerFrame,
2023-08-12 15:49:16 -04:00
Player.spriteSpec.frames,
),
2023-07-19 23:38:24 -04:00
);
this.addComponent(new FacingDirection(leftSprite, rightSprite));
this.addComponent(leftSprite); // face Left by default
}
}