SceneReplication.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Engine/Engine.h>
  5. #include <Urho3D/Graphics/Camera.h>
  6. #include <Urho3D/Graphics/Graphics.h>
  7. #include <Urho3D/Graphics/Light.h>
  8. #include <Urho3D/Graphics/Material.h>
  9. #include <Urho3D/Graphics/Model.h>
  10. #include <Urho3D/Graphics/Octree.h>
  11. #include <Urho3D/Graphics/Renderer.h>
  12. #include <Urho3D/Graphics/StaticModel.h>
  13. #include <Urho3D/Graphics/Zone.h>
  14. #include <Urho3D/Input/Controls.h>
  15. #include <Urho3D/Input/Input.h>
  16. #include <Urho3D/IO/Log.h>
  17. #include <Urho3D/Network/Connection.h>
  18. #include <Urho3D/Network/Network.h>
  19. #include <Urho3D/Network/NetworkEvents.h>
  20. #include <Urho3D/Physics/CollisionShape.h>
  21. #include <Urho3D/Physics/PhysicsEvents.h>
  22. #include <Urho3D/Physics/PhysicsWorld.h>
  23. #include <Urho3D/Physics/RigidBody.h>
  24. #include <Urho3D/Resource/ResourceCache.h>
  25. #include <Urho3D/Scene/Scene.h>
  26. #include <Urho3D/UI/Button.h>
  27. #include <Urho3D/UI/Font.h>
  28. #include <Urho3D/UI/LineEdit.h>
  29. #include <Urho3D/UI/Text.h>
  30. #include <Urho3D/UI/UI.h>
  31. #include <Urho3D/UI/UIEvents.h>
  32. #include "SceneReplication.h"
  33. #include <Urho3D/DebugNew.h>
  34. // UDP port we will use
  35. static const unsigned short SERVER_PORT = 2345;
  36. // Identifier for our custom remote event we use to tell the client which object they control
  37. static const StringHash E_CLIENTOBJECTID("ClientObjectID");
  38. // Identifier for the node ID parameter in the event data
  39. static const StringHash P_ID("ID");
  40. // Control bits we define
  41. static const unsigned CTRL_FORWARD = 1;
  42. static const unsigned CTRL_BACK = 2;
  43. static const unsigned CTRL_LEFT = 4;
  44. static const unsigned CTRL_RIGHT = 8;
  45. URHO3D_DEFINE_APPLICATION_MAIN(SceneReplication)
  46. SceneReplication::SceneReplication(Context* context) :
  47. Sample(context)
  48. {
  49. }
  50. void SceneReplication::Start()
  51. {
  52. // Execute base class startup
  53. Sample::Start();
  54. // Create the scene content
  55. CreateScene();
  56. // Create the UI content
  57. CreateUI();
  58. // Setup the viewport for displaying the scene
  59. SetupViewport();
  60. // Hook up to necessary events
  61. SubscribeToEvents();
  62. // Set the mouse mode to use in the sample
  63. Sample::InitMouseMode(MM_RELATIVE);
  64. }
  65. void SceneReplication::CreateScene()
  66. {
  67. scene_ = new Scene(context_);
  68. auto* cache = GetSubsystem<ResourceCache>();
  69. // Create octree and physics world with default settings. Create them as local so that they are not needlessly replicated
  70. // when a client connects
  71. scene_->CreateComponent<Octree>(LOCAL);
  72. scene_->CreateComponent<PhysicsWorld>(LOCAL);
  73. // All static scene content and the camera are also created as local, so that they are unaffected by scene replication and are
  74. // not removed from the client upon connection. Create a Zone component first for ambient lighting & fog control.
  75. Node* zoneNode = scene_->CreateChild("Zone", LOCAL);
  76. auto* zone = zoneNode->CreateComponent<Zone>();
  77. zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
  78. zone->SetAmbientColor(Color(0.1f, 0.1f, 0.1f));
  79. zone->SetFogStart(100.0f);
  80. zone->SetFogEnd(300.0f);
  81. // Create a directional light without shadows
  82. Node* lightNode = scene_->CreateChild("DirectionalLight", LOCAL);
  83. lightNode->SetDirection(Vector3(0.5f, -1.0f, 0.5f));
  84. auto* light = lightNode->CreateComponent<Light>();
  85. light->SetLightType(LIGHT_DIRECTIONAL);
  86. light->SetColor(Color(0.2f, 0.2f, 0.2f));
  87. light->SetSpecularIntensity(1.0f);
  88. // Create a "floor" consisting of several tiles. Make the tiles physical but leave small cracks between them
  89. for (int y = -20; y <= 20; ++y)
  90. {
  91. for (int x = -20; x <= 20; ++x)
  92. {
  93. Node* floorNode = scene_->CreateChild("FloorTile", LOCAL);
  94. floorNode->SetPosition(Vector3(x * 20.2f, -0.5f, y * 20.2f));
  95. floorNode->SetScale(Vector3(20.0f, 1.0f, 20.0f));
  96. auto* floorObject = floorNode->CreateComponent<StaticModel>();
  97. floorObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  98. floorObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
  99. auto* body = floorNode->CreateComponent<RigidBody>();
  100. body->SetFriction(1.0f);
  101. auto* shape = floorNode->CreateComponent<CollisionShape>();
  102. shape->SetBox(Vector3::ONE);
  103. }
  104. }
  105. // Create the camera. Limit far clip distance to match the fog
  106. // The camera needs to be created into a local node so that each client can retain its own camera, that is unaffected by
  107. // network messages. Furthermore, because the client removes all replicated scene nodes when connecting to a server scene,
  108. // the screen would become blank if the camera node was replicated (as only the locally created camera is assigned to a
  109. // viewport in SetupViewports() below)
  110. cameraNode_ = scene_->CreateChild("Camera", LOCAL);
  111. auto* camera = cameraNode_->CreateComponent<Camera>();
  112. camera->SetFarClip(300.0f);
  113. // Set an initial position for the camera scene node above the plane
  114. cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f));
  115. }
  116. void SceneReplication::CreateUI()
  117. {
  118. auto* cache = GetSubsystem<ResourceCache>();
  119. auto* ui = GetSubsystem<UI>();
  120. UIElement* root = ui->GetRoot();
  121. auto* uiStyle = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  122. // Set style to the UI root so that elements will inherit it
  123. root->SetDefaultStyle(uiStyle);
  124. // Create a Cursor UI element because we want to be able to hide and show it at will. When hidden, the mouse cursor will
  125. // control the camera, and when visible, it can interact with the login UI
  126. SharedPtr<Cursor> cursor(new Cursor(context_));
  127. cursor->SetStyleAuto(uiStyle);
  128. ui->SetCursor(cursor);
  129. // Set starting position of the cursor at the rendering window center
  130. auto* graphics = GetSubsystem<Graphics>();
  131. cursor->SetPosition(graphics->GetWidth() / 2, graphics->GetHeight() / 2);
  132. // Construct the instructions text element
  133. instructionsText_ = ui->GetRoot()->CreateChild<Text>();
  134. instructionsText_->SetText(
  135. "Use WASD keys to move and RMB to rotate view"
  136. );
  137. instructionsText_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  138. // Position the text relative to the screen center
  139. instructionsText_->SetHorizontalAlignment(HA_CENTER);
  140. instructionsText_->SetVerticalAlignment(VA_CENTER);
  141. instructionsText_->SetPosition(0, graphics->GetHeight() / 4);
  142. // Hide until connected
  143. instructionsText_->SetVisible(false);
  144. packetsIn_ = ui->GetRoot()->CreateChild<Text>();
  145. packetsIn_->SetText("Packets in : 0");
  146. packetsIn_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  147. packetsIn_->SetHorizontalAlignment(HA_LEFT);
  148. packetsIn_->SetVerticalAlignment(VA_CENTER);
  149. packetsIn_->SetPosition(10, -10);
  150. packetsOut_ = ui->GetRoot()->CreateChild<Text>();
  151. packetsOut_->SetText("Packets out: 0");
  152. packetsOut_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  153. packetsOut_->SetHorizontalAlignment(HA_LEFT);
  154. packetsOut_->SetVerticalAlignment(VA_CENTER);
  155. packetsOut_->SetPosition(10, 10);
  156. buttonContainer_ = root->CreateChild<UIElement>();
  157. buttonContainer_->SetFixedSize(500, 20);
  158. buttonContainer_->SetPosition(20, 20);
  159. buttonContainer_->SetLayoutMode(LM_HORIZONTAL);
  160. textEdit_ = buttonContainer_->CreateChild<LineEdit>();
  161. textEdit_->SetStyleAuto();
  162. connectButton_ = CreateButton("Connect", 90);
  163. disconnectButton_ = CreateButton("Disconnect", 100);
  164. startServerButton_ = CreateButton("Start Server", 110);
  165. UpdateButtons();
  166. }
  167. void SceneReplication::SetupViewport()
  168. {
  169. auto* renderer = GetSubsystem<Renderer>();
  170. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  171. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  172. renderer->SetViewport(0, viewport);
  173. }
  174. void SceneReplication::SubscribeToEvents()
  175. {
  176. // Subscribe to fixed timestep physics updates for setting or applying controls
  177. SubscribeToEvent(E_PHYSICSPRESTEP, URHO3D_HANDLER(SceneReplication, HandlePhysicsPreStep));
  178. // Subscribe HandlePostUpdate() method for processing update events. Subscribe to PostUpdate instead
  179. // of the usual Update so that physics simulation has already proceeded for the frame, and can
  180. // accurately follow the object with the camera
  181. SubscribeToEvent(E_POSTUPDATE, URHO3D_HANDLER(SceneReplication, HandlePostUpdate));
  182. // Subscribe to button actions
  183. SubscribeToEvent(connectButton_, E_RELEASED, URHO3D_HANDLER(SceneReplication, HandleConnect));
  184. SubscribeToEvent(disconnectButton_, E_RELEASED, URHO3D_HANDLER(SceneReplication, HandleDisconnect));
  185. SubscribeToEvent(startServerButton_, E_RELEASED, URHO3D_HANDLER(SceneReplication, HandleStartServer));
  186. // Subscribe to network events
  187. SubscribeToEvent(E_SERVERCONNECTED, URHO3D_HANDLER(SceneReplication, HandleConnectionStatus));
  188. SubscribeToEvent(E_SERVERDISCONNECTED, URHO3D_HANDLER(SceneReplication, HandleConnectionStatus));
  189. SubscribeToEvent(E_CONNECTFAILED, URHO3D_HANDLER(SceneReplication, HandleConnectionStatus));
  190. SubscribeToEvent(E_CLIENTCONNECTED, URHO3D_HANDLER(SceneReplication, HandleClientConnected));
  191. SubscribeToEvent(E_CLIENTDISCONNECTED, URHO3D_HANDLER(SceneReplication, HandleClientDisconnected));
  192. // This is a custom event, sent from the server to the client. It tells the node ID of the object the client should control
  193. SubscribeToEvent(E_CLIENTOBJECTID, URHO3D_HANDLER(SceneReplication, HandleClientObjectID));
  194. // Events sent between client & server (remote events) must be explicitly registered or else they are not allowed to be received
  195. GetSubsystem<Network>()->RegisterRemoteEvent(E_CLIENTOBJECTID);
  196. }
  197. Button* SceneReplication::CreateButton(const String& text, int width)
  198. {
  199. auto* cache = GetSubsystem<ResourceCache>();
  200. auto* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  201. auto* button = buttonContainer_->CreateChild<Button>();
  202. button->SetStyleAuto();
  203. button->SetFixedWidth(width);
  204. auto* buttonText = button->CreateChild<Text>();
  205. buttonText->SetFont(font, 12);
  206. buttonText->SetAlignment(HA_CENTER, VA_CENTER);
  207. buttonText->SetText(text);
  208. return button;
  209. }
  210. void SceneReplication::UpdateButtons()
  211. {
  212. auto* network = GetSubsystem<Network>();
  213. Connection* serverConnection = network->GetServerConnection();
  214. bool serverRunning = network->IsServerRunning();
  215. // Show and hide buttons so that eg. Connect and Disconnect are never shown at the same time
  216. connectButton_->SetVisible(!serverConnection && !serverRunning);
  217. disconnectButton_->SetVisible(serverConnection || serverRunning);
  218. startServerButton_->SetVisible(!serverConnection && !serverRunning);
  219. textEdit_->SetVisible(!serverConnection && !serverRunning);
  220. }
  221. Node* SceneReplication::CreateControllableObject()
  222. {
  223. auto* cache = GetSubsystem<ResourceCache>();
  224. // Create the scene node & visual representation. This will be a replicated object
  225. Node* ballNode = scene_->CreateChild("Ball");
  226. ballNode->SetPosition(Vector3(Random(40.0f) - 20.0f, 5.0f, Random(40.0f) - 20.0f));
  227. ballNode->SetScale(0.5f);
  228. auto* ballObject = ballNode->CreateComponent<StaticModel>();
  229. ballObject->SetModel(cache->GetResource<Model>("Models/Sphere.mdl"));
  230. ballObject->SetMaterial(cache->GetResource<Material>("Materials/StoneSmall.xml"));
  231. // Create the physics components
  232. auto* body = ballNode->CreateComponent<RigidBody>();
  233. body->SetMass(1.0f);
  234. body->SetFriction(1.0f);
  235. // In addition to friction, use motion damping so that the ball can not accelerate limitlessly
  236. body->SetLinearDamping(0.5f);
  237. body->SetAngularDamping(0.5f);
  238. auto* shape = ballNode->CreateComponent<CollisionShape>();
  239. shape->SetSphere(1.0f);
  240. // Create a random colored point light at the ball so that can see better where is going
  241. auto* light = ballNode->CreateComponent<Light>();
  242. light->SetRange(3.0f);
  243. light->SetColor(
  244. Color(0.5f + ((unsigned)Rand() & 1u) * 0.5f, 0.5f + ((unsigned)Rand() & 1u) * 0.5f, 0.5f + ((unsigned)Rand() & 1u) * 0.5f));
  245. return ballNode;
  246. }
  247. void SceneReplication::MoveCamera()
  248. {
  249. // Right mouse button controls mouse cursor visibility: hide when pressed
  250. auto* ui = GetSubsystem<UI>();
  251. auto* input = GetSubsystem<Input>();
  252. ui->GetCursor()->SetVisible(!input->GetMouseButtonDown(MOUSEB_RIGHT));
  253. // Mouse sensitivity as degrees per pixel
  254. const float MOUSE_SENSITIVITY = 0.1f;
  255. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch and only move the camera
  256. // when the cursor is hidden
  257. if (!ui->GetCursor()->IsVisible())
  258. {
  259. IntVector2 mouseMove = input->GetMouseMove();
  260. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  261. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  262. pitch_ = Clamp(pitch_, 1.0f, 90.0f);
  263. }
  264. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  265. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  266. // Only move the camera / show instructions if we have a controllable object
  267. bool showInstructions = false;
  268. if (clientObjectID_)
  269. {
  270. Node* ballNode = scene_->GetNode(clientObjectID_);
  271. if (ballNode)
  272. {
  273. const float CAMERA_DISTANCE = 5.0f;
  274. // Move camera some distance away from the ball
  275. cameraNode_->SetPosition(ballNode->GetPosition() + cameraNode_->GetRotation() * Vector3::BACK * CAMERA_DISTANCE);
  276. showInstructions = true;
  277. }
  278. }
  279. instructionsText_->SetVisible(showInstructions);
  280. }
  281. void SceneReplication::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  282. {
  283. // We only rotate the camera according to mouse movement since last frame, so do not need the time step
  284. MoveCamera();
  285. if (packetCounterTimer_.GetMSec(false) > 1000 && GetSubsystem<Network>()->GetServerConnection())
  286. {
  287. packetsIn_->SetText("Packets in: " + String(GetSubsystem<Network>()->GetServerConnection()->GetPacketsInPerSec()));
  288. packetsOut_->SetText("Packets out: " + String(GetSubsystem<Network>()->GetServerConnection()->GetPacketsOutPerSec()));
  289. packetCounterTimer_.Reset();
  290. }
  291. if (packetCounterTimer_.GetMSec(false) > 1000 && GetSubsystem<Network>()->GetClientConnections().Size())
  292. {
  293. int packetsIn = 0;
  294. int packetsOut = 0;
  295. auto connections = GetSubsystem<Network>()->GetClientConnections();
  296. for (auto it = connections.Begin(); it != connections.End(); ++it ) {
  297. packetsIn += (*it)->GetPacketsInPerSec();
  298. packetsOut += (*it)->GetPacketsOutPerSec();
  299. }
  300. packetsIn_->SetText("Packets in: " + String(packetsIn));
  301. packetsOut_->SetText("Packets out: " + String(packetsOut));
  302. packetCounterTimer_.Reset();
  303. }
  304. }
  305. void SceneReplication::HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData)
  306. {
  307. // This function is different on the client and server. The client collects controls (WASD controls + yaw angle)
  308. // and sets them to its server connection object, so that they will be sent to the server automatically at a
  309. // fixed rate, by default 30 FPS. The server will actually apply the controls (authoritative simulation.)
  310. auto* network = GetSubsystem<Network>();
  311. Connection* serverConnection = network->GetServerConnection();
  312. // Client: collect controls
  313. if (serverConnection)
  314. {
  315. auto* ui = GetSubsystem<UI>();
  316. auto* input = GetSubsystem<Input>();
  317. Controls controls;
  318. // Copy mouse yaw
  319. controls.yaw_ = yaw_;
  320. // Only apply WASD controls if there is no focused UI element
  321. if (!ui->GetFocusElement())
  322. {
  323. controls.Set(CTRL_FORWARD, input->GetKeyDown(KEY_W));
  324. controls.Set(CTRL_BACK, input->GetKeyDown(KEY_S));
  325. controls.Set(CTRL_LEFT, input->GetKeyDown(KEY_A));
  326. controls.Set(CTRL_RIGHT, input->GetKeyDown(KEY_D));
  327. }
  328. serverConnection->SetControls(controls);
  329. // In case the server wants to do position-based interest management using the NetworkPriority components, we should also
  330. // tell it our observer (camera) position. In this sample it is not in use, but eg. the NinjaSnowWar game uses it
  331. serverConnection->SetPosition(cameraNode_->GetPosition());
  332. }
  333. // Server: apply controls to client objects
  334. else if (network->IsServerRunning())
  335. {
  336. const Vector<SharedPtr<Connection>>& connections = network->GetClientConnections();
  337. for (Connection* connection : connections)
  338. {
  339. // Get the object this connection is controlling
  340. Node* ballNode = serverObjects_[connection];
  341. if (!ballNode)
  342. continue;
  343. RigidBody* body = ballNode->GetComponent<RigidBody>();
  344. // Get the last controls sent by the client
  345. const Controls& controls = connection->GetControls();
  346. // Torque is relative to the forward vector
  347. Quaternion rotation(0.0f, controls.yaw_, 0.0f);
  348. const float MOVE_TORQUE = 3.0f;
  349. // Movement torque is applied before each simulation step, which happen at 60 FPS. This makes the simulation
  350. // independent from rendering framerate. We could also apply forces (which would enable in-air control),
  351. // but want to emphasize that it's a ball which should only control its motion by rolling along the ground
  352. if (controls.buttons_ & CTRL_FORWARD)
  353. body->ApplyTorque(rotation * Vector3::RIGHT * MOVE_TORQUE);
  354. if (controls.buttons_ & CTRL_BACK)
  355. body->ApplyTorque(rotation * Vector3::LEFT * MOVE_TORQUE);
  356. if (controls.buttons_ & CTRL_LEFT)
  357. body->ApplyTorque(rotation * Vector3::FORWARD * MOVE_TORQUE);
  358. if (controls.buttons_ & CTRL_RIGHT)
  359. body->ApplyTorque(rotation * Vector3::BACK * MOVE_TORQUE);
  360. }
  361. }
  362. }
  363. void SceneReplication::HandleConnect(StringHash eventType, VariantMap& eventData)
  364. {
  365. auto* network = GetSubsystem<Network>();
  366. String address = textEdit_->GetText().Trimmed();
  367. if (address.Empty())
  368. address = "localhost"; // Use localhost to connect if nothing else specified
  369. // Connect to server, specify scene to use as a client for replication
  370. clientObjectID_ = 0; // Reset own object ID from possible previous connection
  371. network->Connect(address, SERVER_PORT, scene_);
  372. UpdateButtons();
  373. }
  374. void SceneReplication::HandleDisconnect(StringHash eventType, VariantMap& eventData)
  375. {
  376. auto* network = GetSubsystem<Network>();
  377. Connection* serverConnection = network->GetServerConnection();
  378. // If we were connected to server, disconnect. Or if we were running a server, stop it. In both cases clear the
  379. // scene of all replicated content, but let the local nodes & components (the static world + camera) stay
  380. if (serverConnection)
  381. {
  382. serverConnection->Disconnect();
  383. scene_->Clear(true, false);
  384. clientObjectID_ = 0;
  385. }
  386. // Or if we were running a server, stop it
  387. else if (network->IsServerRunning())
  388. {
  389. network->StopServer();
  390. scene_->Clear(true, false);
  391. }
  392. UpdateButtons();
  393. }
  394. void SceneReplication::HandleStartServer(StringHash eventType, VariantMap& eventData)
  395. {
  396. auto* network = GetSubsystem<Network>();
  397. network->StartServer(SERVER_PORT);
  398. UpdateButtons();
  399. }
  400. void SceneReplication::HandleConnectionStatus(StringHash eventType, VariantMap& eventData)
  401. {
  402. UpdateButtons();
  403. }
  404. void SceneReplication::HandleClientConnected(StringHash eventType, VariantMap& eventData)
  405. {
  406. using namespace ClientConnected;
  407. // When a client connects, assign to scene to begin scene replication
  408. auto* newConnection = static_cast<Connection*>(eventData[P_CONNECTION].GetPtr());
  409. newConnection->SetScene(scene_);
  410. // Then create a controllable object for that client
  411. Node* newObject = CreateControllableObject();
  412. serverObjects_[newConnection] = newObject;
  413. // Finally send the object's node ID using a remote event
  414. VariantMap remoteEventData;
  415. remoteEventData[P_ID] = newObject->GetID();
  416. newConnection->SendRemoteEvent(E_CLIENTOBJECTID, true, remoteEventData);
  417. }
  418. void SceneReplication::HandleClientDisconnected(StringHash eventType, VariantMap& eventData)
  419. {
  420. using namespace ClientConnected;
  421. // When a client disconnects, remove the controlled object
  422. auto* connection = static_cast<Connection*>(eventData[P_CONNECTION].GetPtr());
  423. Node* object = serverObjects_[connection];
  424. if (object)
  425. object->Remove();
  426. serverObjects_.Erase(connection);
  427. }
  428. void SceneReplication::HandleClientObjectID(StringHash eventType, VariantMap& eventData)
  429. {
  430. clientObjectID_ = eventData[P_ID].GetUInt();
  431. }