2023-08-25 18:48:17 -04:00
|
|
|
import { System, SystemNames } from '.';
|
|
|
|
import { Game } from '../Game';
|
|
|
|
import { ComponentNames, NetworkUpdateable } from '../components';
|
2023-08-23 21:44:59 -04:00
|
|
|
import {
|
|
|
|
type MessageQueueProvider,
|
|
|
|
type MessagePublisher,
|
2023-08-25 18:48:17 -04:00
|
|
|
type MessageProcessor
|
|
|
|
} from '../network';
|
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;
|
2023-08-23 21:44:59 -04:00
|
|
|
private messageProcessor: MessageProcessor;
|
2023-08-15 20:30:19 -04:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
queueProvider: MessageQueueProvider,
|
2023-08-23 21:44:59 -04:00
|
|
|
publisher: MessagePublisher,
|
2023-08-25 18:48:17 -04:00
|
|
|
messageProcessor: MessageProcessor
|
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-23 21:44:59 -04:00
|
|
|
this.messageProcessor = messageProcessor;
|
2023-08-13 18:47:58 -04:00
|
|
|
}
|
|
|
|
|
2023-08-15 20:30:19 -04:00
|
|
|
public update(_dt: number, game: Game) {
|
2023-08-23 21:44:59 -04:00
|
|
|
this.queueProvider
|
|
|
|
.getNewMessages()
|
|
|
|
.forEach((message) => this.messageProcessor.process(message));
|
2023-08-15 20:30:19 -04:00
|
|
|
this.queueProvider.clearMessages();
|
|
|
|
|
|
|
|
game.forEachEntityWithComponent(
|
|
|
|
ComponentNames.NetworkUpdateable,
|
|
|
|
(entity) => {
|
|
|
|
const networkUpdateComponent = entity.getComponent<NetworkUpdateable>(
|
2023-08-25 18:48:17 -04:00
|
|
|
ComponentNames.NetworkUpdateable
|
2023-08-15 20:30:19 -04:00
|
|
|
);
|
2023-08-25 18:48:17 -04:00
|
|
|
}
|
2023-08-15 20:30:19 -04:00
|
|
|
);
|
2023-08-23 21:44:59 -04:00
|
|
|
|
|
|
|
this.publisher.publish();
|
2023-08-15 20:30:19 -04:00
|
|
|
}
|
2023-08-13 18:47:58 -04:00
|
|
|
}
|