jumpstorm/engine/entities/Player.ts

75 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-08-25 18:48:17 -04:00
import { Entity, EntityNames } from '.';
import { IMAGES, SPRITE_SPECS, Sprites, type SpriteSpec } from '../config';
2023-07-19 23:38:24 -04:00
import {
Jump,
FacingDirection,
BoundingBox,
Sprite,
Velocity,
Gravity,
WallBounded,
Forces,
Collide,
Control,
Mass,
2023-08-25 18:48:17 -04:00
Moment
} from '../components';
import { Direction } from '../interfaces';
2023-07-19 23:38:24 -04:00
export class Player extends Entity {
private static MASS: number = 10;
private static MOI: number = 100;
2023-07-19 23:38:24 -04:00
2023-08-12 15:49:16 -04:00
private static spriteSpec: SpriteSpec = SPRITE_SPECS.get(
2023-08-25 18:48:17 -04:00
Sprites.COFFEE
2023-08-12 15:49:16 -04:00
) as SpriteSpec;
2023-07-19 23:38:24 -04:00
2023-08-23 21:44:59 -04:00
constructor(playerId: string) {
super(EntityNames.Player);
2023-07-19 23:38:24 -04:00
this.addComponent(
new BoundingBox(
{
x: 300,
2023-08-25 18:48:17 -04:00
y: 100
},
2023-07-19 23:38:24 -04:00
{ width: Player.spriteSpec.width, height: Player.spriteSpec.height },
2023-08-25 18:48:17 -04:00
0
)
2023-07-19 23:38:24 -04:00
);
this.addComponent(
2023-08-25 18:48:17 -04:00
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());
2023-08-23 21:44:59 -04:00
this.addComponent(new Control(playerId));
2023-07-19 23:38:24 -04:00
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-25 18:48:17 -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
}
}