SceneReplication.h 4.4 KB

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