feat: packetsToSend

This commit is contained in:
cha0s 2019-04-26 00:25:01 -05:00
parent f33906ef3d
commit 47d181c3c0

View File

@ -96,6 +96,7 @@ export class App extends decorate(class {}) {
// this.AugmentedParser = augmentParserWithThroughput(SocketIoParser); // this.AugmentedParser = augmentParserWithThroughput(SocketIoParser);
this.hasReceivedState = false; this.hasReceivedState = false;
this.isConnected = false; this.isConnected = false;
this.packetsToSend = [];
this.socket = undefined; this.socket = undefined;
// Simulation. // Simulation.
this.tps = new CycleTracker(config.simulationFrequency); this.tps = new CycleTracker(config.simulationFrequency);
@ -210,6 +211,26 @@ export class App extends decorate(class {}) {
return reactContainer; return reactContainer;
} }
sendPackets() {
// Merge.
const packetMergeMap = new Map();
for (let i = 0; i < this.packetsToSend.length; i++) {
const packet = this.packetsToSend[i];
const Packet = packet.constructor;
if (!packetMergeMap.has(Packet)) {
packetMergeMap.set(Packet, packet);
continue;
}
packetMergeMap.get(Packet).mergeWith(packet);
}
const packetList = Array.from(packetMergeMap.values());
for (let i = 0; i < packetList.length; i++) {
const packet = packetList[i];
this.socket.send(packet);
}
this.packetsToSend = [];
}
eventIsInUi(event) { eventIsInUi(event) {
let walk = event.target; let walk = event.target;
while (walk) { while (walk) {
@ -451,7 +472,7 @@ export class App extends decorate(class {}) {
this.inputHandle = setInterval(() => { this.inputHandle = setInterval(() => {
if (this.actionState !== this.actionRegistry.state) { if (this.actionState !== this.actionRegistry.state) {
this.actionState = this.actionRegistry.state; this.actionState = this.actionRegistry.state;
this.socket.send(InputPacket.fromState(this.actionState)); this.packetsToSend.push(InputPacket.fromState(this.actionState));
} }
}, 1000 * config.inputFrequency); }, 1000 * config.inputFrequency);
// Mouse/touch movement. // Mouse/touch movement.
@ -515,6 +536,8 @@ export class App extends decorate(class {}) {
// Tick synchronized. // Tick synchronized.
this.synchronizer.tick(elapsed); this.synchronizer.tick(elapsed);
this.state = this.synchronizer.state; this.state = this.synchronizer.state;
// Emit packets.
this.sendPackets();
// Sample. // Sample.
this.tps.sample(elapsed); this.tps.sample(elapsed);
}, 1000 * config.simulationFrequency); }, 1000 * config.simulationFrequency);