feat(network): 网络模块增强 - 预测、AOI、增量压缩 (#379)
- 添加 NetworkPredictionSystem 客户端预测系统 - 添加 NetworkAOISystem 兴趣区域管理 - 添加 StateDeltaCompressor 状态增量压缩 - 添加断线重连和状态恢复 - 增强协议支持时间戳、序列号、速度 - 添加中英文文档
This commit is contained in:
@@ -17,12 +17,24 @@ import { rpc } from '@esengine/rpc'
|
||||
* @en Player input
|
||||
*/
|
||||
export interface PlayerInput {
|
||||
/**
|
||||
* @zh 输入序列号(用于客户端预测)
|
||||
* @en Input sequence number (for client prediction)
|
||||
*/
|
||||
seq: number
|
||||
|
||||
/**
|
||||
* @zh 帧序号
|
||||
* @en Frame number
|
||||
*/
|
||||
frame: number
|
||||
|
||||
/**
|
||||
* @zh 客户端时间戳
|
||||
* @en Client timestamp
|
||||
*/
|
||||
timestamp: number
|
||||
|
||||
/**
|
||||
* @zh 移动方向
|
||||
* @en Move direction
|
||||
@@ -41,9 +53,41 @@ export interface PlayerInput {
|
||||
* @en Entity sync state
|
||||
*/
|
||||
export interface EntitySyncState {
|
||||
/**
|
||||
* @zh 网络实体 ID
|
||||
* @en Network entity ID
|
||||
*/
|
||||
netId: number
|
||||
|
||||
/**
|
||||
* @zh 位置
|
||||
* @en Position
|
||||
*/
|
||||
pos?: { x: number; y: number }
|
||||
|
||||
/**
|
||||
* @zh 旋转角度
|
||||
* @en Rotation angle
|
||||
*/
|
||||
rot?: number
|
||||
|
||||
/**
|
||||
* @zh 速度(用于外推)
|
||||
* @en Velocity (for extrapolation)
|
||||
*/
|
||||
vel?: { x: number; y: number }
|
||||
|
||||
/**
|
||||
* @zh 角速度
|
||||
* @en Angular velocity
|
||||
*/
|
||||
angVel?: number
|
||||
|
||||
/**
|
||||
* @zh 自定义数据
|
||||
* @en Custom data
|
||||
*/
|
||||
custom?: Record<string, unknown>
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,6 +101,18 @@ export interface SyncData {
|
||||
*/
|
||||
frame: number
|
||||
|
||||
/**
|
||||
* @zh 服务器时间戳(用于插值)
|
||||
* @en Server timestamp (for interpolation)
|
||||
*/
|
||||
timestamp: number
|
||||
|
||||
/**
|
||||
* @zh 已确认的输入序列号(用于客户端预测校正)
|
||||
* @en Acknowledged input sequence (for client prediction reconciliation)
|
||||
*/
|
||||
ackSeq?: number
|
||||
|
||||
/**
|
||||
* @zh 实体状态列表
|
||||
* @en Entity state list
|
||||
@@ -84,6 +140,30 @@ export interface DespawnData {
|
||||
netId: number
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 完整状态快照(用于重连)
|
||||
* @en Full state snapshot (for reconnection)
|
||||
*/
|
||||
export interface FullStateData {
|
||||
/**
|
||||
* @zh 服务器帧号
|
||||
* @en Server frame number
|
||||
*/
|
||||
frame: number
|
||||
|
||||
/**
|
||||
* @zh 服务器时间戳
|
||||
* @en Server timestamp
|
||||
*/
|
||||
timestamp: number
|
||||
|
||||
/**
|
||||
* @zh 所有实体状态
|
||||
* @en All entity states
|
||||
*/
|
||||
entities: Array<SpawnData & { state?: EntitySyncState }>
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// API Types | API 类型
|
||||
// ============================================================================
|
||||
@@ -106,6 +186,54 @@ export interface JoinResponse {
|
||||
roomId: string
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 重连请求
|
||||
* @en Reconnect request
|
||||
*/
|
||||
export interface ReconnectRequest {
|
||||
/**
|
||||
* @zh 之前的玩家 ID
|
||||
* @en Previous player ID
|
||||
*/
|
||||
playerId: number
|
||||
|
||||
/**
|
||||
* @zh 房间 ID
|
||||
* @en Room ID
|
||||
*/
|
||||
roomId: string
|
||||
|
||||
/**
|
||||
* @zh 重连令牌
|
||||
* @en Reconnection token
|
||||
*/
|
||||
token: string
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 重连响应
|
||||
* @en Reconnect response
|
||||
*/
|
||||
export interface ReconnectResponse {
|
||||
/**
|
||||
* @zh 是否成功
|
||||
* @en Whether successful
|
||||
*/
|
||||
success: boolean
|
||||
|
||||
/**
|
||||
* @zh 完整状态(成功时)
|
||||
* @en Full state (when successful)
|
||||
*/
|
||||
state?: FullStateData
|
||||
|
||||
/**
|
||||
* @zh 错误信息(失败时)
|
||||
* @en Error message (when failed)
|
||||
*/
|
||||
error?: string
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Protocol Definition | 协议定义
|
||||
// ============================================================================
|
||||
@@ -145,6 +273,12 @@ export const gameProtocol = rpc.define({
|
||||
* @en Leave room
|
||||
*/
|
||||
leave: rpc.api<void, void>(),
|
||||
|
||||
/**
|
||||
* @zh 重连
|
||||
* @en Reconnect
|
||||
*/
|
||||
reconnect: rpc.api<ReconnectRequest, ReconnectResponse>(),
|
||||
},
|
||||
msg: {
|
||||
/**
|
||||
@@ -170,6 +304,12 @@ export const gameProtocol = rpc.define({
|
||||
* @en Entity despawn
|
||||
*/
|
||||
despawn: rpc.msg<DespawnData>(),
|
||||
|
||||
/**
|
||||
* @zh 完整状态快照
|
||||
* @en Full state snapshot
|
||||
*/
|
||||
fullState: rpc.msg<FullStateData>(),
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user