SceneReplication.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Copyright (c) 2008-2017 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. SceneReplication(Context* context);
  44. /// Setup after engine initialization and before running the main loop.
  45. virtual void Start();
  46. protected:
  47. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  48. virtual String GetScreenJoystickPatchString() const { return
  49. "<patch>"
  50. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
  51. " <attribute name=\"Is Visible\" value=\"false\" />"
  52. " </add>"
  53. "</patch>";
  54. }
  55. private:
  56. /// Construct the scene content.
  57. void CreateScene();
  58. /// Construct instruction text and the login / start server UI.
  59. void CreateUI();
  60. /// Set up viewport.
  61. void SetupViewport();
  62. /// Subscribe to update, UI and network events.
  63. void SubscribeToEvents();
  64. /// Create a button to the button container.
  65. Button* CreateButton(const String& text, int width);
  66. /// Update visibility of buttons according to connection and server status.
  67. void UpdateButtons();
  68. /// Create a controllable ball object and return its scene node.
  69. Node* CreateControllableObject();
  70. /// Read input and move the camera.
  71. void MoveCamera();
  72. /// Handle the physics world pre-step event.
  73. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  74. /// Handle the logic post-update event.
  75. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  76. /// Handle pressing the connect button.
  77. void HandleConnect(StringHash eventType, VariantMap& eventData);
  78. /// Handle pressing the disconnect button.
  79. void HandleDisconnect(StringHash eventType, VariantMap& eventData);
  80. /// Handle pressing the start server button.
  81. void HandleStartServer(StringHash eventType, VariantMap& eventData);
  82. /// Handle connection status change (just update the buttons that should be shown.)
  83. void HandleConnectionStatus(StringHash eventType, VariantMap& eventData);
  84. /// Handle a client connecting to the server.
  85. void HandleClientConnected(StringHash eventType, VariantMap& eventData);
  86. /// Handle a client disconnecting from the server.
  87. void HandleClientDisconnected(StringHash eventType, VariantMap& eventData);
  88. /// Handle remote event from server which tells our controlled object node ID.
  89. void HandleClientObjectID(StringHash eventType, VariantMap& eventData);
  90. /// Mapping from client connections to controllable objects.
  91. HashMap<Connection*, WeakPtr<Node> > serverObjects_;
  92. /// Button container element.
  93. SharedPtr<UIElement> buttonContainer_;
  94. /// Server address line editor element.
  95. SharedPtr<LineEdit> textEdit_;
  96. /// Connect button.
  97. SharedPtr<Button> connectButton_;
  98. /// Disconnect button.
  99. SharedPtr<Button> disconnectButton_;
  100. /// Start server button.
  101. SharedPtr<Button> startServerButton_;
  102. /// Instructions text.
  103. SharedPtr<Text> instructionsText_;
  104. /// ID of own controllable object (client only.)
  105. unsigned clientObjectID_;
  106. };