SceneReplication.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "Sample.h"
  5. namespace Urho3D
  6. {
  7. class Button;
  8. class Connection;
  9. class Scene;
  10. class Text;
  11. class UIElement;
  12. }
  13. /// Scene network replication example.
  14. /// This sample demonstrates:
  15. /// - Creating a scene in which network clients can join
  16. /// - Giving each client an object to control and sending the controls from the clients to the server
  17. /// where the authoritative simulation happens
  18. /// - Controlling a physics object's movement by applying forces
  19. class SceneReplication : public Sample
  20. {
  21. URHO3D_OBJECT(SceneReplication, Sample);
  22. public:
  23. /// Construct.
  24. explicit SceneReplication(Context* context);
  25. /// Setup after engine initialization and before running the main loop.
  26. void Start() override;
  27. protected:
  28. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  29. String GetScreenJoystickPatchString() const override { return
  30. "<patch>"
  31. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
  32. " <attribute name=\"Is Visible\" value=\"false\" />"
  33. " </add>"
  34. "</patch>";
  35. }
  36. private:
  37. /// Construct the scene content.
  38. void CreateScene();
  39. /// Construct instruction text and the login / start server UI.
  40. void CreateUI();
  41. /// Set up viewport.
  42. void SetupViewport();
  43. /// Subscribe to update, UI and network events.
  44. void SubscribeToEvents();
  45. /// Create a button to the button container.
  46. Button* CreateButton(const String& text, int width);
  47. /// Update visibility of buttons according to connection and server status.
  48. void UpdateButtons();
  49. /// Create a controllable ball object and return its scene node.
  50. Node* CreateControllableObject();
  51. /// Read input and move the camera.
  52. void MoveCamera();
  53. /// Handle the physics world pre-step event.
  54. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  55. /// Handle the logic post-update event.
  56. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  57. /// Handle pressing the connect button.
  58. void HandleConnect(StringHash eventType, VariantMap& eventData);
  59. /// Handle pressing the disconnect button.
  60. void HandleDisconnect(StringHash eventType, VariantMap& eventData);
  61. /// Handle pressing the start server button.
  62. void HandleStartServer(StringHash eventType, VariantMap& eventData);
  63. /// Handle connection status change (just update the buttons that should be shown.)
  64. void HandleConnectionStatus(StringHash eventType, VariantMap& eventData);
  65. /// Handle a client connecting to the server.
  66. void HandleClientConnected(StringHash eventType, VariantMap& eventData);
  67. /// Handle a client disconnecting from the server.
  68. void HandleClientDisconnected(StringHash eventType, VariantMap& eventData);
  69. /// Handle remote event from server which tells our controlled object node ID.
  70. void HandleClientObjectID(StringHash eventType, VariantMap& eventData);
  71. /// Mapping from client connections to controllable objects.
  72. HashMap<Connection*, WeakPtr<Node>> serverObjects_;
  73. /// Button container element.
  74. SharedPtr<UIElement> buttonContainer_;
  75. /// Server address line editor element.
  76. SharedPtr<LineEdit> textEdit_;
  77. /// Connect button.
  78. SharedPtr<Button> connectButton_;
  79. /// Disconnect button.
  80. SharedPtr<Button> disconnectButton_;
  81. /// Start server button.
  82. SharedPtr<Button> startServerButton_;
  83. /// Instructions text.
  84. SharedPtr<Text> instructionsText_;
  85. /// ID of own controllable object (client only.)
  86. unsigned clientObjectID_{};
  87. /// Packets in per second
  88. SharedPtr<Text> packetsIn_;
  89. /// Packets out per second
  90. SharedPtr<Text> packetsOut_;
  91. /// Packet counter UI update timer
  92. Timer packetCounterTimer_;
  93. };