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 516a158..79a40b6 100644 --- a/examples/cocos-creator-multiplayer/backend/src/shared/game/GameSystem.ts +++ b/examples/cocos-creator-multiplayer/backend/src/shared/game/GameSystem.ts @@ -13,9 +13,12 @@ export interface GameSystemState { nextArrowId: number } +/** + * 前后端复用的状态计算模块 + */ export class GameSystem { - // State (Render Pull) + // 当前状态 private _state: GameSystemState = { now: 0, players: [], @@ -26,11 +29,12 @@ export class GameSystem { return this._state } + // 重设状态 reset(state: GameSystemState) { this._state = Object.merge({}, state); } - // Input + // 应用输入,计算状态变更 applyInput(input: GameSystemInput) { if (input.type === 'PlayerMove') { let player = this._state.players.find(v => v.id === input.playerId); 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 e0d90ea..18c15f6 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts +++ b/examples/cocos-creator-multiplayer/frontend/assets/scenes/GameScene/GameScene.ts @@ -159,12 +159,13 @@ export class GameScene extends Component { let sceneOffset = playerNode.forward.clone().normalize().multiplyScalar(gameConfig.arrowDistance); // 攻击落点(逻辑层坐标) let targetPos = new Vec2(playerState.pos.x, playerState.pos.y).add2f(sceneOffset.x, -sceneOffset.z); + // 发送输入 this.gameManager.sendClientInput({ type: 'PlayerAttack', // 显示坐标 —> 逻辑坐标 targetPos: { x: targetPos.x, y: targetPos.y }, targetTime: this.gameManager.state.now + gameConfig.arrowFlyTime - } as any) + }) // 冷却时间 1 秒 this.btnAttack.getComponent(Button)!.interactable = false; 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 256f65a..0f10abf 100644 --- a/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts +++ b/examples/cocos-creator-multiplayer/frontend/assets/scripts/models/GameManager.ts @@ -4,11 +4,16 @@ import { ClientInput, MsgClientInput } from "../shared/protocols/client/MsgClien import { MsgFrame } from "../shared/protocols/server/MsgFrame"; import { serviceProto, ServiceType } from "../shared/protocols/serviceProto"; +/** + * 前端游戏状态管理 + * 主要用于实现前端的预测和和解 + */ export class GameManager { client: WsClient; - + gameSystem = new GameSystem(); + lastServerState: GameSystemState = this.gameSystem.state; lastRecvSetverStateTime = 0; selfPlayerId: number = -1;