From 6ce6946a4401d2ee6fa5cb747fab7d4c658a63c8 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sat, 26 Aug 2023 17:55:27 -0600 Subject: add entity updates over network! --- engine/entities/Entity.ts | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'engine/entities/Entity.ts') 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; 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): void; + + public abstract serialize(): Record; } -- cgit v1.2.3-70-g09d2