Sample2D.h 6.3 KB

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