From 786d41484306edde024e860ab99d9515c26d7d27 Mon Sep 17 00:00:00 2001 From: King Wang Date: Mon, 6 Dec 2021 23:51:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/cocos-creator-multiplayer/backend/src/index.ts | 7 ++++--- .../cocos-creator-multiplayer/backend/src/models/Room.ts | 2 +- .../backend/src/shared/game/GameSystem.ts | 4 +++- .../backend/src/shared/game/gameConfig.ts | 4 ++++ .../backend/src/shared/protocols/PtlJoin.ts | 3 +++ .../backend/src/shared/protocols/client/MsgClientInput.ts | 1 + .../backend/src/shared/protocols/server/MsgFrame.ts | 5 +++++ 7 files changed, 21 insertions(+), 5 deletions(-) diff --git a/examples/cocos-creator-multiplayer/backend/src/index.ts b/examples/cocos-creator-multiplayer/backend/src/index.ts index 8c89268..b626a81 100644 --- a/examples/cocos-creator-multiplayer/backend/src/index.ts +++ b/examples/cocos-creator-multiplayer/backend/src/index.ts @@ -4,7 +4,7 @@ import { WsConnection, WsServer } from "tsrpc"; import { Room } from './models/Room'; import { serviceProto, ServiceType } from './shared/protocols/serviceProto'; -// Create the Server +// 创建 TSRPC WebSocket Server export const server = new WsServer(serviceProto, { port: 3000, json: true @@ -22,15 +22,16 @@ server.flows.postDisconnectFlow.push(v => { export const roomInstance = new Room(server); -// Initialize before server start +// 初始化 async function init() { + // 挂载 API 接口 await server.autoImplementApi(path.resolve(__dirname, 'api')); // TODO // Prepare something... (e.g. connect the db) }; -// Entry function +// 启动入口点 async function main() { await init(); await server.start(); diff --git a/examples/cocos-creator-multiplayer/backend/src/models/Room.ts b/examples/cocos-creator-multiplayer/backend/src/models/Room.ts index 36d03b8..487fb7c 100644 --- a/examples/cocos-creator-multiplayer/backend/src/models/Room.ts +++ b/examples/cocos-creator-multiplayer/backend/src/models/Room.ts @@ -9,7 +9,7 @@ import { ServiceType } from "../shared/protocols/serviceProto"; */ export class Room { - // 次数/秒 + // 帧同步频率,次数/秒 syncRate = gameConfig.syncRate; nextPlayerId = 1; 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 79a40b6..d12e9c0 100644 --- a/examples/cocos-creator-multiplayer/backend/src/shared/game/GameSystem.ts +++ b/examples/cocos-creator-multiplayer/backend/src/shared/game/GameSystem.ts @@ -2,6 +2,7 @@ import { gameConfig } from "./gameConfig"; import { ArrowState } from "./state/ArrowState"; import { PlayerState } from "./state/PlayerState"; +// 状态定义 export interface GameSystemState { // 当前的时间(游戏时间) now: number, @@ -123,11 +124,12 @@ export interface PlayerLeave { type: 'PlayerLeave', playerId: number } +// 时间流逝 export interface TimePast { type: 'TimePast', dt: number } - +// 输入定义 export type GameSystemInput = PlayerMove | PlayerAttack | 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 dcf3ef2..fad7761 100644 --- a/examples/cocos-creator-multiplayer/backend/src/shared/game/gameConfig.ts +++ b/examples/cocos-creator-multiplayer/backend/src/shared/game/gameConfig.ts @@ -3,8 +3,12 @@ export const gameConfig = { moveSpeed: 10, + // 箭矢飞行时间(毫秒) arrowFlyTime: 500, + // 箭矢投掷距离 arrowDistance: 8, + // 箭矢落地命中判定半径 arrowAttackRadius: 2, + // 被箭矢几种后的晕眩时间(毫秒) arrowDizzyTime: 1000 } \ No newline at end of file diff --git a/examples/cocos-creator-multiplayer/backend/src/shared/protocols/PtlJoin.ts b/examples/cocos-creator-multiplayer/backend/src/shared/protocols/PtlJoin.ts index 0f85d8a..63e07de 100644 --- a/examples/cocos-creator-multiplayer/backend/src/shared/protocols/PtlJoin.ts +++ b/examples/cocos-creator-multiplayer/backend/src/shared/protocols/PtlJoin.ts @@ -1,11 +1,14 @@ import { GameSystemState } from "../game/GameSystem"; +/** 加入房间 */ export interface ReqJoin { } export interface ResJoin { + /** 加入房间后,自己的 ID */ playerId: number, + /** 状态同步:一次性同步当前状态 */ gameState: GameSystemState } diff --git a/examples/cocos-creator-multiplayer/backend/src/shared/protocols/client/MsgClientInput.ts b/examples/cocos-creator-multiplayer/backend/src/shared/protocols/client/MsgClientInput.ts index b5cfba2..c22efad 100644 --- a/examples/cocos-creator-multiplayer/backend/src/shared/protocols/client/MsgClientInput.ts +++ b/examples/cocos-creator-multiplayer/backend/src/shared/protocols/client/MsgClientInput.ts @@ -1,5 +1,6 @@ import { PlayerAttack, PlayerMove } from "../../game/GameSystem"; +/** 发送自己的输入 */ export interface MsgClientInput { sn: number, inputs: ClientInput[] diff --git a/examples/cocos-creator-multiplayer/backend/src/shared/protocols/server/MsgFrame.ts b/examples/cocos-creator-multiplayer/backend/src/shared/protocols/server/MsgFrame.ts index 081e125..f45c277 100644 --- a/examples/cocos-creator-multiplayer/backend/src/shared/protocols/server/MsgFrame.ts +++ b/examples/cocos-creator-multiplayer/backend/src/shared/protocols/server/MsgFrame.ts @@ -1,6 +1,11 @@ import { GameSystemInput } from "../../game/GameSystem"; +/** + * 服务端定期广播的同步帧 + * 包含了这一段期间所有输入 + */ export interface MsgFrame { inputs: GameSystemInput[], + /** 当前用户提交的,经服务端确认的最后一条输入的 SN */ lastSn?: number } \ No newline at end of file