@esengine/network 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/LICENSE +21 -0
  2. package/dist/NetworkPlugin.d.ts +100 -0
  3. package/dist/NetworkPlugin.d.ts.map +1 -0
  4. package/dist/components/NetworkIdentity.d.ts +51 -0
  5. package/dist/components/NetworkIdentity.d.ts.map +1 -0
  6. package/dist/components/NetworkTransform.d.ts +71 -0
  7. package/dist/components/NetworkTransform.d.ts.map +1 -0
  8. package/dist/index.d.ts +23 -0
  9. package/dist/index.d.ts.map +1 -0
  10. package/dist/index.js +1348 -0
  11. package/dist/index.js.map +1 -0
  12. package/dist/nodes/NetworkNodes.d.ts +77 -0
  13. package/dist/nodes/NetworkNodes.d.ts.map +1 -0
  14. package/dist/nodes/index.d.ts +9 -0
  15. package/dist/nodes/index.d.ts.map +1 -0
  16. package/dist/services/NetworkService.d.ts +62 -0
  17. package/dist/services/NetworkService.d.ts.map +1 -0
  18. package/dist/sync/ClientPrediction.d.ts +158 -0
  19. package/dist/sync/ClientPrediction.d.ts.map +1 -0
  20. package/dist/sync/IInterpolator.d.ts +62 -0
  21. package/dist/sync/IInterpolator.d.ts.map +1 -0
  22. package/dist/sync/IStateSnapshot.d.ts +116 -0
  23. package/dist/sync/IStateSnapshot.d.ts.map +1 -0
  24. package/dist/sync/SnapshotBuffer.d.ts +58 -0
  25. package/dist/sync/SnapshotBuffer.d.ts.map +1 -0
  26. package/dist/sync/TransformInterpolator.d.ts +47 -0
  27. package/dist/sync/TransformInterpolator.d.ts.map +1 -0
  28. package/dist/sync/index.d.ts +15 -0
  29. package/dist/sync/index.d.ts.map +1 -0
  30. package/dist/systems/NetworkInputSystem.d.ts +32 -0
  31. package/dist/systems/NetworkInputSystem.d.ts.map +1 -0
  32. package/dist/systems/NetworkSpawnSystem.d.ts +37 -0
  33. package/dist/systems/NetworkSpawnSystem.d.ts.map +1 -0
  34. package/dist/systems/NetworkSyncSystem.d.ts +39 -0
  35. package/dist/systems/NetworkSyncSystem.d.ts.map +1 -0
  36. package/dist/tokens.d.ts +29 -0
  37. package/dist/tokens.d.ts.map +1 -0
  38. package/package.json +51 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 ESEngine Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,100 @@
1
+ import { type IPlugin, Core, type ServiceContainer } from '@esengine/ecs-framework';
2
+ import { NetworkService } from './services/NetworkService';
3
+ import { NetworkSyncSystem } from './systems/NetworkSyncSystem';
4
+ import { NetworkSpawnSystem, type PrefabFactory } from './systems/NetworkSpawnSystem';
5
+ import { NetworkInputSystem } from './systems/NetworkInputSystem';
6
+ /**
7
+ * 网络插件
8
+ * Network plugin
9
+ *
10
+ * 提供基于 TSRPC 的网络同步功能。
11
+ * Provides TSRPC-based network synchronization.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import { Core } from '@esengine/ecs-framework';
16
+ * import { NetworkPlugin } from '@esengine/network';
17
+ *
18
+ * const networkPlugin = new NetworkPlugin();
19
+ * await Core.installPlugin(networkPlugin);
20
+ *
21
+ * // 连接到服务器 | Connect to server
22
+ * await networkPlugin.connect('ws://localhost:3000', 'Player1');
23
+ *
24
+ * // 注册预制体 | Register prefab
25
+ * networkPlugin.registerPrefab('player', (scene, spawn) => {
26
+ * const entity = scene.createEntity('Player');
27
+ * return entity;
28
+ * });
29
+ * ```
30
+ */
31
+ export declare class NetworkPlugin implements IPlugin {
32
+ readonly name = "@esengine/network";
33
+ readonly version = "1.0.0";
34
+ private _networkService;
35
+ private _syncSystem;
36
+ private _spawnSystem;
37
+ private _inputSystem;
38
+ /**
39
+ * 网络服务
40
+ * Network service
41
+ */
42
+ get networkService(): NetworkService;
43
+ /**
44
+ * 同步系统
45
+ * Sync system
46
+ */
47
+ get syncSystem(): NetworkSyncSystem;
48
+ /**
49
+ * 生成系统
50
+ * Spawn system
51
+ */
52
+ get spawnSystem(): NetworkSpawnSystem;
53
+ /**
54
+ * 输入系统
55
+ * Input system
56
+ */
57
+ get inputSystem(): NetworkInputSystem;
58
+ /**
59
+ * 是否已连接
60
+ * Is connected
61
+ */
62
+ get isConnected(): boolean;
63
+ /**
64
+ * 安装插件
65
+ * Install plugin
66
+ */
67
+ install(_core: Core, _services: ServiceContainer): void;
68
+ /**
69
+ * 卸载插件
70
+ * Uninstall plugin
71
+ */
72
+ uninstall(): void;
73
+ private _setupSystems;
74
+ /**
75
+ * 连接到服务器
76
+ * Connect to server
77
+ */
78
+ connect(serverUrl: string, playerName: string, roomId?: string): Promise<boolean>;
79
+ /**
80
+ * 断开连接
81
+ * Disconnect
82
+ */
83
+ disconnect(): Promise<void>;
84
+ /**
85
+ * 注册预制体工厂
86
+ * Register prefab factory
87
+ */
88
+ registerPrefab(prefabType: string, factory: PrefabFactory): void;
89
+ /**
90
+ * 发送移动输入
91
+ * Send move input
92
+ */
93
+ sendMoveInput(x: number, y: number): void;
94
+ /**
95
+ * 发送动作输入
96
+ * Send action input
97
+ */
98
+ sendActionInput(action: string): void;
99
+ }
100
+ //# sourceMappingURL=NetworkPlugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NetworkPlugin.d.ts","sourceRoot":"","sources":["../src/NetworkPlugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,IAAI,EAAE,KAAK,gBAAgB,EAAc,MAAM,yBAAyB,CAAC;AAChG,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,KAAK,aAAa,EAAE,MAAM,8BAA8B,CAAC;AACtF,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAElE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,aAAc,YAAW,OAAO;IACzC,SAAgB,IAAI,uBAAuB;IAC3C,SAAgB,OAAO,WAAW;IAElC,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,YAAY,CAAsB;IAC1C,OAAO,CAAC,YAAY,CAAsB;IAE1C;;;OAGG;IACH,IAAI,cAAc,IAAI,cAAc,CAEnC;IAED;;;OAGG;IACH,IAAI,UAAU,IAAI,iBAAiB,CAElC;IAED;;;OAGG;IACH,IAAI,WAAW,IAAI,kBAAkB,CAEpC;IAED;;;OAGG;IACH,IAAI,WAAW,IAAI,kBAAkB,CAEpC;IAED;;;OAGG;IACH,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED;;;OAGG;IACH,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,gBAAgB,GAAG,IAAI;IAWvD;;;OAGG;IACH,SAAS,IAAI,IAAI;IAIjB,OAAO,CAAC,aAAa;IAUrB;;;OAGG;IACU,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI9F;;;OAGG;IACU,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxC;;;OAGG;IACI,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI;IAIvE;;;OAGG;IACI,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI;IAIhD;;;OAGG;IACI,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;CAG/C"}
@@ -0,0 +1,51 @@
1
+ import { Component } from '@esengine/ecs-framework';
2
+ /**
3
+ * 网络身份组件
4
+ * Network identity component
5
+ *
6
+ * 标识一个实体在网络上的唯一身份。
7
+ * Identifies an entity's unique identity on the network.
8
+ */
9
+ export declare class NetworkIdentity extends Component {
10
+ /**
11
+ * 网络实体 ID
12
+ * Network entity ID
13
+ */
14
+ netId: number;
15
+ /**
16
+ * 所有者客户端 ID
17
+ * Owner client ID
18
+ */
19
+ ownerId: number;
20
+ /**
21
+ * 是否为本地玩家拥有
22
+ * Is owned by local player
23
+ */
24
+ bIsLocalPlayer: boolean;
25
+ /**
26
+ * 是否有权限控制
27
+ * Has authority
28
+ */
29
+ bHasAuthority: boolean;
30
+ /**
31
+ * 预制体类型
32
+ * Prefab type
33
+ */
34
+ prefabType: string;
35
+ /**
36
+ * 同步间隔 (ms)
37
+ * Sync interval in milliseconds
38
+ */
39
+ syncInterval: number;
40
+ /**
41
+ * 上次同步时间
42
+ * Last sync time
43
+ */
44
+ lastSyncTime: number;
45
+ /**
46
+ * 检查是否需要同步
47
+ * Check if sync is needed
48
+ */
49
+ needsSync(now: number): boolean;
50
+ }
51
+ //# sourceMappingURL=NetworkIdentity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NetworkIdentity.d.ts","sourceRoot":"","sources":["../../src/components/NetworkIdentity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAmD,MAAM,yBAAyB,CAAC;AAErG;;;;;;GAMG;AACH,qBAEa,eAAgB,SAAQ,SAAS;IAC1C;;;OAGG;IAGI,KAAK,EAAE,MAAM,CAAK;IAEzB;;;OAGG;IAGI,OAAO,EAAE,MAAM,CAAK;IAE3B;;;OAGG;IACI,cAAc,EAAE,OAAO,CAAS;IAEvC;;;OAGG;IACI,aAAa,EAAE,OAAO,CAAS;IAEtC;;;OAGG;IAGI,UAAU,EAAE,MAAM,CAAM;IAE/B;;;OAGG;IAGI,YAAY,EAAE,MAAM,CAAO;IAElC;;;OAGG;IACI,YAAY,EAAE,MAAM,CAAK;IAEhC;;;OAGG;IACI,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;CAGzC"}
@@ -0,0 +1,71 @@
1
+ import { Component } from '@esengine/ecs-framework';
2
+ /**
3
+ * 网络变换组件
4
+ * Network transform component
5
+ *
6
+ * 同步实体的位置和旋转。支持插值平滑。
7
+ * Syncs entity position and rotation with interpolation smoothing.
8
+ */
9
+ export declare class NetworkTransform extends Component {
10
+ /**
11
+ * 目标位置 X
12
+ * Target position X
13
+ */
14
+ targetX: number;
15
+ /**
16
+ * 目标位置 Y
17
+ * Target position Y
18
+ */
19
+ targetY: number;
20
+ /**
21
+ * 目标旋转
22
+ * Target rotation
23
+ */
24
+ targetRotation: number;
25
+ /**
26
+ * 当前位置 X
27
+ * Current position X
28
+ */
29
+ currentX: number;
30
+ /**
31
+ * 当前位置 Y
32
+ * Current position Y
33
+ */
34
+ currentY: number;
35
+ /**
36
+ * 当前旋转
37
+ * Current rotation
38
+ */
39
+ currentRotation: number;
40
+ /**
41
+ * 插值速度
42
+ * Interpolation speed
43
+ */
44
+ lerpSpeed: number;
45
+ /**
46
+ * 是否启用插值
47
+ * Enable interpolation
48
+ */
49
+ bInterpolate: boolean;
50
+ /**
51
+ * 同步间隔 (ms)
52
+ * Sync interval in milliseconds
53
+ */
54
+ syncInterval: number;
55
+ /**
56
+ * 上次同步时间
57
+ * Last sync time
58
+ */
59
+ lastSyncTime: number;
60
+ /**
61
+ * 设置目标位置
62
+ * Set target position
63
+ */
64
+ setTarget(x: number, y: number, rotation?: number): void;
65
+ /**
66
+ * 立即跳转到目标位置
67
+ * Snap to target position immediately
68
+ */
69
+ snap(): void;
70
+ }
71
+ //# sourceMappingURL=NetworkTransform.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NetworkTransform.d.ts","sourceRoot":"","sources":["../../src/components/NetworkTransform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAmD,MAAM,yBAAyB,CAAC;AAErG;;;;;;GAMG;AACH,qBAEa,gBAAiB,SAAQ,SAAS;IAC3C;;;OAGG;IACI,OAAO,EAAE,MAAM,CAAK;IAE3B;;;OAGG;IACI,OAAO,EAAE,MAAM,CAAK;IAE3B;;;OAGG;IACI,cAAc,EAAE,MAAM,CAAK;IAElC;;;OAGG;IACI,QAAQ,EAAE,MAAM,CAAK;IAE5B;;;OAGG;IACI,QAAQ,EAAE,MAAM,CAAK;IAE5B;;;OAGG;IACI,eAAe,EAAE,MAAM,CAAK;IAEnC;;;OAGG;IAGI,SAAS,EAAE,MAAM,CAAM;IAE9B;;;OAGG;IAGI,YAAY,EAAE,OAAO,CAAQ;IAEpC;;;OAGG;IAGI,YAAY,EAAE,MAAM,CAAM;IAEjC;;;OAGG;IACI,YAAY,EAAE,MAAM,CAAK;IAEhC;;;OAGG;IACI,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI;IAQ/D;;;OAGG;IACI,IAAI,IAAI,IAAI;CAKtB"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @esengine/network
3
+ *
4
+ * 基于 TSRPC 的网络同步模块(客户端)
5
+ * TSRPC-based network synchronization module (client)
6
+ */
7
+ export type { ServiceType, Vec2, IEntityState, IPlayerInput, MsgSync, MsgInput, MsgSpawn, MsgDespawn, ReqJoin, ResJoin } from '@esengine/network-protocols';
8
+ export { serviceProto } from '@esengine/network-protocols';
9
+ export { NetworkServiceToken, NetworkSyncSystemToken, NetworkSpawnSystemToken, NetworkInputSystemToken } from './tokens';
10
+ export { NetworkPlugin } from './NetworkPlugin';
11
+ export { NetworkService, ENetworkState } from './services/NetworkService';
12
+ export type { INetworkCallbacks } from './services/NetworkService';
13
+ export { NetworkIdentity } from './components/NetworkIdentity';
14
+ export { NetworkTransform } from './components/NetworkTransform';
15
+ export { NetworkSyncSystem } from './systems/NetworkSyncSystem';
16
+ export { NetworkSpawnSystem } from './systems/NetworkSpawnSystem';
17
+ export type { PrefabFactory } from './systems/NetworkSpawnSystem';
18
+ export { NetworkInputSystem } from './systems/NetworkInputSystem';
19
+ export type { IStateSnapshot, ITransformState, ITransformStateWithVelocity, ISnapshotBufferConfig, ISnapshotBuffer } from './sync';
20
+ export type { IInterpolator, IExtrapolator, IInputSnapshot, IPredictedState, IPredictor, ClientPredictionConfig } from './sync';
21
+ export { lerp, lerpAngle, smoothDamp, SnapshotBuffer, createSnapshotBuffer, TransformInterpolator, HermiteTransformInterpolator, createTransformInterpolator, createHermiteTransformInterpolator, ClientPrediction, createClientPrediction } from './sync';
22
+ export { IsLocalPlayerTemplate, IsServerTemplate, HasAuthorityTemplate, GetNetworkIdTemplate, GetLocalPlayerIdTemplate, IsLocalPlayerExecutor, IsServerExecutor, HasAuthorityExecutor, GetNetworkIdExecutor, GetLocalPlayerIdExecutor, NetworkNodeDefinitions } from './nodes';
23
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,YAAY,EACR,WAAW,EACX,IAAI,EACJ,YAAY,EACZ,YAAY,EACZ,OAAO,EACP,QAAQ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