summaryrefslogtreecommitdiff
path: root/engine/entities/Entity.ts
diff options
context:
space:
mode:
authorElizabeth (Lizzy) Hunt <elizabeth.hunt@simponic.xyz>2023-08-26 17:57:05 -0600
committerGitHub <noreply@github.com>2023-08-26 17:57:05 -0600
commit8a4ab8d79b5ce1dabb431168398b5d5111fe326c (patch)
treee60767dc5295edf379cf421e20171dc418e548b7 /engine/entities/Entity.ts
parentc6e9baa0009f7cce0f6ff156a3957ef04a8cb684 (diff)
parent6ce6946a4401d2ee6fa5cb747fab7d4c658a63c8 (diff)
downloadjumpstorm-8a4ab8d79b5ce1dabb431168398b5d5111fe326c.tar.gz
jumpstorm-8a4ab8d79b5ce1dabb431168398b5d5111fe326c.zip
Merge pull request #1 from Simponic/network
Network
Diffstat (limited to 'engine/entities/Entity.ts')
-rw-r--r--engine/entities/Entity.ts46
1 files changed, 38 insertions, 8 deletions
diff --git a/engine/entities/Entity.ts b/engine/entities/Entity.ts
index ca8d314..63fb370 100644
--- a/engine/entities/Entity.ts
+++ b/engine/entities/Entity.ts
@@ -1,13 +1,17 @@
-import type { Component } from "../components";
+import { EntityNames, Floor, Player } from '.';
+import { type Component } from '../components';
-export abstract class Entity {
- private static ID = 0;
+const randomId = () =>
+ (performance.now() + Math.random() * 10_000_000).toString();
- public readonly id: number;
- public readonly components: Map<string, Component>;
+export abstract class Entity {
+ public id: string;
+ public components: Map<string, Component>;
+ public name: string;
- constructor() {
- this.id = Entity.ID++;
+ constructor(name: string, id: string = randomId()) {
+ this.name = name;
+ this.id = id;
this.components = new Map();
}
@@ -17,7 +21,7 @@ export abstract class Entity {
public getComponent<T extends Component>(name: string): T {
if (!this.hasComponent(name)) {
- throw new Error("Entity does not have component " + name);
+ throw new Error('Entity does not have component ' + name);
}
return this.components.get(name) as T;
}
@@ -29,4 +33,30 @@ export abstract class Entity {
public hasComponent(name: string): boolean {
return this.components.has(name);
}
+
+ public static from(entityName: string, id: string, args: any): Entity {
+ let entity: Entity;
+
+ switch (entityName) {
+ case EntityNames.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>;
}