Sample2D.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include <Urho3D/Core/Object.h>
  5. namespace Urho3D
  6. {
  7. class Node;
  8. class Scene;
  9. }
  10. class Character2D;
  11. // All Urho3D classes reside in namespace Urho3D
  12. using namespace Urho3D;
  13. const float CAMERA_MIN_DIST = 0.1f;
  14. const float CAMERA_MAX_DIST = 6.0f;
  15. /// Convenient functions for Urho2D and Physics2D samples:
  16. /// - Generate collision shapes from a tmx file objects
  17. /// - Create Spriter Imp character
  18. /// - Create enemies, coins and platforms to tile map placeholders
  19. /// - Handle camera zoom using PageUp, PageDown and MouseWheel
  20. /// - Create UI instructions
  21. /// - Create a particle emitter attached to a given node
  22. /// - Play a non-looping sound effect
  23. /// - Load/Save the scene
  24. /// - Create XML patch instructions for screen joystick layout
  25. class Sample2D : public Object
  26. {
  27. URHO3D_OBJECT(Sample2D, Object);
  28. public:
  29. /// Construct.
  30. explicit Sample2D(Context* context);
  31. /// Destruct.
  32. ~Sample2D() override = default;
  33. /// Generate physics collision shapes from the tmx file's objects located in tileMapLayer.
  34. void CreateCollisionShapesFromTMXObjects(Node* tileMapNode, TileMapLayer2D* tileMapLayer, const TileMapInfo2D& info);
  35. /// Build collision shape from Tiled 'Rectangle' objects.
  36. CollisionBox2D* CreateRectangleShape(Node* node, TileMapObject2D* object, const Vector2& size, const TileMapInfo2D& info);
  37. /// Build collision shape from Tiled 'Ellipse' objects.
  38. CollisionCircle2D* CreateCircleShape(Node* node, TileMapObject2D* object, float radius, const TileMapInfo2D& info);
  39. /// Build collision shape from Tiled 'Polygon' objects.
  40. CollisionPolygon2D* CreatePolygonShape(Node* node, TileMapObject2D* object);
  41. /// Build collision shape from Tiled 'Poly Line' objects.
  42. void CreatePolyLineShape(Node* node, TileMapObject2D* object);
  43. /// Create Imp Spriter character.
  44. Node* CreateCharacter(const TileMapInfo2D& info, float friction, const Vector3& position, float scale);
  45. /// Create a trigger (will be cloned at each tmx placeholder).
  46. Node* CreateTrigger();
  47. /// Create an enemy (will be cloned at each tmx placeholder).
  48. Node* CreateEnemy();
  49. /// Create an Orc (will be cloned at each tmx placeholder).
  50. Node* CreateOrc();
  51. /// Create a coin (will be cloned at each tmx placeholder).
  52. Node* CreateCoin();
  53. /// Create a moving platform (will be cloned at each tmx placeholder).
  54. Node* CreateMovingPlatform();
  55. /// Instantiate enemies and moving platforms at each placeholder (placeholders are Poly Line objects defining a path from points).
  56. void PopulateMovingEntities(TileMapLayer2D* movingEntitiesLayer);
  57. /// Instantiate coins to pick at each placeholder.
  58. void PopulateCoins(TileMapLayer2D* coinsLayer);
  59. /// Instantiate triggers at each placeholder (Rectangle objects).
  60. void PopulateTriggers(TileMapLayer2D* triggersLayer);
  61. /// Read input and zoom the camera.
  62. float Zoom(Camera* camera);
  63. /// Create path from tmx object's points.
  64. Vector<Vector2> CreatePathFromPoints(TileMapObject2D* object, const Vector2& offset);
  65. /// Create the UI content.
  66. void CreateUIContent(const String& demoTitle, int remainingLifes, int remainingCoins);
  67. /// Handle 'EXIT' button released event.
  68. void HandleExitButton(StringHash eventType, VariantMap& eventData);
  69. /// Save the scene.
  70. void SaveScene(bool initial);
  71. /// Create a background 2D sprite, optionally rotated by a ValueAnimation object.
  72. void CreateBackgroundSprite(const TileMapInfo2D& info, float scale, const String& texture, bool animate);
  73. /// Create a particle emitter attached to the given node.
  74. void SpawnEffect(Node* node);
  75. /// Play a non-looping sound effect.
  76. void PlaySoundEffect(const String& soundName);
  77. /// Filename used in load/save functions.
  78. String demoFilename_;
  79. /// The scene.
  80. Scene* scene_{};
  81. protected:
  82. /// Return XML patch instructions for screen joystick layout.
  83. virtual String GetScreenJoystickPatchString() const { return
  84. "<patch>"
  85. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />"
  86. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Fight</replace>"
  87. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">"
  88. " <element type=\"Text\">"
  89. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  90. " <attribute name=\"Text\" value=\"SPACE\" />"
  91. " </element>"
  92. " </add>"
  93. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />"
  94. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Jump</replace>"
  95. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">"
  96. " <element type=\"Text\">"
  97. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  98. " <attribute name=\"Text\" value=\"UP\" />"
  99. " </element>"
  100. " </add>"
  101. "</patch>";
  102. }
  103. };