summaryrefslogtreecommitdiff
path: root/engine/entities
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-08-26 17:55:27 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-08-26 17:55:27 -0600
commit6ce6946a4401d2ee6fa5cb747fab7d4c658a63c8 (patch)
treee60767dc5295edf379cf421e20171dc418e548b7 /engine/entities
parent594921352c8d82fe5f1a6201a4d5f9fbd9b719fc (diff)
downloadjumpstorm-6ce6946a4401d2ee6fa5cb747fab7d4c658a63c8.tar.gz
jumpstorm-6ce6946a4401d2ee6fa5cb747fab7d4c658a63c8.zip
add entity updates over network!
Diffstat (limited to 'engine/entities')
-rw-r--r--engine/entities/Entity.ts32
-rw-r--r--engine/entities/Floor.ts24
-rw-r--r--engine/entities/Player.ts44
3 files changed, 85 insertions, 15 deletions
diff --git a/engine/entities/Entity.ts b/engine/entities/Entity.ts
index b016fc0..63fb370 100644
--- a/engine/entities/Entity.ts
+++ b/engine/entities/Entity.ts
@@ -1,12 +1,15 @@
-import { EntityNames, Player } from '.';
-import type { Component } from '../components';
+import { EntityNames, Floor, Player } from '.';
+import { type Component } from '../components';
+
+const randomId = () =>
+ (performance.now() + Math.random() * 10_000_000).toString();
export abstract class Entity {
public id: string;
public components: Map<string, Component>;
public name: string;
- constructor(name: string, id: string = crypto.randomUUID()) {
+ constructor(name: string, id: string = randomId()) {
this.name = name;
this.id = id;
this.components = new Map();
@@ -31,14 +34,29 @@ export abstract class Entity {
return this.components.has(name);
}
- static from(entityName: string, args: any): Entity {
+ public static from(entityName: string, id: string, args: any): Entity {
+ let entity: Entity;
+
switch (entityName) {
case EntityNames.Player:
- const player = new Player(args.playerId);
- player.id = args.id;
- return player;
+ const player = new Player();
+ player.setFrom(args);
+ entity = player;
+ break;
+ case EntityNames.Floor:
+ const floor = new Floor(args.floorWidth);
+ floor.setFrom(args);
+ entity = floor;
+ break;
default:
throw new Error('.from() Entity type not implemented: ' + entityName);
}
+
+ entity.id = id;
+ return entity;
}
+
+ public abstract setFrom(args: Record<string, any>): void;
+
+ public abstract serialize(): Record<string, any>;
}
diff --git a/engine/entities/Floor.ts b/engine/entities/Floor.ts
index 6f9b13b..b4f48e5 100644
--- a/engine/entities/Floor.ts
+++ b/engine/entities/Floor.ts
@@ -1,5 +1,5 @@
import { IMAGES, SPRITE_SPECS, Sprites, type SpriteSpec } from '../config';
-import { BoundingBox, Sprite } from '../components';
+import { BoundingBox, ComponentNames, Sprite } from '../components';
import { TopCollidable } from '../components/TopCollidable';
import { Entity, EntityNames } from '../entities';
@@ -8,9 +8,13 @@ export class Floor extends Entity {
Sprites.FLOOR
) as SpriteSpec;
+ private width: number;
+
constructor(width: number) {
super(EntityNames.Floor);
+ this.width = width;
+
this.addComponent(
new Sprite(
IMAGES.get((Floor.spriteSpec?.states?.get(width) as SpriteSpec).sheet),
@@ -23,4 +27,22 @@ export class Floor extends Entity {
this.addComponent(new TopCollidable());
}
+
+ public serialize() {
+ return {
+ floorWidth: this.width,
+ boundingBox: this.getComponent<BoundingBox>(ComponentNames.BoundingBox)
+ };
+ }
+
+ public setFrom(args: any) {
+ const { boundingBox } = args;
+ this.addComponent(
+ new BoundingBox(
+ boundingBox.center,
+ boundingBox.dimension,
+ boundingBox.rotation
+ )
+ );
+ }
}
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));
}
}