From 50c897671205b4729be818daabff4f91419b5f2c Mon Sep 17 00:00:00 2001 From: k8w Date: Fri, 3 Dec 2021 01:11:51 +0800 Subject: [PATCH] timePast auto reconnect --- .../assets/scenes/GameScene/GameScene.ts | 2 ++ .../assets/scripts/models/GameManager.ts | 29 +++++++------------ 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts b/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts index 39c38a6..6718246 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts +++ b/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts @@ -69,6 +69,8 @@ export class GameScene extends Component { } update(dt: number) { + this.gameManager.localTimePast(); + // Send Inputs if (this._selfSpeed && this._selfSpeed.lengthSqr()) { this._selfSpeed.normalize().multiplyScalar(gameConfig.moveSpeed); 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 926360a..5dd50ca 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts +++ b/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts @@ -46,12 +46,8 @@ export class GameManager { if (!this.client.isConnected) { let resConnect = await this.client.connect(); if (!resConnect.isSucc) { - if (confirm('连接到服务器失败,是否重试')) { - return this.join(); - } - else { - return; - } + await new Promise(rs => { setTimeout(rs, 2000) }) + return this.join(); } } @@ -92,13 +88,6 @@ export class GameManager { }); }) }) - - // 本地时间流逝(会被下一次服务器状态覆盖) - this.gameSystem.applyInput({ - type: 'TimePast', - dt: Date.now() - this.lastRecvSetverStateTime - }); - this.lastRecvSetverStateTime = Date.now(); } pendingInputMsgs: MsgClientInput[] = []; @@ -119,13 +108,15 @@ export class GameManager { ...input, playerId: this.selfPlayerId }); + } - // 本地时间流逝(会被下一次服务器状态覆盖) - // this.gameSystem.applyInput({ - // type: 'TimePast', - // dt: Date.now() - this.lastRecvSetverStateTime - // }); - // this.lastRecvSetverStateTime = Date.now(); + // 本地时间流逝(会被下一次服务器状态覆盖) + localTimePast() { + this.gameSystem.applyInput({ + type: 'TimePast', + dt: Date.now() - this.lastRecvSetverStateTime + }); + this.lastRecvSetverStateTime = Date.now(); } } \ No newline at end of file