SceneReplication.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Sample.h"
  24. namespace Urho3D
  25. {
  26. class Button;
  27. class Connection;
  28. class Scene;
  29. class Text;
  30. class UIElement;
  31. }
  32. /// Scene network replication example.
  33. /// This sample demonstrates:
  34. /// - Creating a scene in which network clients can join
  35. /// - Giving each client an object to control and sending the controls from the clients to the server
  36. /// where the authoritative simulation happens
  37. /// - Controlling a physics object's movement by applying forces
  38. class SceneReplication : public Sample
  39. {
  40. URHO3D_OBJECT(SceneReplication, Sample);
  41. public:
  42. /// Construct.
  43. explicit SceneReplication(Context* context);
  44. /// Setup before engine initialization. Modifies the engine parameters.
  45. void Setup() override;
  46. /// Setup after engine initialization and before running the main loop.
  47. void Start() override;
  48. /// Exit the app
  49. void Exit();
  50. protected:
  51. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  52. String GetScreenJoystickPatchString() const override { return
  53. "<patch>"
  54. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
  55. " <attribute name=\"Is Visible\" value=\"false\" />"
  56. " </add>"
  57. "</patch>";
  58. }
  59. private:
  60. /// Construct the scene content.
  61. void CreateScene();
  62. /// Construct instruction text and the login / start server UI.
  63. void CreateUI();
  64. /// Set up viewport.
  65. void SetupViewport();
  66. /// Subscribe to update, UI and network events.
  67. void SubscribeToEvents();
  68. /// Create a button to the button container.
  69. Button* CreateButton(const String& text, int width);
  70. /// Update visibility of buttons according to connection and server status.
  71. void UpdateButtons();
  72. /// Create a controllable ball object and return its scene node.
  73. Node* CreateControllableObject();
  74. /// Read input and move the camera.
  75. void MoveCamera();
  76. /// Handle the physics world pre-step event.
  77. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  78. /// Handle the logic post-update event.
  79. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  80. /// Handle pressing the connect button.
  81. void HandleConnect(StringHash eventType, VariantMap& eventData);
  82. /// Handle pressing the disconnect button.
  83. void HandleDisconnect(StringHash eventType, VariantMap& eventData);
  84. /// Handle pressing the start server button.
  85. void HandleStartServer(StringHash eventType, VariantMap& eventData);
  86. /// Handle connection status change (just update the buttons that should be shown.)
  87. void HandleConnectionStatus(StringHash eventType, VariantMap& eventData);
  88. /// Handle a client connecting to the server.
  89. void HandleClientConnected(StringHash eventType, VariantMap& eventData);
  90. /// Handle a client disconnecting from the server.
  91. void HandleClientDisconnected(StringHash eventType, VariantMap& eventData);
  92. /// Handle remote event from server which tells our controlled object node ID.
  93. void HandleClientObjectID(StringHash eventType, VariantMap& eventData);
  94. /// Start the server
  95. void StartServer();
  96. /// Mapping from client connections to controllable objects.
  97. HashMap<Connection*, WeakPtr<Node> > serverObjects_;
  98. /// Button container element.
  99. SharedPtr<UIElement> buttonContainer_;
  100. /// Server address line editor element.
  101. SharedPtr<LineEdit> serverAddress_;
  102. /// Server port
  103. SharedPtr<LineEdit> serverPort_;
  104. /// Connect button.
  105. SharedPtr<Button> connectButton_;
  106. /// Disconnect button.
  107. SharedPtr<Button> disconnectButton_;
  108. /// Start server button.
  109. SharedPtr<Button> startServerButton_;
  110. /// Instructions text.
  111. SharedPtr<Text> instructionsText_;
  112. /// ID of own controllable object (client only.)
  113. unsigned clientObjectID_{};
  114. /// Packets in per second
  115. SharedPtr<Text> packetsIn_;
  116. /// Packets out per second
  117. SharedPtr<Text> packetsOut_;
  118. /// Packet counter UI update timer
  119. Timer packetCounterTimer_;
  120. };