jumpstorm/engine/entities/Player.ts

106 lines
2.9 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,
Mass,
2023-08-26 19:55:27 -04:00
Moment,
ComponentNames,
Control
2023-08-25 18:48:17 -04:00
} 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-26 19:55:27 -04:00
constructor() {
2023-08-23 21:44:59 -04:00
super(EntityNames.Player);
2023-07-19 23:38:24 -04:00
this.addComponent(
new BoundingBox(
{
2023-08-26 19:55:27 -04:00
x: 0,
y: 0
},
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());
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));
2023-08-26 19:55:27 -04:00
this.addComponent(leftSprite); // face left by default
}
public serialize(): Record<string, any> {
return {
control: this.getComponent<Control>(ComponentNames.Control),
boundingBox: this.getComponent<BoundingBox>(ComponentNames.BoundingBox),
velocity: this.getComponent<Velocity>(ComponentNames.Velocity),
forces: this.getComponent<Forces>(ComponentNames.Forces)
};
}
public setFrom(args: Record<string, any>) {
const { control, forces, velocity, boundingBox } = args;
2023-08-26 19:55:27 -04:00
let center = boundingBox.center;
const myCenter = this.getComponent<BoundingBox>(
ComponentNames.BoundingBox
).center;
const distance = Math.sqrt(
Math.pow(center.y - myCenter.y, 2) + Math.pow(center.x - myCenter.x, 2)
);
const clientServerPredictionCenterThreshold = 30;
if (distance < clientServerPredictionCenterThreshold) center = myCenter;
2023-08-26 19:55:27 -04:00
[
Object.assign(new Control(control.controllableBy), control),
new Velocity(velocity.velocity),
new Forces(forces.forces),
new BoundingBox(center, boundingBox.dimension, boundingBox.rotation)
].forEach((component) => this.addComponent(component));
2023-07-19 23:38:24 -04:00
}
}