diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-08-15 18:30:19 -0600 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-08-15 18:30:19 -0600 |
commit | 732fe6f4811cc082bf938fed2d28c1f9c8bbd1f6 (patch) | |
tree | bf9d2aa160fb3e5564f4ae788ecc86927b716e0b /engine | |
parent | 2dc3120831fbcd03b635bbad59213ff0bf1f8879 (diff) | |
download | jumpstorm-732fe6f4811cc082bf938fed2d28c1f9c8bbd1f6.tar.gz jumpstorm-732fe6f4811cc082bf938fed2d28c1f9c8bbd1f6.zip |
generate uuids for entities; scaffolding for a server
Diffstat (limited to 'engine')
-rw-r--r-- | engine/Game.ts | 10 | ||||
-rw-r--r-- | engine/components/NetworkUpdateable.ts | 8 | ||||
-rw-r--r-- | engine/entities/Entity.ts | 6 | ||||
-rw-r--r-- | engine/structures/QuadTree.ts | 12 | ||||
-rw-r--r-- | engine/systems/NetworkUpdate.ts | 36 |
5 files changed, 52 insertions, 20 deletions
diff --git a/engine/Game.ts b/engine/Game.ts index 07d06e8..8dc5db7 100644 --- a/engine/Game.ts +++ b/engine/Game.ts @@ -7,9 +7,9 @@ export class Game { private running: boolean; private lastTimeStamp: number; - public entities: Map<number, Entity>; + public entities: Map<string, Entity>; public systems: Map<string, System>; - public componentEntities: Map<string, Set<number>>; + public componentEntities: Map<string, Set<string>>; constructor() { this.lastTimeStamp = performance.now(); @@ -29,11 +29,11 @@ export class Game { this.entities.set(entity.id, entity); } - public getEntity(id: number): Entity | undefined { + public getEntity(id: string): Entity | undefined { return this.entities.get(id); } - public removeEntity(id: number) { + public removeEntity(id: string) { this.entities.delete(id); } @@ -75,7 +75,7 @@ export class Game { if (!this.componentEntities.has(component.name)) { this.componentEntities.set( component.name, - new Set<number>([entity.id]), + new Set<string>([entity.id]), ); return; } diff --git a/engine/components/NetworkUpdateable.ts b/engine/components/NetworkUpdateable.ts index 73ceeba..980b064 100644 --- a/engine/components/NetworkUpdateable.ts +++ b/engine/components/NetworkUpdateable.ts @@ -1,7 +1,13 @@ import { Component, ComponentNames } from "."; export class NetworkUpdateable extends Component { - constructor() { + public isPublish: boolean; + public isSubscribe: boolean; + + constructor(isPublish: boolean, isSubscribe: boolean) { super(ComponentNames.NetworkUpdateable); + + this.isPublish = isPublish; + this.isSubscribe = isSubscribe; } } diff --git a/engine/entities/Entity.ts b/engine/entities/Entity.ts index ca8d314..b2d875d 100644 --- a/engine/entities/Entity.ts +++ b/engine/entities/Entity.ts @@ -1,13 +1,11 @@ import type { Component } from "../components"; export abstract class Entity { - private static ID = 0; - - public readonly id: number; + public readonly id: string; public readonly components: Map<string, Component>; constructor() { - this.id = Entity.ID++; + this.id = crypto.randomUUID(); this.components = new Map(); } diff --git a/engine/structures/QuadTree.ts b/engine/structures/QuadTree.ts index a57c6e7..49afdad 100644 --- a/engine/structures/QuadTree.ts +++ b/engine/structures/QuadTree.ts @@ -1,7 +1,7 @@ import type { Coord2D, Dimension2D } from "../interfaces"; interface BoxedEntry { - id: number; + id: string; dimension: Dimension2D; center: Coord2D; } @@ -72,8 +72,8 @@ export class QuadTree { } } - public getNeighborIds(boxedEntry: BoxedEntry): number[] { - const neighbors: number[] = this.objects.map(({ id }) => id); + public getNeighborIds(boxedEntry: BoxedEntry): string[] { + const neighbors: string[] = this.objects.map(({ id }) => id); if (this.hasChildren()) { this.getQuadrants(boxedEntry).forEach((quadrant) => { @@ -160,11 +160,7 @@ export class QuadTree { this.objects.forEach((boxedEntry) => { this.getQuadrants(boxedEntry).forEach((direction) => { const quadrant = this.children.get(direction); - quadrant?.insert( - boxedEntry.id, - boxedEntry.dimension, - boxedEntry.center, - ); + quadrant?.insert(boxedEntry); }); }); diff --git a/engine/systems/NetworkUpdate.ts b/engine/systems/NetworkUpdate.ts index dc7be20..6f8acb9 100644 --- a/engine/systems/NetworkUpdate.ts +++ b/engine/systems/NetworkUpdate.ts @@ -1,10 +1,42 @@ import { System, SystemNames } from "."; import { Game } from "../Game"; +import { ComponentNames, NetworkUpdateable } from "../components"; + +export interface MessageQueueProvider { + getNewMessages(): any[]; + clearMessages(): void; +} + +export interface MessagePublisher { + addMessage(message: any): void; + publish(): void; +} export class NetworkUpdate extends System { - constructor() { + private queueProvider: MessageQueueProvider; + private publisher: MessagePublisher; + + constructor( + queueProvider: MessageQueueProvider, + publisher: MessagePublisher, + ) { super(SystemNames.NetworkUpdate); + + this.queueProvider = queueProvider; + this.publisher = publisher; } - public update(_dt: number, _game: Game) {} + public update(_dt: number, game: Game) { + const messages = this.queueProvider.getNewMessages(); + this.queueProvider.clearMessages(); + + game.forEachEntityWithComponent( + ComponentNames.NetworkUpdateable, + (entity) => { + const networkUpdateComponent = entity.getComponent<NetworkUpdateable>( + ComponentNames.NetworkUpdateable, + ); + }, + ); + } } |