From c551f519cadb8628d58114512770c69b01d80a0c Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Tue, 29 Aug 2023 12:26:58 -0600 Subject: [PATCH] make server update interval an inheritable trait on entities --- engine/entities/Entity.ts | 2 ++ engine/entities/Floor.ts | 4 ++++ engine/entities/Player.ts | 4 ++++ engine/systems/NetworkUpdate.ts | 18 ++++++++++++------ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/engine/entities/Entity.ts b/engine/entities/Entity.ts index 63fb370..7078f79 100644 --- a/engine/entities/Entity.ts +++ b/engine/entities/Entity.ts @@ -59,4 +59,6 @@ export abstract class Entity { public abstract setFrom(args: Record): void; public abstract serialize(): Record; + + public abstract getNextUpdateInterval(): number; } diff --git a/engine/entities/Floor.ts b/engine/entities/Floor.ts index b4f48e5..f56c0d3 100644 --- a/engine/entities/Floor.ts +++ b/engine/entities/Floor.ts @@ -45,4 +45,8 @@ export class Floor extends Entity { ) ); } + + public getNextUpdateInterval() { + return Math.random() * 500; + } } diff --git a/engine/entities/Player.ts b/engine/entities/Player.ts index a7a41f8..b42fca4 100644 --- a/engine/entities/Player.ts +++ b/engine/entities/Player.ts @@ -102,4 +102,8 @@ export class Player extends Entity { new BoundingBox(center, boundingBox.dimension, boundingBox.rotation) ].forEach((component) => this.addComponent(component)); } + + public getNextUpdateInterval() { + return Math.random() * 30 + 50; + } } diff --git a/engine/systems/NetworkUpdate.ts b/engine/systems/NetworkUpdate.ts index a54be2e..f4c8f9c 100644 --- a/engine/systems/NetworkUpdate.ts +++ b/engine/systems/NetworkUpdate.ts @@ -16,9 +16,10 @@ export class NetworkUpdate extends System { private queueProvider: MessageQueueProvider; private publisher: MessagePublisher; private messageProcessor: MessageProcessor; - private entityUpdateInfo: Map; + private nextPublishInterval: number; + constructor( queueProvider: MessageQueueProvider, publisher: MessagePublisher, @@ -31,6 +32,7 @@ export class NetworkUpdate extends System { this.messageProcessor = messageProcessor; this.entityUpdateInfo = new Map(); + this.nextPublishInterval = 0; } public update(dt: number, game: Game) { @@ -69,7 +71,7 @@ export class NetworkUpdate extends System { updateInfo.timer -= dt; this.entityUpdateInfo.set(entity.id, updateInfo); if (updateInfo.timer > 0) return; - updateInfo.timer = this.getNextUpdateTimeMs(); + updateInfo.timer = entity.getNextUpdateInterval(); this.entityUpdateInfo.set(entity.id, updateInfo); // maybe update if hash is not consitent @@ -92,11 +94,15 @@ export class NetworkUpdate extends System { body: updateMessages }); - // 3. publish changes - this.publisher.publish(); + // 3. maybe publish changes - we don't want to overload the socket + this.nextPublishInterval -= dt; + if (this.nextPublishInterval < 0) { + this.publisher.publish(); + this.nextPublishInterval = this.getNextUpdateInterval(); + } } - private getNextUpdateTimeMs() { - return Math.random() * 30 + 50; + private getNextUpdateInterval(): number { + return Math.random() * 30; } }