make server update interval an inheritable trait on entities

This commit is contained in:
Elizabeth Hunt 2023-08-29 12:26:58 -06:00
parent fd1bb1cca9
commit c551f519ca
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
4 changed files with 22 additions and 6 deletions

View File

@ -59,4 +59,6 @@ export abstract class Entity {
public abstract setFrom(args: Record<string, any>): void; public abstract setFrom(args: Record<string, any>): void;
public abstract serialize(): Record<string, any>; public abstract serialize(): Record<string, any>;
public abstract getNextUpdateInterval(): number;
} }

View File

@ -45,4 +45,8 @@ export class Floor extends Entity {
) )
); );
} }
public getNextUpdateInterval() {
return Math.random() * 500;
}
} }

View File

@ -102,4 +102,8 @@ export class Player extends Entity {
new BoundingBox(center, boundingBox.dimension, boundingBox.rotation) new BoundingBox(center, boundingBox.dimension, boundingBox.rotation)
].forEach((component) => this.addComponent(component)); ].forEach((component) => this.addComponent(component));
} }
public getNextUpdateInterval() {
return Math.random() * 30 + 50;
}
} }

View File

@ -16,9 +16,10 @@ export class NetworkUpdate extends System {
private queueProvider: MessageQueueProvider; private queueProvider: MessageQueueProvider;
private publisher: MessagePublisher; private publisher: MessagePublisher;
private messageProcessor: MessageProcessor; private messageProcessor: MessageProcessor;
private entityUpdateInfo: Map<string, EntityUpdateInfo>; private entityUpdateInfo: Map<string, EntityUpdateInfo>;
private nextPublishInterval: number;
constructor( constructor(
queueProvider: MessageQueueProvider, queueProvider: MessageQueueProvider,
publisher: MessagePublisher, publisher: MessagePublisher,
@ -31,6 +32,7 @@ export class NetworkUpdate extends System {
this.messageProcessor = messageProcessor; this.messageProcessor = messageProcessor;
this.entityUpdateInfo = new Map(); this.entityUpdateInfo = new Map();
this.nextPublishInterval = 0;
} }
public update(dt: number, game: Game) { public update(dt: number, game: Game) {
@ -69,7 +71,7 @@ export class NetworkUpdate extends System {
updateInfo.timer -= dt; updateInfo.timer -= dt;
this.entityUpdateInfo.set(entity.id, updateInfo); this.entityUpdateInfo.set(entity.id, updateInfo);
if (updateInfo.timer > 0) return; if (updateInfo.timer > 0) return;
updateInfo.timer = this.getNextUpdateTimeMs(); updateInfo.timer = entity.getNextUpdateInterval();
this.entityUpdateInfo.set(entity.id, updateInfo); this.entityUpdateInfo.set(entity.id, updateInfo);
// maybe update if hash is not consitent // maybe update if hash is not consitent
@ -92,11 +94,15 @@ export class NetworkUpdate extends System {
body: updateMessages body: updateMessages
}); });
// 3. publish changes // 3. maybe publish changes - we don't want to overload the socket
this.nextPublishInterval -= dt;
if (this.nextPublishInterval < 0) {
this.publisher.publish(); this.publisher.publish();
this.nextPublishInterval = this.getNextUpdateInterval();
}
} }
private getNextUpdateTimeMs() { private getNextUpdateInterval(): number {
return Math.random() * 30 + 50; return Math.random() * 30;
} }
} }