feat(network): 基于 TSRPC 的网络同步模块 (#318)
- network-protocols: 共享协议包,使用 TSRPC CLI 生成完整类型验证 - network: 浏览器客户端,提供 NetworkPlugin、NetworkService 和同步系统 - network-server: Node.js 服务端,提供 GameServer 和房间管理
This commit is contained in:
70
packages/network/src/components/NetworkIdentity.ts
Normal file
70
packages/network/src/components/NetworkIdentity.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { Component, ECSComponent, Serialize, Serializable, Property } from '@esengine/ecs-framework';
|
||||
|
||||
/**
|
||||
* 网络身份组件
|
||||
* Network identity component
|
||||
*
|
||||
* 标识一个实体在网络上的唯一身份。
|
||||
* Identifies an entity's unique identity on the network.
|
||||
*/
|
||||
@ECSComponent('NetworkIdentity')
|
||||
@Serializable({ version: 1, typeId: 'NetworkIdentity' })
|
||||
export class NetworkIdentity extends Component {
|
||||
/**
|
||||
* 网络实体 ID
|
||||
* Network entity ID
|
||||
*/
|
||||
@Serialize()
|
||||
@Property({ type: 'integer', label: 'Net ID', readOnly: true })
|
||||
public netId: number = 0;
|
||||
|
||||
/**
|
||||
* 所有者客户端 ID
|
||||
* Owner client ID
|
||||
*/
|
||||
@Serialize()
|
||||
@Property({ type: 'integer', label: 'Owner ID', readOnly: true })
|
||||
public ownerId: number = 0;
|
||||
|
||||
/**
|
||||
* 是否为本地玩家拥有
|
||||
* Is owned by local player
|
||||
*/
|
||||
public bIsLocalPlayer: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否有权限控制
|
||||
* Has authority
|
||||
*/
|
||||
public bHasAuthority: boolean = false;
|
||||
|
||||
/**
|
||||
* 预制体类型
|
||||
* Prefab type
|
||||
*/
|
||||
@Serialize()
|
||||
@Property({ type: 'string', label: 'Prefab Type' })
|
||||
public prefabType: string = '';
|
||||
|
||||
/**
|
||||
* 同步间隔 (ms)
|
||||
* Sync interval in milliseconds
|
||||
*/
|
||||
@Serialize()
|
||||
@Property({ type: 'number', label: 'Sync Interval', min: 16 })
|
||||
public syncInterval: number = 100;
|
||||
|
||||
/**
|
||||
* 上次同步时间
|
||||
* Last sync time
|
||||
*/
|
||||
public lastSyncTime: number = 0;
|
||||
|
||||
/**
|
||||
* 检查是否需要同步
|
||||
* Check if sync is needed
|
||||
*/
|
||||
public needsSync(now: number): boolean {
|
||||
return now - this.lastSyncTime >= this.syncInterval;
|
||||
}
|
||||
}
|
||||
100
packages/network/src/components/NetworkTransform.ts
Normal file
100
packages/network/src/components/NetworkTransform.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import { Component, ECSComponent, Serialize, Serializable, Property } from '@esengine/ecs-framework';
|
||||
|
||||
/**
|
||||
* 网络变换组件
|
||||
* Network transform component
|
||||
*
|
||||
* 同步实体的位置和旋转。支持插值平滑。
|
||||
* Syncs entity position and rotation with interpolation smoothing.
|
||||
*/
|
||||
@ECSComponent('NetworkTransform', { requires: ['NetworkIdentity'] })
|
||||
@Serializable({ version: 1, typeId: 'NetworkTransform' })
|
||||
export class NetworkTransform extends Component {
|
||||
/**
|
||||
* 目标位置 X
|
||||
* Target position X
|
||||
*/
|
||||
public targetX: number = 0;
|
||||
|
||||
/**
|
||||
* 目标位置 Y
|
||||
* Target position Y
|
||||
*/
|
||||
public targetY: number = 0;
|
||||
|
||||
/**
|
||||
* 目标旋转
|
||||
* Target rotation
|
||||
*/
|
||||
public targetRotation: number = 0;
|
||||
|
||||
/**
|
||||
* 当前位置 X
|
||||
* Current position X
|
||||
*/
|
||||
public currentX: number = 0;
|
||||
|
||||
/**
|
||||
* 当前位置 Y
|
||||
* Current position Y
|
||||
*/
|
||||
public currentY: number = 0;
|
||||
|
||||
/**
|
||||
* 当前旋转
|
||||
* Current rotation
|
||||
*/
|
||||
public currentRotation: number = 0;
|
||||
|
||||
/**
|
||||
* 插值速度
|
||||
* Interpolation speed
|
||||
*/
|
||||
@Serialize()
|
||||
@Property({ type: 'number', label: 'Lerp Speed', min: 0.1, max: 50 })
|
||||
public lerpSpeed: number = 10;
|
||||
|
||||
/**
|
||||
* 是否启用插值
|
||||
* Enable interpolation
|
||||
*/
|
||||
@Serialize()
|
||||
@Property({ type: 'boolean', label: 'Interpolate' })
|
||||
public bInterpolate: boolean = true;
|
||||
|
||||
/**
|
||||
* 同步间隔 (ms)
|
||||
* Sync interval in milliseconds
|
||||
*/
|
||||
@Serialize()
|
||||
@Property({ type: 'number', label: 'Sync Interval', min: 16 })
|
||||
public syncInterval: number = 50;
|
||||
|
||||
/**
|
||||
* 上次同步时间
|
||||
* Last sync time
|
||||
*/
|
||||
public lastSyncTime: number = 0;
|
||||
|
||||
/**
|
||||
* 设置目标位置
|
||||
* Set target position
|
||||
*/
|
||||
public setTarget(x: number, y: number, rotation?: number): void {
|
||||
this.targetX = x;
|
||||
this.targetY = y;
|
||||
if (rotation !== undefined) {
|
||||
this.targetRotation = rotation;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 立即跳转到目标位置
|
||||
* Snap to target position immediately
|
||||
*/
|
||||
public snap(): void {
|
||||
this.currentX = this.targetX;
|
||||
this.currentY = this.targetY;
|
||||
this.currentRotation = this.targetRotation;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user