@nice2dev/game-engine 1.3.15 → 1.3.17
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.
- package/CHANGELOG.md +13 -0
- package/dist/cjs/accessibility/Accessibility.js +152 -165
- package/dist/cjs/ai/AI.js +292 -294
- package/dist/cjs/ai/BehaviorTree.js +1029 -1113
- package/dist/cjs/ai/StateMachine.js +694 -756
- package/dist/cjs/animation/Animation.js +232 -241
- package/dist/cjs/audio/AudioAdvanced.js +221 -236
- package/dist/cjs/audio/AudioBridge.js +441 -435
- package/dist/cjs/audio/AudioManager.js +242 -259
- package/dist/cjs/build-cloud/BuildOrchestration.js +1059 -1098
- package/dist/cjs/build-cloud/GodotBuildCloud.js +641 -670
- package/dist/cjs/build-cloud/UnityBuildCloud.js +563 -610
- package/dist/cjs/build-cloud/UnrealBuildCloud.js +610 -635
- package/dist/cjs/core/EventBus.js +51 -56
- package/dist/cjs/core/GameClock.js +62 -66
- package/dist/cjs/core/GameConfig.js +72 -79
- package/dist/cjs/core/GameLoop.js +80 -89
- package/dist/cjs/core/ServiceLocator.js +61 -65
- package/dist/cjs/core/Validation.js +72 -82
- package/dist/cjs/core/math.js +108 -112
- package/dist/cjs/devtools/DevTools.js +542 -563
- package/dist/cjs/devtools/DeveloperExperience.js +521 -559
- package/dist/cjs/devtools/GameplayAnalytics.js +636 -646
- package/dist/cjs/dialogue/DialogueSystem.js +984 -986
- package/dist/cjs/docs/DocGenerator.js +339 -364
- package/dist/cjs/ecs/World.js +355 -371
- package/dist/cjs/editor/AchievementEditor.js +580 -636
- package/dist/cjs/editor/AdvancedEditor.js +184 -191
- package/dist/cjs/editor/AnimationRetargeting.js +696 -741
- package/dist/cjs/editor/AssetBrowser.js +1072 -1160
- package/dist/cjs/editor/AssetManager.js +187 -192
- package/dist/cjs/editor/ConsolePro.js +971 -1061
- package/dist/cjs/editor/CraftingEditor.js +680 -730
- package/dist/cjs/editor/DebugTools.js +187 -185
- package/dist/cjs/editor/DialogueSystem.js +844 -901
- package/dist/cjs/editor/LeaderboardSystem.js +429 -483
- package/dist/cjs/editor/NiceGameEditor.js +1903 -1201
- package/dist/cjs/editor/PrefabSystem.js +772 -838
- package/dist/cjs/editor/ProfilerPanel.js +649 -700
- package/dist/cjs/editor/QuestEditor.js +792 -839
- package/dist/cjs/editor/ReplaySystem.js +585 -634
- package/dist/cjs/editor/SceneEditor.js +176 -200
- package/dist/cjs/editor/ScreenshotMode.js +621 -688
- package/dist/cjs/editor/ShaderGraph.js +1556 -1582
- package/dist/cjs/editor/TimelineEditor.js +715 -778
- package/dist/cjs/editor/UndoRedoPro.js +830 -904
- package/dist/cjs/editor/VRARMode.js +582 -645
- package/dist/cjs/engine/NiceGameEngine.js +141 -164
- package/dist/cjs/engine/SaveSystem.js +441 -495
- package/dist/cjs/enterprise/Enterprise.js +254 -261
- package/dist/cjs/export/BevyExporter.js +927 -967
- package/dist/cjs/export/CocosExporter.js +644 -673
- package/dist/cjs/export/Construct3Exporter.js +783 -794
- package/dist/cjs/export/DefoldExporter.js +1028 -1069
- package/dist/cjs/export/GDevelopExporter.js +674 -708
- package/dist/cjs/export/GameMakerExporter.js +791 -805
- package/dist/cjs/export/GodotExporter.js +1003 -1045
- package/dist/cjs/export/MinecraftBedrockExporter.js +573 -578
- package/dist/cjs/export/MonoGameExporter.js +1265 -1293
- package/dist/cjs/export/Pico8Exporter.js +766 -812
- package/dist/cjs/export/PlatformExporter.js +188 -206
- package/dist/cjs/export/RPGMakerExporter.js +860 -879
- package/dist/cjs/export/RobloxExporter.js +883 -919
- package/dist/cjs/export/Solar2DExporter.js +1218 -1256
- package/dist/cjs/export/ThreeJSExporter.js +1006 -1073
- package/dist/cjs/export/Tic80Exporter.js +1210 -1267
- package/dist/cjs/export/UnityExporter.js +1116 -1131
- package/dist/cjs/export/UnrealExporter.js +889 -919
- package/dist/cjs/export/WebExporter.js +931 -970
- package/dist/cjs/export/index.js +42 -65
- package/dist/cjs/godot-integration/GodotAssetLibrary.js +659 -692
- package/dist/cjs/godot-integration/GodotImport.js +929 -1027
- package/dist/cjs/godot-integration/GodotLiveLink.js +915 -945
- package/dist/cjs/godot-integration/components/GodotProjectBrowser.js +911 -611
- package/dist/cjs/i18n/I18n.js +860 -900
- package/dist/cjs/i18n/useTranslation.js +84 -89
- package/dist/cjs/import/AsepriteImporter.js +658 -716
- package/dist/cjs/import/DragonBonesImporter.js +437 -461
- package/dist/cjs/import/GameMakerImporter.js +511 -521
- package/dist/cjs/import/GodotSceneImporter.js +788 -822
- package/dist/cjs/import/LDtkImporter.js +436 -454
- package/dist/cjs/import/Live2DImporter.js +497 -518
- package/dist/cjs/import/NdgFormat.js +424 -457
- package/dist/cjs/import/OgmoImporter.js +466 -493
- package/dist/cjs/import/RPGMakerImporter.js +451 -477
- package/dist/cjs/import/SceneImporter.js +378 -423
- package/dist/cjs/import/SpineImporter.js +511 -546
- package/dist/cjs/import/SpriterImporter.js +590 -613
- package/dist/cjs/import/TiledMapImporter.js +811 -846
- package/dist/cjs/import/UnitySceneImporter.js +687 -703
- package/dist/cjs/import/index.js +206 -251
- package/dist/cjs/index.js +275 -278
- package/dist/cjs/input/GamepadNavigation.js +185 -205
- package/dist/cjs/input/InputManager.js +397 -421
- package/dist/cjs/input/useGamepads.js +70 -81
- package/dist/cjs/integration/IconSprite.js +129 -256
- package/dist/cjs/inventory/InventorySystem.js +920 -995
- package/dist/cjs/kids/KidMode.js +997 -1022
- package/dist/cjs/kids/KidTools.js +570 -594
- package/dist/cjs/monetization/Monetization.js +170 -177
- package/dist/cjs/multiplayer/LocalMultiplayer.js +287 -287
- package/dist/cjs/multiplayer/MiniGameTypes.js +49 -59
- package/dist/cjs/native-engine/AnimationSystem.js +731 -806
- package/dist/cjs/native-engine/AssetPipeline.js +454 -515
- package/dist/cjs/native-engine/AudioEngine.js +763 -804
- package/dist/cjs/native-engine/InputSystem.js +1075 -1145
- package/dist/cjs/native-engine/NetworkingSystem.js +733 -793
- package/dist/cjs/native-engine/PhysicsEngine.js +583 -607
- package/dist/cjs/native-engine/RenderingEngine.js +302 -350
- package/dist/cjs/native-engine/SceneGraph.js +769 -808
- package/dist/cjs/network/MultiplayerTransport.js +97 -132
- package/dist/cjs/network/Networking.js +549 -564
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/AbortController.js +21 -29
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/AccessTokenHttpClient.js +40 -48
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/DefaultHttpClient.js +35 -43
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/DefaultReconnectPolicy.js +10 -15
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Errors.js +103 -135
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/FetchHttpClient.js +131 -158
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HandshakeProtocol.js +39 -53
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HeaderNames.js +5 -8
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HttpClient.js +40 -48
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HttpConnection.js +494 -572
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HubConnection.js +826 -953
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HubConnectionBuilder.js +125 -146
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/IHubProtocol.js +13 -23
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ILogger.js +11 -25
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ITransport.js +13 -24
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/JsonHubProtocol.js +108 -121
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Loggers.js +10 -14
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/LongPollingTransport.js +150 -185
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/MessageBuffer.js +158 -196
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ServerSentEventsTransport.js +90 -105
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Subject.js +30 -35
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/TextMessageFormat.js +16 -21
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Utils.js +215 -248
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/WebSocketTransport.js +135 -157
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/XhrHttpClient.js +76 -85
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/index.js +24 -27
- package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/pkg-version.js +3 -4
- package/dist/cjs/pathfinding/Pathfinding.js +245 -263
- package/dist/cjs/performance/AssetStreaming.js +561 -635
- package/dist/cjs/performance/BenchmarkSuite.js +283 -311
- package/dist/cjs/performance/DeltaCompression.js +467 -526
- package/dist/cjs/performance/DeterministicPhysics.js +450 -498
- package/dist/cjs/performance/ECSOptimization.js +644 -695
- package/dist/cjs/performance/GameSaveSystem.js +509 -556
- package/dist/cjs/performance/HotReload.js +447 -500
- package/dist/cjs/performance/InputRecording.js +511 -561
- package/dist/cjs/performance/InterestManagement.js +453 -500
- package/dist/cjs/performance/LocalizationRuntime.js +376 -423
- package/dist/cjs/performance/MemoryManagement.js +410 -470
- package/dist/cjs/performance/ModSupport.js +445 -496
- package/dist/cjs/performance/MultiThreadedPhysics.js +400 -443
- package/dist/cjs/performance/NetworkPrediction.js +424 -482
- package/dist/cjs/performance/ObjectPooling.js +569 -617
- package/dist/cjs/performance/Performance.js +174 -190
- package/dist/cjs/performance/PerformanceAdvanced.js +614 -637
- package/dist/cjs/performance/WebGPURenderer.js +656 -683
- package/dist/cjs/physics/PhysicsAdvanced.js +345 -379
- package/dist/cjs/physics/PhysicsEngine2D.js +391 -427
- package/dist/cjs/plugins/PluginSDK.js +432 -466
- package/dist/cjs/plugins/PluginTestKit.js +307 -330
- package/dist/cjs/procedural/Procedural.js +243 -254
- package/dist/cjs/procedural/WorldBuilding.js +297 -314
- package/dist/cjs/quest/QuestSystem.js +975 -932
- package/dist/cjs/release/MobileExport.js +157 -163
- package/dist/cjs/release/Release.js +225 -238
- package/dist/cjs/rendering/Camera2D.js +121 -124
- package/dist/cjs/rendering/Renderer2D.js +321 -335
- package/dist/cjs/rendering/WebGPURenderPipeline.js +632 -651
- package/dist/cjs/runtime3d/Runtime3D.js +359 -378
- package/dist/cjs/runtime3d/SceneEditor3D.js +252 -263
- package/dist/cjs/scene/SceneManager.js +177 -193
- package/dist/cjs/scripting/GraphToAST.js +574 -547
- package/dist/cjs/scripting/LanguageExporter.js +254 -279
- package/dist/cjs/scripting/NodeGraph.js +546 -570
- package/dist/cjs/scripting/ScriptAST.js +31 -39
- package/dist/cjs/scripting/VisualScripting2.js +1016 -1065
- package/dist/cjs/scripting/exporters/CSharpExporter.js +464 -496
- package/dist/cjs/scripting/exporters/GDScriptExporter.js +390 -442
- package/dist/cjs/scripting/exporters/LuaExporter.js +406 -447
- package/dist/cjs/scripting/exporters/PythonExporter.js +484 -549
- package/dist/cjs/scripting/exporters/RustExporter.js +459 -512
- package/dist/cjs/scripting/exporters/TypeScriptExporter.js +505 -556
- package/dist/cjs/social/Social.js +72 -80
- package/dist/cjs/systems/LightingSystem.js +844 -931
- package/dist/cjs/systems/ParticleSystem2.js +1447 -1451
- package/dist/cjs/systems/PhysicsDebugSystem.js +1070 -1141
- package/dist/cjs/systems/TerrainSystem.js +985 -1098
- package/dist/cjs/templates/ActionTemplates.js +296 -309
- package/dist/cjs/templates/FightingTemplate.js +417 -463
- package/dist/cjs/templates/MetroidvaniaTemplate.js +353 -405
- package/dist/cjs/templates/PartyTemplates.js +190 -202
- package/dist/cjs/templates/PuzzleTemplates.js +245 -261
- package/dist/cjs/templates/RPGTemplates.js +160 -178
- package/dist/cjs/templates/SportsTemplates.js +237 -250
- package/dist/cjs/templates/StrategyTemplates.js +143 -153
- package/dist/cjs/templates/SurvivalTemplate.js +336 -380
- package/dist/cjs/templates/WaveDefense.js +499 -526
- package/dist/cjs/tilemap/Tilemap.js +207 -214
- package/dist/cjs/tutorials/TutorialBranching.js +396 -417
- package/dist/cjs/tutorials/TutorialEngine.js +888 -931
- package/dist/cjs/tutorials/TutorialOverlay.js +602 -640
- package/dist/cjs/tutorials/content/Tutorials_T05_T08.js +787 -809
- package/dist/cjs/tutorials/content/Tutorials_T09_T11.js +736 -756
- package/dist/cjs/tutorials/content/Tutorials_T12_T15.js +754 -778
- package/dist/cjs/ui/GameUI.js +295 -312
- package/dist/cjs/unity-integration/UnityAssetStore.js +700 -741
- package/dist/cjs/unity-integration/UnityImport.js +1066 -1176
- package/dist/cjs/unity-integration/UnityLiveLink.js +934 -981
- package/dist/cjs/unity-integration/components/UnityAssetStoreBrowser.js +1233 -757
- package/dist/cjs/unity-integration/components/UnityProjectBrowser.js +1240 -768
- package/dist/cjs/unreal-integration/UnrealImport.js +391 -412
- package/dist/cjs/unreal-integration/UnrealLiveLink.js +1102 -1137
- package/dist/cjs/unreal-integration/UnrealMarketplace.js +436 -473
- package/dist/cjs/unreal-integration/components/UnrealProjectBrowser.js +1150 -767
- package/dist/cjs/xr/ARVR.js +167 -175
- package/dist/esm/accessibility/Accessibility.js +160 -164
- package/dist/esm/ai/AI.js +310 -293
- package/dist/esm/ai/BehaviorTree.js +1057 -1112
- package/dist/esm/ai/StateMachine.js +709 -755
- package/dist/esm/animation/Animation.js +237 -240
- package/dist/esm/audio/AudioAdvanced.js +239 -235
- package/dist/esm/audio/AudioBridge.js +448 -434
- package/dist/esm/audio/AudioManager.js +243 -258
- package/dist/esm/build-cloud/BuildOrchestration.js +1071 -1097
- package/dist/esm/build-cloud/GodotBuildCloud.js +647 -669
- package/dist/esm/build-cloud/UnityBuildCloud.js +571 -609
- package/dist/esm/build-cloud/UnrealBuildCloud.js +615 -634
- package/dist/esm/core/EventBus.js +52 -55
- package/dist/esm/core/GameClock.js +63 -65
- package/dist/esm/core/GameConfig.js +77 -78
- package/dist/esm/core/GameLoop.js +81 -88
- package/dist/esm/core/ServiceLocator.js +63 -64
- package/dist/esm/core/Validation.js +82 -81
- package/dist/esm/core/math.js +109 -111
- package/dist/esm/devtools/DevTools.js +546 -562
- package/dist/esm/devtools/DeveloperExperience.js +535 -558
- package/dist/esm/devtools/GameplayAnalytics.js +647 -645
- package/dist/esm/dialogue/DialogueSystem.js +998 -985
- package/dist/esm/docs/DocGenerator.js +342 -363
- package/dist/esm/ecs/World.js +358 -370
- package/dist/esm/editor/AchievementEditor.js +584 -635
- package/dist/esm/editor/AdvancedEditor.js +191 -190
- package/dist/esm/editor/AnimationRetargeting.js +700 -740
- package/dist/esm/editor/AssetBrowser.js +1078 -1159
- package/dist/esm/editor/AssetManager.js +188 -191
- package/dist/esm/editor/ConsolePro.js +977 -1060
- package/dist/esm/editor/CraftingEditor.js +684 -729
- package/dist/esm/editor/DebugTools.js +194 -184
- package/dist/esm/editor/DialogueSystem.js +849 -900
- package/dist/esm/editor/LeaderboardSystem.js +433 -482
- package/dist/esm/editor/NiceGameEditor.js +1908 -1200
- package/dist/esm/editor/PrefabSystem.js +777 -837
- package/dist/esm/editor/ProfilerPanel.js +654 -699
- package/dist/esm/editor/QuestEditor.js +795 -838
- package/dist/esm/editor/ReplaySystem.js +591 -633
- package/dist/esm/editor/SceneEditor.js +189 -199
- package/dist/esm/editor/ScreenshotMode.js +628 -687
- package/dist/esm/editor/ShaderGraph.js +1565 -1581
- package/dist/esm/editor/TimelineEditor.js +733 -777
- package/dist/esm/editor/UndoRedoPro.js +835 -903
- package/dist/esm/editor/VRARMode.js +586 -644
- package/dist/esm/engine/NiceGameEngine.js +142 -163
- package/dist/esm/engine/SaveSystem.js +447 -494
- package/dist/esm/enterprise/Enterprise.js +265 -260
- package/dist/esm/export/BevyExporter.js +929 -966
- package/dist/esm/export/CocosExporter.js +646 -672
- package/dist/esm/export/Construct3Exporter.js +785 -793
- package/dist/esm/export/DefoldExporter.js +1030 -1068
- package/dist/esm/export/GDevelopExporter.js +676 -707
- package/dist/esm/export/GameMakerExporter.js +793 -804
- package/dist/esm/export/GodotExporter.js +1004 -1044
- package/dist/esm/export/MinecraftBedrockExporter.js +575 -577
- package/dist/esm/export/MonoGameExporter.js +1267 -1292
- package/dist/esm/export/Pico8Exporter.js +768 -811
- package/dist/esm/export/PlatformExporter.js +193 -205
- package/dist/esm/export/RPGMakerExporter.js +862 -878
- package/dist/esm/export/RobloxExporter.js +885 -918
- package/dist/esm/export/Solar2DExporter.js +1220 -1255
- package/dist/esm/export/ThreeJSExporter.js +1007 -1072
- package/dist/esm/export/Tic80Exporter.js +1212 -1266
- package/dist/esm/export/UnityExporter.js +1117 -1130
- package/dist/esm/export/UnrealExporter.js +891 -918
- package/dist/esm/export/WebExporter.js +933 -969
- package/dist/esm/export/index.js +86 -71
- package/dist/esm/godot-integration/GodotAssetLibrary.js +663 -691
- package/dist/esm/godot-integration/GodotImport.js +935 -1026
- package/dist/esm/godot-integration/GodotLiveLink.js +918 -944
- package/dist/esm/godot-integration/components/GodotProjectBrowser.js +912 -610
- package/dist/esm/i18n/I18n.js +871 -899
- package/dist/esm/i18n/useTranslation.js +88 -88
- package/dist/esm/import/AsepriteImporter.js +637 -715
- package/dist/esm/import/DragonBonesImporter.js +417 -460
- package/dist/esm/import/GameMakerImporter.js +491 -520
- package/dist/esm/import/GodotSceneImporter.js +767 -821
- package/dist/esm/import/LDtkImporter.js +415 -453
- package/dist/esm/import/Live2DImporter.js +477 -517
- package/dist/esm/import/NdgFormat.js +410 -456
- package/dist/esm/import/OgmoImporter.js +446 -492
- package/dist/esm/import/RPGMakerImporter.js +431 -476
- package/dist/esm/import/SceneImporter.js +385 -422
- package/dist/esm/import/SpineImporter.js +491 -545
- package/dist/esm/import/SpriterImporter.js +570 -612
- package/dist/esm/import/TiledMapImporter.js +790 -845
- package/dist/esm/import/UnitySceneImporter.js +666 -702
- package/dist/esm/import/index.js +247 -258
- package/dist/esm/index.js +1332 -188
- package/dist/esm/input/GamepadNavigation.js +187 -204
- package/dist/esm/input/InputManager.js +401 -420
- package/dist/esm/input/useGamepads.js +72 -80
- package/dist/esm/integration/IconSprite.js +143 -255
- package/dist/esm/inventory/InventorySystem.js +925 -994
- package/dist/esm/kids/KidMode.js +1003 -1021
- package/dist/esm/kids/KidTools.js +573 -593
- package/dist/esm/monetization/Monetization.js +180 -176
- package/dist/esm/multiplayer/LocalMultiplayer.js +290 -286
- package/dist/esm/multiplayer/MiniGameTypes.js +58 -58
- package/dist/esm/native-engine/AnimationSystem.js +735 -805
- package/dist/esm/native-engine/AssetPipeline.js +456 -514
- package/dist/esm/native-engine/AudioEngine.js +765 -803
- package/dist/esm/native-engine/InputSystem.js +1079 -1144
- package/dist/esm/native-engine/NetworkingSystem.js +740 -792
- package/dist/esm/native-engine/PhysicsEngine.js +585 -606
- package/dist/esm/native-engine/RenderingEngine.js +304 -349
- package/dist/esm/native-engine/SceneGraph.js +773 -807
- package/dist/esm/network/MultiplayerTransport.js +99 -131
- package/dist/esm/network/Networking.js +556 -563
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/AbortController.js +22 -28
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/AccessTokenHttpClient.js +41 -47
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/DefaultHttpClient.js +36 -42
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/DefaultReconnectPolicy.js +11 -14
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Errors.js +111 -134
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/FetchHttpClient.js +132 -157
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HandshakeProtocol.js +40 -52
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HeaderNames.js +6 -7
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HttpClient.js +42 -47
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HttpConnection.js +496 -571
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HubConnection.js +828 -952
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HubConnectionBuilder.js +126 -145
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/IHubProtocol.js +14 -23
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ILogger.js +12 -25
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ITransport.js +15 -24
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/JsonHubProtocol.js +109 -120
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Loggers.js +11 -13
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/LongPollingTransport.js +151 -184
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/MessageBuffer.js +159 -195
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ServerSentEventsTransport.js +91 -104
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Subject.js +31 -34
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/TextMessageFormat.js +17 -20
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Utils.js +230 -248
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/WebSocketTransport.js +136 -156
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/XhrHttpClient.js +77 -84
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/index.js +31 -12
- package/dist/esm/node_modules/@microsoft/signalr/dist/esm/pkg-version.js +4 -3
- package/dist/esm/pathfinding/Pathfinding.js +249 -262
- package/dist/esm/performance/AssetStreaming.js +567 -634
- package/dist/esm/performance/BenchmarkSuite.js +285 -310
- package/dist/esm/performance/DeltaCompression.js +478 -525
- package/dist/esm/performance/DeterministicPhysics.js +479 -497
- package/dist/esm/performance/ECSOptimization.js +653 -694
- package/dist/esm/performance/GameSaveSystem.js +515 -555
- package/dist/esm/performance/HotReload.js +454 -499
- package/dist/esm/performance/InputRecording.js +520 -560
- package/dist/esm/performance/InterestManagement.js +460 -499
- package/dist/esm/performance/LocalizationRuntime.js +382 -422
- package/dist/esm/performance/MemoryManagement.js +415 -469
- package/dist/esm/performance/ModSupport.js +455 -495
- package/dist/esm/performance/MultiThreadedPhysics.js +408 -442
- package/dist/esm/performance/NetworkPrediction.js +436 -481
- package/dist/esm/performance/ObjectPooling.js +582 -616
- package/dist/esm/performance/Performance.js +182 -189
- package/dist/esm/performance/PerformanceAdvanced.js +621 -636
- package/dist/esm/performance/WebGPURenderer.js +660 -682
- package/dist/esm/physics/PhysicsAdvanced.js +359 -378
- package/dist/esm/physics/PhysicsEngine2D.js +393 -426
- package/dist/esm/plugins/PluginSDK.js +435 -465
- package/dist/esm/plugins/PluginTestKit.js +315 -329
- package/dist/esm/procedural/Procedural.js +248 -253
- package/dist/esm/procedural/WorldBuilding.js +316 -313
- package/dist/esm/quest/QuestSystem.js +982 -931
- package/dist/esm/release/MobileExport.js +163 -162
- package/dist/esm/release/Release.js +234 -237
- package/dist/esm/rendering/Camera2D.js +122 -123
- package/dist/esm/rendering/Renderer2D.js +323 -334
- package/dist/esm/rendering/WebGPURenderPipeline.js +647 -650
- package/dist/esm/runtime3d/Runtime3D.js +368 -377
- package/dist/esm/runtime3d/SceneEditor3D.js +268 -262
- package/dist/esm/scene/SceneManager.js +178 -192
- package/dist/esm/scripting/GraphToAST.js +576 -546
- package/dist/esm/scripting/LanguageExporter.js +263 -278
- package/dist/esm/scripting/NodeGraph.js +550 -569
- package/dist/esm/scripting/ScriptAST.js +45 -38
- package/dist/esm/scripting/VisualScripting2.js +1025 -1064
- package/dist/esm/scripting/exporters/CSharpExporter.js +465 -495
- package/dist/esm/scripting/exporters/GDScriptExporter.js +391 -441
- package/dist/esm/scripting/exporters/LuaExporter.js +407 -446
- package/dist/esm/scripting/exporters/PythonExporter.js +485 -548
- package/dist/esm/scripting/exporters/RustExporter.js +460 -511
- package/dist/esm/scripting/exporters/TypeScriptExporter.js +506 -555
- package/dist/esm/social/Social.js +77 -79
- package/dist/esm/systems/LightingSystem.js +855 -930
- package/dist/esm/systems/ParticleSystem2.js +1453 -1450
- package/dist/esm/systems/PhysicsDebugSystem.js +1078 -1140
- package/dist/esm/systems/TerrainSystem.js +996 -1097
- package/dist/esm/templates/ActionTemplates.js +307 -308
- package/dist/esm/templates/FightingTemplate.js +439 -462
- package/dist/esm/templates/MetroidvaniaTemplate.js +391 -404
- package/dist/esm/templates/PartyTemplates.js +206 -201
- package/dist/esm/templates/PuzzleTemplates.js +260 -260
- package/dist/esm/templates/RPGTemplates.js +172 -177
- package/dist/esm/templates/SportsTemplates.js +251 -249
- package/dist/esm/templates/StrategyTemplates.js +158 -152
- package/dist/esm/templates/SurvivalTemplate.js +367 -379
- package/dist/esm/templates/WaveDefense.js +501 -525
- package/dist/esm/tilemap/Tilemap.js +209 -213
- package/dist/esm/tutorials/TutorialBranching.js +399 -416
- package/dist/esm/tutorials/TutorialEngine.js +893 -930
- package/dist/esm/tutorials/TutorialOverlay.js +603 -639
- package/dist/esm/tutorials/content/Tutorials_T05_T08.js +792 -808
- package/dist/esm/tutorials/content/Tutorials_T09_T11.js +740 -755
- package/dist/esm/tutorials/content/Tutorials_T12_T15.js +759 -777
- package/dist/esm/ui/GameUI.js +296 -311
- package/dist/esm/unity-integration/UnityAssetStore.js +704 -740
- package/dist/esm/unity-integration/UnityImport.js +1071 -1175
- package/dist/esm/unity-integration/UnityLiveLink.js +938 -980
- package/dist/esm/unity-integration/components/UnityAssetStoreBrowser.js +1234 -756
- package/dist/esm/unity-integration/components/UnityProjectBrowser.js +1241 -767
- package/dist/esm/unreal-integration/UnrealImport.js +395 -411
- package/dist/esm/unreal-integration/UnrealLiveLink.js +1105 -1136
- package/dist/esm/unreal-integration/UnrealMarketplace.js +442 -472
- package/dist/esm/unreal-integration/components/UnrealProjectBrowser.js +1151 -766
- package/dist/esm/xr/ARVR.js +181 -174
- package/package.json +7 -12
|
@@ -1,816 +1,764 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file NetworkingSystem.ts
|
|
3
|
-
* @description Networking System for Nice2Dev Game Engine
|
|
4
|
-
* PRO-1.11.6: Networking System
|
|
5
|
-
*
|
|
6
|
-
* Features:
|
|
7
|
-
* - WebSocket multiplayer
|
|
8
|
-
* - WebRTC peer-to-peer
|
|
9
|
-
* - State synchronization
|
|
10
|
-
* - Client-side prediction
|
|
11
|
-
* - Lag compensation
|
|
12
|
-
* - Matchmaking
|
|
13
|
-
* - Lobby system
|
|
14
|
-
*/
|
|
15
|
-
// ============================================================
|
|
16
|
-
// Network Manager
|
|
17
|
-
// ============================================================
|
|
18
1
|
class NetworkManager {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.messageHandlers.set('join-response', (msg) => {
|
|
74
|
-
clearTimeout(timeout);
|
|
75
|
-
const data = msg.data;
|
|
76
|
-
this.localPlayer = {
|
|
77
|
-
id: data.playerId,
|
|
78
|
-
name: '',
|
|
79
|
-
role: 'client',
|
|
80
|
-
ping: 0,
|
|
81
|
-
state: 'connected',
|
|
82
|
-
inputSequence: 0,
|
|
83
|
-
pendingInputs: [],
|
|
84
|
-
};
|
|
85
|
-
this.serverTimeOffset = data.serverTime - Date.now();
|
|
86
|
-
for (const player of data.players) {
|
|
87
|
-
this.players.set(player.id, player);
|
|
88
|
-
}
|
|
89
|
-
resolve();
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
async hostServer(port, playerName) {
|
|
94
|
-
this.connection = new WebSocketServer(port, this.config);
|
|
95
|
-
await this.connection.start();
|
|
2
|
+
constructor(config) {
|
|
3
|
+
this.connection = null;
|
|
4
|
+
this.players = /* @__PURE__ */ new Map();
|
|
5
|
+
this.localPlayer = null;
|
|
6
|
+
this.entities = /* @__PURE__ */ new Map();
|
|
7
|
+
this.snapshotBuffer = [];
|
|
8
|
+
this.currentTick = 0;
|
|
9
|
+
this.serverTimeOffset = 0;
|
|
10
|
+
this.callbacks = {};
|
|
11
|
+
this.messageHandlers = /* @__PURE__ */ new Map();
|
|
12
|
+
this.config = {
|
|
13
|
+
maxPlayers: 16,
|
|
14
|
+
tickRate: 60,
|
|
15
|
+
interpolationDelay: 100,
|
|
16
|
+
predictionEnabled: true,
|
|
17
|
+
lagCompensationWindow: 200,
|
|
18
|
+
reconciliationEnabled: true,
|
|
19
|
+
snapshotBufferSize: 120,
|
|
20
|
+
heartbeatInterval: 1e3,
|
|
21
|
+
timeoutDuration: 1e4,
|
|
22
|
+
compressionEnabled: true,
|
|
23
|
+
encryptionEnabled: false,
|
|
24
|
+
...config
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
// ─── Connection ────────────────────────────────────────
|
|
28
|
+
async connectToServer(url, playerName) {
|
|
29
|
+
if (this.connection) {
|
|
30
|
+
throw new Error("Already connected");
|
|
31
|
+
}
|
|
32
|
+
this.connection = new WebSocketConnection(url, this.config);
|
|
33
|
+
await this.connection.connect();
|
|
34
|
+
this.connection.onMessage((msg) => this.handleMessage(msg));
|
|
35
|
+
this.connection.onDisconnect((reason) => {
|
|
36
|
+
this.callbacks.onDisconnected?.(reason);
|
|
37
|
+
this.cleanup();
|
|
38
|
+
});
|
|
39
|
+
this.sendMessage({
|
|
40
|
+
type: "join-request",
|
|
41
|
+
channel: "system",
|
|
42
|
+
mode: "reliable",
|
|
43
|
+
data: { playerName }
|
|
44
|
+
});
|
|
45
|
+
await this.waitForJoinResponse();
|
|
46
|
+
this.callbacks.onConnected?.();
|
|
47
|
+
}
|
|
48
|
+
async waitForJoinResponse() {
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
const timeout = setTimeout(() => {
|
|
51
|
+
reject(new Error("Join timeout"));
|
|
52
|
+
}, this.config.timeoutDuration);
|
|
53
|
+
this.messageHandlers.set("join-response", (msg) => {
|
|
54
|
+
clearTimeout(timeout);
|
|
55
|
+
const data = msg.data;
|
|
96
56
|
this.localPlayer = {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
};
|
|
105
|
-
this.players.set('host', this.localPlayer);
|
|
106
|
-
}
|
|
107
|
-
disconnect() {
|
|
108
|
-
if (this.connection) {
|
|
109
|
-
this.sendMessage({
|
|
110
|
-
type: 'leave',
|
|
111
|
-
channel: 'system',
|
|
112
|
-
mode: 'reliable',
|
|
113
|
-
data: {},
|
|
114
|
-
});
|
|
115
|
-
this.connection.disconnect();
|
|
116
|
-
this.cleanup();
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
cleanup() {
|
|
120
|
-
this.connection = null;
|
|
121
|
-
this.players.clear();
|
|
122
|
-
this.localPlayer = null;
|
|
123
|
-
this.entities.clear();
|
|
124
|
-
this.snapshotBuffer = [];
|
|
125
|
-
this.currentTick = 0;
|
|
126
|
-
}
|
|
127
|
-
isConnected() {
|
|
128
|
-
return this.connection?.getState() === 'connected';
|
|
129
|
-
}
|
|
130
|
-
getConnectionState() {
|
|
131
|
-
return this.connection?.getState() ?? 'disconnected';
|
|
132
|
-
}
|
|
133
|
-
// ─── Players ───────────────────────────────────────────
|
|
134
|
-
getLocalPlayer() {
|
|
135
|
-
return this.localPlayer;
|
|
136
|
-
}
|
|
137
|
-
getPlayer(id) {
|
|
138
|
-
return this.players.get(id);
|
|
139
|
-
}
|
|
140
|
-
getPlayers() {
|
|
141
|
-
return Array.from(this.players.values());
|
|
142
|
-
}
|
|
143
|
-
getPlayerCount() {
|
|
144
|
-
return this.players.size;
|
|
145
|
-
}
|
|
146
|
-
// ─── Entities ──────────────────────────────────────────
|
|
147
|
-
registerEntity(entity) {
|
|
148
|
-
this.entities.set(entity.networkId, entity);
|
|
149
|
-
}
|
|
150
|
-
unregisterEntity(entity) {
|
|
151
|
-
this.entities.delete(entity.networkId);
|
|
152
|
-
}
|
|
153
|
-
getEntity(id) {
|
|
154
|
-
return this.entities.get(id);
|
|
155
|
-
}
|
|
156
|
-
isEntityOwner(entity) {
|
|
157
|
-
return entity.ownerId === this.localPlayer?.id;
|
|
158
|
-
}
|
|
159
|
-
// ─── Messages ──────────────────────────────────────────
|
|
160
|
-
sendMessage(message) {
|
|
161
|
-
if (!this.connection) {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
const fullMessage = {
|
|
165
|
-
...message,
|
|
166
|
-
timestamp: Date.now() + this.serverTimeOffset,
|
|
167
|
-
senderId: this.localPlayer?.id ?? 'unknown',
|
|
168
|
-
};
|
|
169
|
-
this.connection.send(fullMessage);
|
|
170
|
-
}
|
|
171
|
-
sendToPlayer(playerId, message) {
|
|
172
|
-
this.sendMessage({
|
|
173
|
-
...message,
|
|
174
|
-
data: { ...message.data, targetPlayerId: playerId },
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
broadcast(message, excludeSelf = false) {
|
|
178
|
-
this.sendMessage({
|
|
179
|
-
...message,
|
|
180
|
-
data: { ...message.data, broadcast: true, excludeSelf },
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
handleMessage(message) {
|
|
184
|
-
// Check for registered handler
|
|
185
|
-
const handler = this.messageHandlers.get(message.type);
|
|
186
|
-
if (handler) {
|
|
187
|
-
handler(message);
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
switch (message.type) {
|
|
191
|
-
case 'player-joined':
|
|
192
|
-
this.handlePlayerJoined(message);
|
|
193
|
-
break;
|
|
194
|
-
case 'player-left':
|
|
195
|
-
this.handlePlayerLeft(message);
|
|
196
|
-
break;
|
|
197
|
-
case 'state-sync':
|
|
198
|
-
this.handleStateSync(message);
|
|
199
|
-
break;
|
|
200
|
-
case 'ping':
|
|
201
|
-
this.handlePing(message);
|
|
202
|
-
break;
|
|
203
|
-
default:
|
|
204
|
-
this.callbacks.onMessage?.(message);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
handlePlayerJoined(message) {
|
|
208
|
-
const player = message.data;
|
|
209
|
-
this.players.set(player.id, player);
|
|
210
|
-
this.callbacks.onPlayerJoined?.(player);
|
|
211
|
-
}
|
|
212
|
-
handlePlayerLeft(message) {
|
|
213
|
-
const { playerId } = message.data;
|
|
214
|
-
const player = this.players.get(playerId);
|
|
215
|
-
if (player) {
|
|
216
|
-
this.players.delete(playerId);
|
|
217
|
-
this.callbacks.onPlayerLeft?.(player);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
handleStateSync(message) {
|
|
221
|
-
const snapshot = message.data;
|
|
222
|
-
// Add to buffer
|
|
223
|
-
this.snapshotBuffer.push(snapshot);
|
|
224
|
-
if (this.snapshotBuffer.length > this.config.snapshotBufferSize) {
|
|
225
|
-
this.snapshotBuffer.shift();
|
|
226
|
-
}
|
|
227
|
-
this.callbacks.onStateSync?.(snapshot);
|
|
228
|
-
}
|
|
229
|
-
handlePing(message) {
|
|
230
|
-
const { sendTime } = message.data;
|
|
231
|
-
const ping = Date.now() - sendTime;
|
|
232
|
-
if (this.localPlayer) {
|
|
233
|
-
this.localPlayer.ping = ping;
|
|
234
|
-
}
|
|
235
|
-
this.callbacks.onPingUpdate?.(ping);
|
|
236
|
-
}
|
|
237
|
-
// ─── Input ─────────────────────────────────────────────
|
|
238
|
-
sendInput(input) {
|
|
239
|
-
if (!this.localPlayer) {
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
const playerInput = {
|
|
243
|
-
sequence: ++this.localPlayer.inputSequence,
|
|
244
|
-
timestamp: Date.now() + this.serverTimeOffset,
|
|
245
|
-
actions: input,
|
|
57
|
+
id: data.playerId,
|
|
58
|
+
name: "",
|
|
59
|
+
role: "client",
|
|
60
|
+
ping: 0,
|
|
61
|
+
state: "connected",
|
|
62
|
+
inputSequence: 0,
|
|
63
|
+
pendingInputs: []
|
|
246
64
|
};
|
|
247
|
-
|
|
65
|
+
this.serverTimeOffset = data.serverTime - Date.now();
|
|
66
|
+
for (const player of data.players) {
|
|
67
|
+
this.players.set(player.id, player);
|
|
68
|
+
}
|
|
69
|
+
resolve();
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
async hostServer(port, playerName) {
|
|
74
|
+
this.connection = new WebSocketServer(port, this.config);
|
|
75
|
+
await this.connection.start();
|
|
76
|
+
this.localPlayer = {
|
|
77
|
+
id: "host",
|
|
78
|
+
name: playerName,
|
|
79
|
+
role: "host",
|
|
80
|
+
ping: 0,
|
|
81
|
+
state: "playing",
|
|
82
|
+
inputSequence: 0,
|
|
83
|
+
pendingInputs: []
|
|
84
|
+
};
|
|
85
|
+
this.players.set("host", this.localPlayer);
|
|
86
|
+
}
|
|
87
|
+
disconnect() {
|
|
88
|
+
if (this.connection) {
|
|
89
|
+
this.sendMessage({
|
|
90
|
+
type: "leave",
|
|
91
|
+
channel: "system",
|
|
92
|
+
mode: "reliable",
|
|
93
|
+
data: {}
|
|
94
|
+
});
|
|
95
|
+
this.connection.disconnect();
|
|
96
|
+
this.cleanup();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
cleanup() {
|
|
100
|
+
this.connection = null;
|
|
101
|
+
this.players.clear();
|
|
102
|
+
this.localPlayer = null;
|
|
103
|
+
this.entities.clear();
|
|
104
|
+
this.snapshotBuffer = [];
|
|
105
|
+
this.currentTick = 0;
|
|
106
|
+
}
|
|
107
|
+
isConnected() {
|
|
108
|
+
return this.connection?.getState() === "connected";
|
|
109
|
+
}
|
|
110
|
+
getConnectionState() {
|
|
111
|
+
return this.connection?.getState() ?? "disconnected";
|
|
112
|
+
}
|
|
113
|
+
// ─── Players ───────────────────────────────────────────
|
|
114
|
+
getLocalPlayer() {
|
|
115
|
+
return this.localPlayer;
|
|
116
|
+
}
|
|
117
|
+
getPlayer(id) {
|
|
118
|
+
return this.players.get(id);
|
|
119
|
+
}
|
|
120
|
+
getPlayers() {
|
|
121
|
+
return Array.from(this.players.values());
|
|
122
|
+
}
|
|
123
|
+
getPlayerCount() {
|
|
124
|
+
return this.players.size;
|
|
125
|
+
}
|
|
126
|
+
// ─── Entities ──────────────────────────────────────────
|
|
127
|
+
registerEntity(entity) {
|
|
128
|
+
this.entities.set(entity.networkId, entity);
|
|
129
|
+
}
|
|
130
|
+
unregisterEntity(entity) {
|
|
131
|
+
this.entities.delete(entity.networkId);
|
|
132
|
+
}
|
|
133
|
+
getEntity(id) {
|
|
134
|
+
return this.entities.get(id);
|
|
135
|
+
}
|
|
136
|
+
isEntityOwner(entity) {
|
|
137
|
+
return entity.ownerId === this.localPlayer?.id;
|
|
138
|
+
}
|
|
139
|
+
// ─── Messages ──────────────────────────────────────────
|
|
140
|
+
sendMessage(message) {
|
|
141
|
+
if (!this.connection) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const fullMessage = {
|
|
145
|
+
...message,
|
|
146
|
+
timestamp: Date.now() + this.serverTimeOffset,
|
|
147
|
+
senderId: this.localPlayer?.id ?? "unknown"
|
|
148
|
+
};
|
|
149
|
+
this.connection.send(fullMessage);
|
|
150
|
+
}
|
|
151
|
+
sendToPlayer(playerId, message) {
|
|
152
|
+
this.sendMessage({
|
|
153
|
+
...message,
|
|
154
|
+
data: { ...message.data, targetPlayerId: playerId }
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
broadcast(message, excludeSelf = false) {
|
|
158
|
+
this.sendMessage({
|
|
159
|
+
...message,
|
|
160
|
+
data: { ...message.data, broadcast: true, excludeSelf }
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
handleMessage(message) {
|
|
164
|
+
const handler = this.messageHandlers.get(message.type);
|
|
165
|
+
if (handler) {
|
|
166
|
+
handler(message);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
switch (message.type) {
|
|
170
|
+
case "player-joined":
|
|
171
|
+
this.handlePlayerJoined(message);
|
|
172
|
+
break;
|
|
173
|
+
case "player-left":
|
|
174
|
+
this.handlePlayerLeft(message);
|
|
175
|
+
break;
|
|
176
|
+
case "state-sync":
|
|
177
|
+
this.handleStateSync(message);
|
|
178
|
+
break;
|
|
179
|
+
case "ping":
|
|
180
|
+
this.handlePing(message);
|
|
181
|
+
break;
|
|
182
|
+
default:
|
|
183
|
+
this.callbacks.onMessage?.(message);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
handlePlayerJoined(message) {
|
|
187
|
+
const player = message.data;
|
|
188
|
+
this.players.set(player.id, player);
|
|
189
|
+
this.callbacks.onPlayerJoined?.(player);
|
|
190
|
+
}
|
|
191
|
+
handlePlayerLeft(message) {
|
|
192
|
+
const { playerId } = message.data;
|
|
193
|
+
const player = this.players.get(playerId);
|
|
194
|
+
if (player) {
|
|
195
|
+
this.players.delete(playerId);
|
|
196
|
+
this.callbacks.onPlayerLeft?.(player);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
handleStateSync(message) {
|
|
200
|
+
const snapshot = message.data;
|
|
201
|
+
this.snapshotBuffer.push(snapshot);
|
|
202
|
+
if (this.snapshotBuffer.length > this.config.snapshotBufferSize) {
|
|
203
|
+
this.snapshotBuffer.shift();
|
|
204
|
+
}
|
|
205
|
+
this.callbacks.onStateSync?.(snapshot);
|
|
206
|
+
}
|
|
207
|
+
handlePing(message) {
|
|
208
|
+
const { sendTime } = message.data;
|
|
209
|
+
const ping = Date.now() - sendTime;
|
|
210
|
+
if (this.localPlayer) {
|
|
211
|
+
this.localPlayer.ping = ping;
|
|
212
|
+
}
|
|
213
|
+
this.callbacks.onPingUpdate?.(ping);
|
|
214
|
+
}
|
|
215
|
+
// ─── Input ─────────────────────────────────────────────
|
|
216
|
+
sendInput(input) {
|
|
217
|
+
if (!this.localPlayer) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const playerInput = {
|
|
221
|
+
sequence: ++this.localPlayer.inputSequence,
|
|
222
|
+
timestamp: Date.now() + this.serverTimeOffset,
|
|
223
|
+
actions: input
|
|
224
|
+
};
|
|
225
|
+
if (this.config.predictionEnabled) {
|
|
226
|
+
this.localPlayer.pendingInputs.push(playerInput);
|
|
227
|
+
}
|
|
228
|
+
this.sendMessage({
|
|
229
|
+
type: "player-input",
|
|
230
|
+
channel: "gameplay",
|
|
231
|
+
mode: "reliable-ordered",
|
|
232
|
+
data: playerInput
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
// ─── State Synchronization ─────────────────────────────
|
|
236
|
+
update(_deltaTime) {
|
|
237
|
+
if (!this.connection) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
this.currentTick++;
|
|
241
|
+
if (this.currentTick % (this.config.tickRate * (this.config.heartbeatInterval / 1e3)) === 0) {
|
|
242
|
+
this.sendMessage({
|
|
243
|
+
type: "ping",
|
|
244
|
+
channel: "system",
|
|
245
|
+
mode: "unreliable",
|
|
246
|
+
data: { sendTime: Date.now() }
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
const renderTime = Date.now() - this.config.interpolationDelay + this.serverTimeOffset;
|
|
250
|
+
for (const entity of this.entities.values()) {
|
|
251
|
+
if (this.isEntityOwner(entity)) {
|
|
248
252
|
if (this.config.predictionEnabled) {
|
|
249
|
-
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
};
|
|
365
|
-
}
|
|
366
|
-
const ratioA = Math.sin((1 - t) * halfTheta) / sinHalfTheta;
|
|
367
|
-
const ratioB = Math.sin(t * halfTheta) / sinHalfTheta;
|
|
368
|
-
return {
|
|
369
|
-
x: a.x * ratioA + b.x * ratioB,
|
|
370
|
-
y: a.y * ratioA + b.y * ratioB,
|
|
371
|
-
z: a.z * ratioA + b.z * ratioB,
|
|
372
|
-
w: a.w * ratioA + b.w * ratioB,
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
// ─── Server Authority ──────────────────────────────────
|
|
376
|
-
reconcileState(serverSequence, serverState) {
|
|
377
|
-
if (!this.localPlayer || !this.config.reconciliationEnabled) {
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
// Remove acknowledged inputs
|
|
381
|
-
this.localPlayer.pendingInputs = this.localPlayer.pendingInputs.filter((input) => input.sequence > serverSequence);
|
|
382
|
-
// Get local entity
|
|
383
|
-
const entity = this.entities.get(serverState.id);
|
|
384
|
-
if (!entity || !this.isEntityOwner(entity)) {
|
|
385
|
-
return;
|
|
386
|
-
}
|
|
387
|
-
// Apply server state
|
|
388
|
-
entity.applyState(serverState);
|
|
389
|
-
// Re-apply pending inputs
|
|
390
|
-
for (const _input of this.localPlayer.pendingInputs) {
|
|
391
|
-
// Would re-simulate with input
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
// ─── Events ────────────────────────────────────────────
|
|
395
|
-
on(event, callback) {
|
|
396
|
-
this.callbacks[event] = callback;
|
|
397
|
-
}
|
|
398
|
-
off(event) {
|
|
399
|
-
delete this.callbacks[event];
|
|
400
|
-
}
|
|
401
|
-
// ─── Custom Message Types ──────────────────────────────
|
|
402
|
-
registerMessageHandler(type, handler) {
|
|
403
|
-
this.messageHandlers.set(type, handler);
|
|
404
|
-
}
|
|
405
|
-
unregisterMessageHandler(type) {
|
|
406
|
-
this.messageHandlers.delete(type);
|
|
407
|
-
}
|
|
253
|
+
this.applyPrediction(entity);
|
|
254
|
+
}
|
|
255
|
+
} else {
|
|
256
|
+
this.interpolateEntity(entity, renderTime);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
applyPrediction(_entity) {
|
|
261
|
+
if (!this.localPlayer) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
for (const _input of this.localPlayer.pendingInputs) {
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
interpolateEntity(entity, renderTime) {
|
|
268
|
+
let before = null;
|
|
269
|
+
let after = null;
|
|
270
|
+
for (const snapshot of this.snapshotBuffer) {
|
|
271
|
+
if (snapshot.timestamp <= renderTime) {
|
|
272
|
+
before = snapshot;
|
|
273
|
+
} else {
|
|
274
|
+
after = snapshot;
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (!before || !after) {
|
|
279
|
+
if (this.snapshotBuffer.length > 0) {
|
|
280
|
+
const latest = this.snapshotBuffer[this.snapshotBuffer.length - 1];
|
|
281
|
+
const entityState = latest.entities.find((e) => e.id === entity.networkId);
|
|
282
|
+
if (entityState) {
|
|
283
|
+
entity.applyState(entityState);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const t = (renderTime - before.timestamp) / (after.timestamp - before.timestamp);
|
|
289
|
+
const beforeState = before.entities.find((e) => e.id === entity.networkId);
|
|
290
|
+
const afterState = after.entities.find((e) => e.id === entity.networkId);
|
|
291
|
+
if (beforeState && afterState) {
|
|
292
|
+
const interpolated = {
|
|
293
|
+
id: entity.networkId,
|
|
294
|
+
ownerId: entity.ownerId,
|
|
295
|
+
type: entity.type,
|
|
296
|
+
position: this.lerpVec3(beforeState.position, afterState.position, t),
|
|
297
|
+
rotation: this.slerpQuat(beforeState.rotation, afterState.rotation, t),
|
|
298
|
+
velocity: beforeState.velocity && afterState.velocity ? this.lerpVec3(beforeState.velocity, afterState.velocity, t) : void 0
|
|
299
|
+
};
|
|
300
|
+
entity.applyState(interpolated);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
lerpVec3(a, b, t) {
|
|
304
|
+
return {
|
|
305
|
+
x: a.x + (b.x - a.x) * t,
|
|
306
|
+
y: a.y + (b.y - a.y) * t,
|
|
307
|
+
z: a.z + (b.z - a.z) * t
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
slerpQuat(a, b, t) {
|
|
311
|
+
let cosHalfTheta = a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z;
|
|
312
|
+
if (Math.abs(cosHalfTheta) >= 1) {
|
|
313
|
+
return a;
|
|
314
|
+
}
|
|
315
|
+
if (cosHalfTheta < 0) {
|
|
316
|
+
b = { x: -b.x, y: -b.y, z: -b.z, w: -b.w };
|
|
317
|
+
cosHalfTheta = -cosHalfTheta;
|
|
318
|
+
}
|
|
319
|
+
const halfTheta = Math.acos(cosHalfTheta);
|
|
320
|
+
const sinHalfTheta = Math.sqrt(1 - cosHalfTheta * cosHalfTheta);
|
|
321
|
+
if (Math.abs(sinHalfTheta) < 1e-3) {
|
|
322
|
+
return {
|
|
323
|
+
x: a.x * 0.5 + b.x * 0.5,
|
|
324
|
+
y: a.y * 0.5 + b.y * 0.5,
|
|
325
|
+
z: a.z * 0.5 + b.z * 0.5,
|
|
326
|
+
w: a.w * 0.5 + b.w * 0.5
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
const ratioA = Math.sin((1 - t) * halfTheta) / sinHalfTheta;
|
|
330
|
+
const ratioB = Math.sin(t * halfTheta) / sinHalfTheta;
|
|
331
|
+
return {
|
|
332
|
+
x: a.x * ratioA + b.x * ratioB,
|
|
333
|
+
y: a.y * ratioA + b.y * ratioB,
|
|
334
|
+
z: a.z * ratioA + b.z * ratioB,
|
|
335
|
+
w: a.w * ratioA + b.w * ratioB
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
// ─── Server Authority ──────────────────────────────────
|
|
339
|
+
reconcileState(serverSequence, serverState) {
|
|
340
|
+
if (!this.localPlayer || !this.config.reconciliationEnabled) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
this.localPlayer.pendingInputs = this.localPlayer.pendingInputs.filter(
|
|
344
|
+
(input) => input.sequence > serverSequence
|
|
345
|
+
);
|
|
346
|
+
const entity = this.entities.get(serverState.id);
|
|
347
|
+
if (!entity || !this.isEntityOwner(entity)) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
entity.applyState(serverState);
|
|
351
|
+
for (const _input of this.localPlayer.pendingInputs) {
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
// ─── Events ────────────────────────────────────────────
|
|
355
|
+
on(event, callback) {
|
|
356
|
+
this.callbacks[event] = callback;
|
|
357
|
+
}
|
|
358
|
+
off(event) {
|
|
359
|
+
delete this.callbacks[event];
|
|
360
|
+
}
|
|
361
|
+
// ─── Custom Message Types ──────────────────────────────
|
|
362
|
+
registerMessageHandler(type, handler) {
|
|
363
|
+
this.messageHandlers.set(type, handler);
|
|
364
|
+
}
|
|
365
|
+
unregisterMessageHandler(type) {
|
|
366
|
+
this.messageHandlers.delete(type);
|
|
367
|
+
}
|
|
408
368
|
}
|
|
409
|
-
// ============================================================
|
|
410
|
-
// Network Entity
|
|
411
|
-
// ============================================================
|
|
412
369
|
class NetworkEntity {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
370
|
+
constructor(networkId, ownerId, type) {
|
|
371
|
+
this.position = { x: 0, y: 0, z: 0 };
|
|
372
|
+
this.rotation = { x: 0, y: 0, z: 0, w: 1 };
|
|
373
|
+
this.velocity = { x: 0, y: 0, z: 0 };
|
|
374
|
+
this.customState = {};
|
|
375
|
+
this.networkId = networkId;
|
|
376
|
+
this.ownerId = ownerId;
|
|
377
|
+
this.type = type;
|
|
378
|
+
}
|
|
379
|
+
getState() {
|
|
380
|
+
return {
|
|
381
|
+
id: this.networkId,
|
|
382
|
+
ownerId: this.ownerId,
|
|
383
|
+
type: this.type,
|
|
384
|
+
position: { ...this.position },
|
|
385
|
+
rotation: { ...this.rotation },
|
|
386
|
+
velocity: { ...this.velocity },
|
|
387
|
+
customState: { ...this.customState }
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
applyState(state) {
|
|
391
|
+
this.position = { ...state.position };
|
|
392
|
+
this.rotation = { ...state.rotation };
|
|
393
|
+
if (state.velocity) {
|
|
394
|
+
this.velocity = { ...state.velocity };
|
|
395
|
+
}
|
|
396
|
+
if (state.customState) {
|
|
397
|
+
this.customState = { ...state.customState };
|
|
398
|
+
}
|
|
399
|
+
}
|
|
443
400
|
}
|
|
444
|
-
// ============================================================
|
|
445
|
-
// Connection Implementations
|
|
446
|
-
// ============================================================
|
|
447
401
|
class NetworkConnection {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
402
|
+
constructor() {
|
|
403
|
+
this.state = "disconnected";
|
|
404
|
+
this.messageCallback = null;
|
|
405
|
+
this.disconnectCallback = null;
|
|
406
|
+
}
|
|
407
|
+
getState() {
|
|
408
|
+
return this.state;
|
|
409
|
+
}
|
|
410
|
+
onMessage(callback) {
|
|
411
|
+
this.messageCallback = callback;
|
|
412
|
+
}
|
|
413
|
+
onDisconnect(callback) {
|
|
414
|
+
this.disconnectCallback = callback;
|
|
415
|
+
}
|
|
462
416
|
}
|
|
463
417
|
class WebSocketConnection extends NetworkConnection {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
418
|
+
constructor(url, config) {
|
|
419
|
+
super();
|
|
420
|
+
this.socket = null;
|
|
421
|
+
this.reconnectAttempts = 0;
|
|
422
|
+
this.maxReconnectAttempts = 5;
|
|
423
|
+
this.url = url;
|
|
424
|
+
this.config = config;
|
|
425
|
+
}
|
|
426
|
+
async connect() {
|
|
427
|
+
return new Promise((resolve, reject) => {
|
|
428
|
+
this.state = "connecting";
|
|
429
|
+
this.socket = new WebSocket(this.url);
|
|
430
|
+
this.socket.onopen = () => {
|
|
431
|
+
this.state = "connected";
|
|
467
432
|
this.reconnectAttempts = 0;
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
this.state = 'disconnected';
|
|
517
|
-
}
|
|
518
|
-
send(message) {
|
|
519
|
-
if (this.socket && this.state === 'connected') {
|
|
520
|
-
const data = this.config.compressionEnabled
|
|
521
|
-
? this.compress(JSON.stringify(message))
|
|
522
|
-
: JSON.stringify(message);
|
|
523
|
-
this.socket.send(data);
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
async start() {
|
|
527
|
-
// Client connection - use connect() instead
|
|
528
|
-
throw new Error('Use connect() for client connections');
|
|
529
|
-
}
|
|
530
|
-
compress(data) {
|
|
531
|
-
// Would implement compression
|
|
532
|
-
return data;
|
|
533
|
-
}
|
|
433
|
+
resolve();
|
|
434
|
+
};
|
|
435
|
+
this.socket.onmessage = (event) => {
|
|
436
|
+
try {
|
|
437
|
+
const message = JSON.parse(event.data);
|
|
438
|
+
this.messageCallback?.(message);
|
|
439
|
+
} catch (e) {
|
|
440
|
+
console.error("Failed to parse message:", e);
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
this.socket.onclose = (event) => {
|
|
444
|
+
this.state = "disconnected";
|
|
445
|
+
this.disconnectCallback?.(event.reason || "Connection closed");
|
|
446
|
+
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
|
447
|
+
this.reconnectAttempts++;
|
|
448
|
+
setTimeout(() => this.connect(), 1e3 * this.reconnectAttempts);
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
this.socket.onerror = () => {
|
|
452
|
+
this.state = "disconnected";
|
|
453
|
+
reject(new Error("WebSocket connection failed"));
|
|
454
|
+
};
|
|
455
|
+
setTimeout(() => {
|
|
456
|
+
if (this.state === "connecting") {
|
|
457
|
+
this.socket?.close();
|
|
458
|
+
reject(new Error("Connection timeout"));
|
|
459
|
+
}
|
|
460
|
+
}, this.config.timeoutDuration);
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
disconnect() {
|
|
464
|
+
this.state = "disconnecting";
|
|
465
|
+
this.socket?.close();
|
|
466
|
+
this.socket = null;
|
|
467
|
+
this.state = "disconnected";
|
|
468
|
+
}
|
|
469
|
+
send(message) {
|
|
470
|
+
if (this.socket && this.state === "connected") {
|
|
471
|
+
const data = this.config.compressionEnabled ? this.compress(JSON.stringify(message)) : JSON.stringify(message);
|
|
472
|
+
this.socket.send(data);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
async start() {
|
|
476
|
+
throw new Error("Use connect() for client connections");
|
|
477
|
+
}
|
|
478
|
+
compress(data) {
|
|
479
|
+
return data;
|
|
480
|
+
}
|
|
534
481
|
}
|
|
535
482
|
class WebSocketServer extends NetworkConnection {
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
client.send(JSON.stringify(message));
|
|
572
|
-
}
|
|
573
|
-
}
|
|
483
|
+
constructor(port, config) {
|
|
484
|
+
super();
|
|
485
|
+
this.clients = /* @__PURE__ */ new Map();
|
|
486
|
+
this.port = port;
|
|
487
|
+
this.config = config;
|
|
488
|
+
}
|
|
489
|
+
async connect() {
|
|
490
|
+
throw new Error("Use start() for server");
|
|
491
|
+
}
|
|
492
|
+
async start() {
|
|
493
|
+
this.state = "connected";
|
|
494
|
+
console.log(`Server started on port ${this.port}`);
|
|
495
|
+
}
|
|
496
|
+
disconnect() {
|
|
497
|
+
this.state = "disconnecting";
|
|
498
|
+
for (const client of this.clients.values()) {
|
|
499
|
+
client.close();
|
|
500
|
+
}
|
|
501
|
+
this.clients.clear();
|
|
502
|
+
this.state = "disconnected";
|
|
503
|
+
}
|
|
504
|
+
send(message) {
|
|
505
|
+
const data = JSON.stringify(message);
|
|
506
|
+
for (const client of this.clients.values()) {
|
|
507
|
+
if (client.readyState === WebSocket.OPEN) {
|
|
508
|
+
client.send(data);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
sendToClient(clientId, message) {
|
|
513
|
+
const client = this.clients.get(clientId);
|
|
514
|
+
if (client && client.readyState === WebSocket.OPEN) {
|
|
515
|
+
client.send(JSON.stringify(message));
|
|
516
|
+
}
|
|
517
|
+
}
|
|
574
518
|
}
|
|
575
519
|
class LobbyManager {
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
520
|
+
constructor(network) {
|
|
521
|
+
this.currentLobby = null;
|
|
522
|
+
this.availableLobbies = [];
|
|
523
|
+
this.callbacks = {};
|
|
524
|
+
this.network = network;
|
|
525
|
+
this.setupMessageHandlers();
|
|
526
|
+
}
|
|
527
|
+
setupMessageHandlers() {
|
|
528
|
+
this.network.registerMessageHandler("lobby-created", (msg) => {
|
|
529
|
+
const lobby = msg.data;
|
|
530
|
+
this.currentLobby = lobby;
|
|
531
|
+
this.callbacks.onLobbyCreated?.(lobby);
|
|
532
|
+
});
|
|
533
|
+
this.network.registerMessageHandler("lobby-joined", (msg) => {
|
|
534
|
+
const lobby = msg.data;
|
|
535
|
+
this.currentLobby = lobby;
|
|
536
|
+
this.callbacks.onLobbyJoined?.(lobby);
|
|
537
|
+
});
|
|
538
|
+
this.network.registerMessageHandler("lobby-player-joined", (msg) => {
|
|
539
|
+
const player = msg.data;
|
|
540
|
+
if (this.currentLobby) {
|
|
541
|
+
this.currentLobby.players.push(player);
|
|
542
|
+
this.callbacks.onPlayerJoined?.(player);
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
this.network.registerMessageHandler("lobby-player-left", (msg) => {
|
|
546
|
+
const { playerId } = msg.data;
|
|
547
|
+
if (this.currentLobby) {
|
|
548
|
+
const idx = this.currentLobby.players.findIndex((p) => p.id === playerId);
|
|
549
|
+
if (idx >= 0) {
|
|
550
|
+
const player = this.currentLobby.players.splice(idx, 1)[0];
|
|
551
|
+
this.callbacks.onPlayerLeft?.(player);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
});
|
|
555
|
+
this.network.registerMessageHandler("lobby-list", (msg) => {
|
|
556
|
+
this.availableLobbies = msg.data;
|
|
557
|
+
});
|
|
558
|
+
this.network.registerMessageHandler("game-starting", () => {
|
|
559
|
+
if (this.currentLobby) {
|
|
560
|
+
this.currentLobby.state = "starting";
|
|
561
|
+
this.callbacks.onGameStarted?.();
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
// ─── Lobby Management ──────────────────────────────────
|
|
566
|
+
createLobby(config) {
|
|
567
|
+
this.network.sendMessage({
|
|
568
|
+
type: "create-lobby",
|
|
569
|
+
channel: "system",
|
|
570
|
+
mode: "reliable",
|
|
571
|
+
data: config
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
joinLobby(lobbyId, password) {
|
|
575
|
+
this.network.sendMessage({
|
|
576
|
+
type: "join-lobby",
|
|
577
|
+
channel: "system",
|
|
578
|
+
mode: "reliable",
|
|
579
|
+
data: { lobbyId, password }
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
leaveLobby() {
|
|
583
|
+
if (!this.currentLobby) {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
this.network.sendMessage({
|
|
587
|
+
type: "leave-lobby",
|
|
588
|
+
channel: "system",
|
|
589
|
+
mode: "reliable",
|
|
590
|
+
data: { lobbyId: this.currentLobby.id }
|
|
591
|
+
});
|
|
592
|
+
this.currentLobby = null;
|
|
593
|
+
this.callbacks.onLobbyLeft?.();
|
|
594
|
+
}
|
|
595
|
+
refreshLobbyList() {
|
|
596
|
+
this.network.sendMessage({
|
|
597
|
+
type: "list-lobbies",
|
|
598
|
+
channel: "system",
|
|
599
|
+
mode: "reliable",
|
|
600
|
+
data: {}
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
startGame() {
|
|
604
|
+
if (!this.currentLobby) {
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
this.network.sendMessage({
|
|
608
|
+
type: "start-game",
|
|
609
|
+
channel: "system",
|
|
610
|
+
mode: "reliable",
|
|
611
|
+
data: { lobbyId: this.currentLobby.id }
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
// ─── Lobby Settings ────────────────────────────────────
|
|
615
|
+
updateLobbySettings(settings) {
|
|
616
|
+
if (!this.currentLobby) {
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
this.network.sendMessage({
|
|
620
|
+
type: "update-lobby",
|
|
621
|
+
channel: "system",
|
|
622
|
+
mode: "reliable",
|
|
623
|
+
data: { lobbyId: this.currentLobby.id, settings }
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
kickPlayer(playerId) {
|
|
627
|
+
if (!this.currentLobby) {
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
this.network.sendMessage({
|
|
631
|
+
type: "kick-player",
|
|
632
|
+
channel: "system",
|
|
633
|
+
mode: "reliable",
|
|
634
|
+
data: { lobbyId: this.currentLobby.id, playerId }
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
// ─── Getters ───────────────────────────────────────────
|
|
638
|
+
getCurrentLobby() {
|
|
639
|
+
return this.currentLobby;
|
|
640
|
+
}
|
|
641
|
+
getAvailableLobbies() {
|
|
642
|
+
return this.availableLobbies;
|
|
643
|
+
}
|
|
644
|
+
isInLobby() {
|
|
645
|
+
return this.currentLobby !== null;
|
|
646
|
+
}
|
|
647
|
+
isHost() {
|
|
648
|
+
const localPlayer = this.network.getLocalPlayer();
|
|
649
|
+
return this.currentLobby?.host.id === localPlayer?.id;
|
|
650
|
+
}
|
|
651
|
+
// ─── Events ────────────────────────────────────────────
|
|
652
|
+
on(event, callback) {
|
|
653
|
+
this.callbacks[event] = callback;
|
|
654
|
+
}
|
|
711
655
|
}
|
|
712
656
|
class MatchmakingManager {
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
657
|
+
constructor(network) {
|
|
658
|
+
this.status = {
|
|
659
|
+
state: "idle",
|
|
660
|
+
playersFound: 0,
|
|
661
|
+
playersRequired: 0,
|
|
662
|
+
estimatedWait: 0
|
|
663
|
+
};
|
|
664
|
+
this.callbacks = {};
|
|
665
|
+
this.network = network;
|
|
666
|
+
this.setupMessageHandlers();
|
|
667
|
+
}
|
|
668
|
+
setupMessageHandlers() {
|
|
669
|
+
this.network.registerMessageHandler("matchmaking-status", (msg) => {
|
|
670
|
+
this.status = msg.data;
|
|
671
|
+
this.callbacks.onStatusUpdate?.(this.status);
|
|
672
|
+
});
|
|
673
|
+
this.network.registerMessageHandler("match-found", (msg) => {
|
|
674
|
+
const { matchId } = msg.data;
|
|
675
|
+
this.status.state = "found";
|
|
676
|
+
this.status.matchId = matchId;
|
|
677
|
+
this.callbacks.onMatchFound?.(matchId);
|
|
678
|
+
});
|
|
679
|
+
this.network.registerMessageHandler("match-failed", (msg) => {
|
|
680
|
+
const { reason } = msg.data;
|
|
681
|
+
this.status.state = "failed";
|
|
682
|
+
this.callbacks.onMatchFailed?.(reason);
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
startMatchmaking(criteria) {
|
|
686
|
+
this.status = {
|
|
687
|
+
state: "searching",
|
|
688
|
+
playersFound: 1,
|
|
689
|
+
playersRequired: criteria.teamSize ?? 2,
|
|
690
|
+
estimatedWait: 30
|
|
691
|
+
};
|
|
692
|
+
this.network.sendMessage({
|
|
693
|
+
type: "start-matchmaking",
|
|
694
|
+
channel: "system",
|
|
695
|
+
mode: "reliable",
|
|
696
|
+
data: criteria
|
|
697
|
+
});
|
|
698
|
+
this.callbacks.onStatusUpdate?.(this.status);
|
|
699
|
+
}
|
|
700
|
+
cancelMatchmaking() {
|
|
701
|
+
this.network.sendMessage({
|
|
702
|
+
type: "cancel-matchmaking",
|
|
703
|
+
channel: "system",
|
|
704
|
+
mode: "reliable",
|
|
705
|
+
data: {}
|
|
706
|
+
});
|
|
707
|
+
this.status.state = "idle";
|
|
708
|
+
this.callbacks.onStatusUpdate?.(this.status);
|
|
709
|
+
}
|
|
710
|
+
acceptMatch() {
|
|
711
|
+
if (!this.status.matchId) {
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
this.status.state = "joining";
|
|
715
|
+
this.network.sendMessage({
|
|
716
|
+
type: "accept-match",
|
|
717
|
+
channel: "system",
|
|
718
|
+
mode: "reliable",
|
|
719
|
+
data: { matchId: this.status.matchId }
|
|
720
|
+
});
|
|
721
|
+
this.callbacks.onStatusUpdate?.(this.status);
|
|
722
|
+
}
|
|
723
|
+
declineMatch() {
|
|
724
|
+
if (!this.status.matchId) {
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
this.network.sendMessage({
|
|
728
|
+
type: "decline-match",
|
|
729
|
+
channel: "system",
|
|
730
|
+
mode: "reliable",
|
|
731
|
+
data: { matchId: this.status.matchId }
|
|
732
|
+
});
|
|
733
|
+
this.status.state = "searching";
|
|
734
|
+
delete this.status.matchId;
|
|
735
|
+
this.callbacks.onStatusUpdate?.(this.status);
|
|
736
|
+
}
|
|
737
|
+
getStatus() {
|
|
738
|
+
return { ...this.status };
|
|
739
|
+
}
|
|
740
|
+
isSearching() {
|
|
741
|
+
return this.status.state === "searching";
|
|
742
|
+
}
|
|
743
|
+
on(event, callback) {
|
|
744
|
+
this.callbacks[event] = callback;
|
|
745
|
+
}
|
|
802
746
|
}
|
|
803
|
-
// ============================================================
|
|
804
|
-
// Factory Functions
|
|
805
|
-
// ============================================================
|
|
806
747
|
function createNetworkManager(config) {
|
|
807
|
-
|
|
748
|
+
return new NetworkManager(config);
|
|
808
749
|
}
|
|
809
750
|
function createLobbyManager(network) {
|
|
810
|
-
|
|
751
|
+
return new LobbyManager(network);
|
|
811
752
|
}
|
|
812
753
|
function createMatchmakingManager(network) {
|
|
813
|
-
|
|
754
|
+
return new MatchmakingManager(network);
|
|
814
755
|
}
|
|
815
|
-
|
|
816
|
-
|
|
756
|
+
export {
|
|
757
|
+
LobbyManager,
|
|
758
|
+
MatchmakingManager,
|
|
759
|
+
NetworkEntity,
|
|
760
|
+
NetworkManager,
|
|
761
|
+
createLobbyManager,
|
|
762
|
+
createMatchmakingManager,
|
|
763
|
+
createNetworkManager
|
|
764
|
+
};
|