This commit is contained in:
k8w 2021-12-03 17:27:50 +08:00
parent 3597da5a69
commit 9e1f7830c1

View File

@ -69,15 +69,17 @@ export class GameManager {
} }
private _onServerSync(frame: MsgFrame) { private _onServerSync(frame: MsgFrame) {
// 同步权威状态 // 回滚至上一次的权威状态
this.gameSystem.reset(this.lastServerState); this.gameSystem.reset(this.lastServerState);
// 计算最新的权威状态
for (let input of frame.inputs) { for (let input of frame.inputs) {
this.gameSystem.applyInput(input); this.gameSystem.applyInput(input);
} }
this.lastServerState = Object.merge({}, this.gameSystem.state); this.lastServerState = Object.merge({}, this.gameSystem.state);
this.lastRecvSetverStateTime = Date.now(); this.lastRecvSetverStateTime = Date.now();
// 和解 // 和解 = 权威状态 + 本地输入 (最新的本地预测状态)
let lastSn = frame.lastSn ?? -1; let lastSn = frame.lastSn ?? -1;
this.pendingInputMsgs.remove(v => v.sn <= lastSn); this.pendingInputMsgs.remove(v => v.sn <= lastSn);
this.pendingInputMsgs.forEach(m => { this.pendingInputMsgs.forEach(m => {
@ -96,14 +98,17 @@ export class GameManager {
return; return;
} }
// 构造消息
let msg: MsgClientInput = { let msg: MsgClientInput = {
sn: ++this.lastSN, sn: ++this.lastSN,
inputs: [input] inputs: [input]
} }
// 向服务端发送输入
this.pendingInputMsgs.push(msg); this.pendingInputMsgs.push(msg);
this.client.sendMsg('client/ClientInput', msg); this.client.sendMsg('client/ClientInput', msg);
// 预测 // 预测状态:本地立即应用输入
this.gameSystem.applyInput({ this.gameSystem.applyInput({
...input, ...input,
playerId: this.selfPlayerId playerId: this.selfPlayerId