Urho2DPlatformer.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include "Utilities2D/Sample2D.h"
  25. class Character2D;
  26. class Sample2D;
  27. /// Urho2D platformer example.
  28. /// This sample demonstrates:
  29. /// - Creating an orthogonal 2D scene from tile map file
  30. /// - Displaying the scene using the Renderer subsystem
  31. /// - Handling keyboard to move a character and zoom 2D camera
  32. /// - Generating physics shapes from the tmx file's objects
  33. /// - Mixing physics and translations to move the character
  34. /// - Using Box2D Contact listeners to handle the gameplay
  35. /// - Displaying debug geometry for physics and tile map
  36. /// Note that this sample uses some functions from Sample2D utility class.
  37. class Urho2DPlatformer : public Sample
  38. {
  39. URHO3D_OBJECT(Urho2DPlatformer, Sample);
  40. public:
  41. /// Construct.
  42. explicit Urho2DPlatformer(Context* context);
  43. /// Setup after engine initialization and before running the main loop.
  44. void Start() override;
  45. /// Setup before engine initialization. Modifies the engine parameters.
  46. void Setup() override;
  47. private:
  48. /// Construct the scene content.
  49. void CreateScene();
  50. /// Subscribe to application-wide logic update events.
  51. void SubscribeToEvents();
  52. /// Handle the logic update event.
  53. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  54. /// Handle the logic post update event.
  55. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  56. /// Handle the post render update event.
  57. void HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData);
  58. /// Handle the end rendering event.
  59. void HandleSceneRendered(StringHash eventType, VariantMap& eventData);
  60. /// Handle the contact begin event (Box2D contact listener).
  61. void HandleCollisionBegin(StringHash eventType, VariantMap& eventData);
  62. /// Handle the contact end event (Box2D contact listener).
  63. void HandleCollisionEnd(StringHash eventType, VariantMap& eventData);
  64. /// Handle reloading the scene.
  65. void ReloadScene(bool reInit);
  66. /// Handle 'PLAY' button released event.
  67. void HandlePlayButton(StringHash eventType, VariantMap& eventData);
  68. /// The controllable character component.
  69. WeakPtr<Character2D> character2D_;
  70. /// Flag for drawing debug geometry.
  71. bool drawDebug_{};
  72. /// Scaling factor based on tiles' aspect ratio.
  73. float moveSpeedScale_{};
  74. /// Sample2D utility object.
  75. SharedPtr<Sample2D> sample2D_;
  76. };