From c9ac4a6470531ffa7a0b06ad3daa5144bc6ea66d Mon Sep 17 00:00:00 2001 From: k8w Date: Fri, 3 Dec 2021 01:00:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=84=E7=AE=AD=E5=87=86=E7=A1=AE=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/src/shared/game/GameSystem.ts | 14 +++++++------- .../backend/src/shared/game/gameConfig.ts | 3 ++- .../src/shared/protocols/serviceProto.ts | 6 +++--- .../frontend/assets/prefabs/Arrow/Arrow.ts | 1 - .../assets/scenes/GameScene/GameScene.ts | 3 ++- .../assets/scripts/models/GameManager.ts | 17 +++++++++++++++++ 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/examples/cocos-creator-multiplayer/backend/src/shared/game/GameSystem.ts b/examples/cocos-creator-multiplayer/backend/src/shared/game/GameSystem.ts index 6a4621a..805b5ac 100644 --- a/examples/cocos-creator-multiplayer/backend/src/shared/game/GameSystem.ts +++ b/examples/cocos-creator-multiplayer/backend/src/shared/game/GameSystem.ts @@ -49,8 +49,8 @@ export class GameSystem { startPos: { ...player.pos }, startTime: this._state.now, targetPos: { - x: player.pos.x + input.direction.x * gameConfig.arrowDistance, - y: player.pos.y + input.direction.y * gameConfig.arrowDistance + x: player.pos.x + input.offset.x, + y: player.pos.y + input.offset.y }, targetTime: this._state.now + gameConfig.arrowFlyTime }); @@ -75,11 +75,11 @@ export class GameSystem { // 伤害判定 let damagedPlayers = this._state.players.filter(v => { // 不能伤害自己 - if (v.id === arrow.fromPlayerId) { - return false; - } + // if (v.id === arrow.fromPlayerId) { + // return false; + // } - return (v.pos.x - arrow.targetPos.x) * (v.pos.x - arrow.targetPos.x) + (v.pos.y - arrow.targetPos.y) * (v.pos.y - arrow.targetPos.y) <= gameConfig.arrowDistance * gameConfig.arrowDistance + return (v.pos.x - arrow.targetPos.x) * (v.pos.x - arrow.targetPos.x) + (v.pos.y - arrow.targetPos.y) * (v.pos.y - arrow.targetPos.y) <= gameConfig.arrowAttackRadius * gameConfig.arrowAttackRadius }); damagedPlayers.forEach(p => { // 设置击晕状态 @@ -112,7 +112,7 @@ export interface PlayerMove { export interface PlayerAttack { type: 'PlayerAttack', playerId: number, - direction: { x: number, y: number }, + offset: { x: number, y: number }, } export interface PlayerJoin { type: 'PlayerJoin', diff --git a/examples/cocos-creator-multiplayer/backend/src/shared/game/gameConfig.ts b/examples/cocos-creator-multiplayer/backend/src/shared/game/gameConfig.ts index 9f6b124..7f973d2 100644 --- a/examples/cocos-creator-multiplayer/backend/src/shared/game/gameConfig.ts +++ b/examples/cocos-creator-multiplayer/backend/src/shared/game/gameConfig.ts @@ -9,6 +9,7 @@ export const gameConfig = { moveSpeed: 10, arrowFlyTime: 500, - arrowDistance: 7, + arrowDistance: 8, + arrowAttackRadius: 3, arrowDizzyTime: 1000 } \ No newline at end of file diff --git a/examples/cocos-creator-multiplayer/backend/src/shared/protocols/serviceProto.ts b/examples/cocos-creator-multiplayer/backend/src/shared/protocols/serviceProto.ts index cf61964..b9fdcbb 100644 --- a/examples/cocos-creator-multiplayer/backend/src/shared/protocols/serviceProto.ts +++ b/examples/cocos-creator-multiplayer/backend/src/shared/protocols/serviceProto.ts @@ -17,7 +17,7 @@ export interface ServiceType { } export const serviceProto: ServiceProto = { - "version": 1, + "version": 2, "services": [ { "id": 0, @@ -159,8 +159,8 @@ export const serviceProto: ServiceProto = { } }, { - "id": 2, - "name": "direction", + "id": 3, + "name": "offset", "type": { "type": "Interface", "properties": [ diff --git a/examples/cocos-creator-multiplayer/frontend/assets/prefabs/Arrow/Arrow.ts b/examples/cocos-creator-multiplayer/frontend/assets/prefabs/Arrow/Arrow.ts index d615b46..6d8b8d7 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/prefabs/Arrow/Arrow.ts +++ b/examples/cocos-creator-multiplayer/frontend/assets/prefabs/Arrow/Arrow.ts @@ -22,7 +22,6 @@ export class Arrow extends Component { this._endPos.set(state.targetPos.x, 0, -state.targetPos.y); } - updateState(state: ArrowState, now: number) { let percent = MathUtil.limit((now - state.startTime) / (state.targetTime - state.startTime), 0, 1); 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 c089af8..39c38a6 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts +++ b/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts @@ -143,9 +143,10 @@ export class GameScene extends Component { } onBtnAttack() { + let offset = this._playerInstances[this.gameManager.selfPlayerId].node.forward.clone().normalize().multiplyScalar(gameConfig.arrowDistance); this.gameManager.sendClientInput({ type: 'PlayerAttack', - direction: this._playerInstances[this.gameManager.selfPlayerId].node.forward + offset: { x: offset.x, y: -offset.z } }) } 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 6ec3182..926360a 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts +++ b/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts @@ -11,6 +11,7 @@ export class GameManager { gameSystem = new GameSystem(); lastServerState: GameSystemState = this.gameSystem.state; + lastRecvSetverStateTime = 0; selfPlayerId: number = -1; lastSN = 0; @@ -67,6 +68,7 @@ export class GameManager { this.gameSystem.reset(ret.res.gameState); this.lastServerState = Object.merge(ret.res.gameState); + this.lastRecvSetverStateTime = Date.now(); this.selfPlayerId = ret.res.playerId; } @@ -77,6 +79,7 @@ export class GameManager { this.gameSystem.applyInput(input); } this.lastServerState = Object.merge({}, this.gameSystem.state); + this.lastRecvSetverStateTime = Date.now(); // 和解 let lastSn = frame.lastSn ?? -1; @@ -89,6 +92,13 @@ export class GameManager { }); }) }) + + // 本地时间流逝(会被下一次服务器状态覆盖) + this.gameSystem.applyInput({ + type: 'TimePast', + dt: Date.now() - this.lastRecvSetverStateTime + }); + this.lastRecvSetverStateTime = Date.now(); } pendingInputMsgs: MsgClientInput[] = []; @@ -109,6 +119,13 @@ export class GameManager { ...input, playerId: this.selfPlayerId }); + + // 本地时间流逝(会被下一次服务器状态覆盖) + // this.gameSystem.applyInput({ + // type: 'TimePast', + // dt: Date.now() - this.lastRecvSetverStateTime + // }); + // this.lastRecvSetverStateTime = Date.now(); } } \ No newline at end of file