From 9e1f7830c166f289eebcc7dd1841168ebb365cbe Mon Sep 17 00:00:00 2001 From: k8w Date: Fri, 3 Dec 2021 17:27:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../frontend/assets/scripts/models/GameManager.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts b/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts index 4351a88..5d0c8dc 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts +++ b/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts @@ -69,15 +69,17 @@ export class GameManager { } private _onServerSync(frame: MsgFrame) { - // 同步权威状态 + // 回滚至上一次的权威状态 this.gameSystem.reset(this.lastServerState); + + // 计算最新的权威状态 for (let input of frame.inputs) { this.gameSystem.applyInput(input); } this.lastServerState = Object.merge({}, this.gameSystem.state); this.lastRecvSetverStateTime = Date.now(); - // 和解 + // 和解 = 权威状态 + 本地输入 (最新的本地预测状态) let lastSn = frame.lastSn ?? -1; this.pendingInputMsgs.remove(v => v.sn <= lastSn); this.pendingInputMsgs.forEach(m => { @@ -96,14 +98,17 @@ export class GameManager { return; } + // 构造消息 let msg: MsgClientInput = { sn: ++this.lastSN, inputs: [input] } + + // 向服务端发送输入 this.pendingInputMsgs.push(msg); this.client.sendMsg('client/ClientInput', msg); - // 预测 + // 预测状态:本地立即应用输入 this.gameSystem.applyInput({ ...input, playerId: this.selfPlayerId