2023-08-13 18:47:58 -04:00
|
|
|
import { System, SystemNames } from ".";
|
|
|
|
import { Game } from "../Game";
|
2023-08-15 20:30:19 -04:00
|
|
|
import { ComponentNames, NetworkUpdateable } from "../components";
|
|
|
|
|
|
|
|
export interface MessageQueueProvider {
|
|
|
|
getNewMessages(): any[];
|
|
|
|
clearMessages(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MessagePublisher {
|
|
|
|
addMessage(message: any): void;
|
|
|
|
publish(): void;
|
|
|
|
}
|
2023-08-13 18:47:58 -04:00
|
|
|
|
|
|
|
export class NetworkUpdate extends System {
|
2023-08-15 20:30:19 -04:00
|
|
|
private queueProvider: MessageQueueProvider;
|
|
|
|
private publisher: MessagePublisher;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
queueProvider: MessageQueueProvider,
|
2023-08-21 19:22:23 -04:00
|
|
|
publisher: MessagePublisher
|
2023-08-15 20:30:19 -04:00
|
|
|
) {
|
2023-08-13 18:47:58 -04:00
|
|
|
super(SystemNames.NetworkUpdate);
|
2023-08-15 20:30:19 -04:00
|
|
|
|
|
|
|
this.queueProvider = queueProvider;
|
|
|
|
this.publisher = publisher;
|
2023-08-13 18:47:58 -04:00
|
|
|
}
|
|
|
|
|
2023-08-15 20:30:19 -04:00
|
|
|
public update(_dt: number, game: Game) {
|
|
|
|
const messages = this.queueProvider.getNewMessages();
|
2023-08-21 19:22:23 -04:00
|
|
|
if (messages.length) console.log(messages);
|
2023-08-15 20:30:19 -04:00
|
|
|
this.queueProvider.clearMessages();
|
|
|
|
|
|
|
|
game.forEachEntityWithComponent(
|
|
|
|
ComponentNames.NetworkUpdateable,
|
|
|
|
(entity) => {
|
|
|
|
const networkUpdateComponent = entity.getComponent<NetworkUpdateable>(
|
2023-08-21 19:22:23 -04:00
|
|
|
ComponentNames.NetworkUpdateable
|
2023-08-15 20:30:19 -04:00
|
|
|
);
|
2023-08-21 19:22:23 -04:00
|
|
|
}
|
2023-08-15 20:30:19 -04:00
|
|
|
);
|
|
|
|
}
|
2023-08-13 18:47:58 -04:00
|
|
|
}
|