diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-08-26 17:55:27 -0600 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-08-26 17:55:27 -0600 |
commit | 6ce6946a4401d2ee6fa5cb747fab7d4c658a63c8 (patch) | |
tree | e60767dc5295edf379cf421e20171dc418e548b7 /engine/entities/Player.ts | |
parent | 594921352c8d82fe5f1a6201a4d5f9fbd9b719fc (diff) | |
download | jumpstorm-6ce6946a4401d2ee6fa5cb747fab7d4c658a63c8.tar.gz jumpstorm-6ce6946a4401d2ee6fa5cb747fab7d4c658a63c8.zip |
add entity updates over network!
Diffstat (limited to 'engine/entities/Player.ts')
-rw-r--r-- | engine/entities/Player.ts | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/engine/entities/Player.ts b/engine/entities/Player.ts index 947fbd6..4d91c6f 100644 --- a/engine/entities/Player.ts +++ b/engine/entities/Player.ts @@ -10,9 +10,10 @@ import { WallBounded, Forces, Collide, - Control, Mass, - Moment + Moment, + ComponentNames, + Control } from '../components'; import { Direction } from '../interfaces'; @@ -24,14 +25,14 @@ export class Player extends Entity { Sprites.COFFEE ) as SpriteSpec; - constructor(playerId: string) { + constructor() { super(EntityNames.Player); this.addComponent( new BoundingBox( { - x: 300, - y: 100 + x: 0, + y: 0 }, { width: Player.spriteSpec.width, height: Player.spriteSpec.height }, 0 @@ -48,7 +49,6 @@ export class Player extends Entity { this.addComponent(new Gravity()); this.addComponent(new Jump()); - this.addComponent(new Control(playerId)); this.addComponent(new Collide()); this.addComponent(new WallBounded()); @@ -69,6 +69,36 @@ export class Player extends Entity { ); this.addComponent(new FacingDirection(leftSprite, rightSprite)); - this.addComponent(leftSprite); // face Left by default + 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, velocity, forces, boundingBox } = args; + + 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) + ); + if (distance < 30) center = myCenter; + + [ + 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)); } } |