CharacterDemo.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "Sample.h"
  5. namespace Urho3D
  6. {
  7. class Node;
  8. class Scene;
  9. }
  10. class Character;
  11. class Touch;
  12. /// Moving character example.
  13. /// This sample demonstrates:
  14. /// - Controlling a humanoid character through physics
  15. /// - Driving animations using the AnimationController component
  16. /// - Manual control of a bone scene node
  17. /// - Implementing 1st and 3rd person cameras, using raycasts to avoid the 3rd person camera clipping into scenery
  18. /// - Defining attributes of a custom component so that it can be saved and loaded
  19. /// - Using touch inputs/gyroscope for iOS/Android (implemented through an external file)
  20. class CharacterDemo : public Sample
  21. {
  22. URHO3D_OBJECT(CharacterDemo, Sample);
  23. public:
  24. /// Construct.
  25. explicit CharacterDemo(Context* context);
  26. /// Destruct.
  27. ~CharacterDemo() override;
  28. /// Setup after engine initialization and before running the main loop.
  29. void Start() override;
  30. protected:
  31. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  32. String GetScreenJoystickPatchString() const override { return
  33. "<patch>"
  34. " <add sel=\"/element\">"
  35. " <element type=\"Button\">"
  36. " <attribute name=\"Name\" value=\"Button3\" />"
  37. " <attribute name=\"Position\" value=\"-120 -120\" />"
  38. " <attribute name=\"Size\" value=\"96 96\" />"
  39. " <attribute name=\"Horiz Alignment\" value=\"Right\" />"
  40. " <attribute name=\"Vert Alignment\" value=\"Bottom\" />"
  41. " <attribute name=\"Texture\" value=\"Texture2D;Textures/TouchInput.png\" />"
  42. " <attribute name=\"Image Rect\" value=\"96 0 192 96\" />"
  43. " <attribute name=\"Hover Image Offset\" value=\"0 0\" />"
  44. " <attribute name=\"Pressed Image Offset\" value=\"0 0\" />"
  45. " <element type=\"Text\">"
  46. " <attribute name=\"Name\" value=\"Label\" />"
  47. " <attribute name=\"Horiz Alignment\" value=\"Center\" />"
  48. " <attribute name=\"Vert Alignment\" value=\"Center\" />"
  49. " <attribute name=\"Color\" value=\"0 0 0 1\" />"
  50. " <attribute name=\"Text\" value=\"Gyroscope\" />"
  51. " </element>"
  52. " <element type=\"Text\">"
  53. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  54. " <attribute name=\"Text\" value=\"G\" />"
  55. " </element>"
  56. " </element>"
  57. " </add>"
  58. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />"
  59. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">1st/3rd</replace>"
  60. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">"
  61. " <element type=\"Text\">"
  62. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  63. " <attribute name=\"Text\" value=\"F\" />"
  64. " </element>"
  65. " </add>"
  66. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />"
  67. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Jump</replace>"
  68. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">"
  69. " <element type=\"Text\">"
  70. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  71. " <attribute name=\"Text\" value=\"SPACE\" />"
  72. " </element>"
  73. " </add>"
  74. "</patch>";
  75. }
  76. private:
  77. /// Create static scene content.
  78. void CreateScene();
  79. /// Create controllable character.
  80. void CreateCharacter();
  81. /// Construct an instruction text to the UI.
  82. void CreateInstructions();
  83. /// Subscribe to necessary events.
  84. void SubscribeToEvents();
  85. /// Handle application update. Set controls to character.
  86. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  87. /// Handle application post-update. Update camera position after character has moved.
  88. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  89. /// Touch utility object.
  90. SharedPtr<Touch> touch_;
  91. /// The controllable character component.
  92. WeakPtr<Character> character_;
  93. /// First person camera flag.
  94. bool firstPerson_;
  95. };