Character.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // Copyright (c) 2008-2021 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/Input/Controls.h>
  24. #include <Urho3D/Scene/LogicComponent.h>
  25. using namespace Urho3D;
  26. const unsigned CTRL_FORWARD = 1;
  27. const unsigned CTRL_BACK = 2;
  28. const unsigned CTRL_LEFT = 4;
  29. const unsigned CTRL_RIGHT = 8;
  30. const unsigned CTRL_JUMP = 16;
  31. const float MOVE_FORCE = 0.8f;
  32. const float INAIR_MOVE_FORCE = 0.02f;
  33. const float BRAKE_FORCE = 0.2f;
  34. const float JUMP_FORCE = 7.0f;
  35. const float YAW_SENSITIVITY = 0.1f;
  36. const float INAIR_THRESHOLD_TIME = 0.1f;
  37. /// Character component, responsible for physical movement according to controls, as well as animation.
  38. class Character : public LogicComponent
  39. {
  40. URHO3D_OBJECT(Character, LogicComponent);
  41. public:
  42. /// Construct.
  43. explicit Character(Context* context);
  44. /// Register object factory and attributes.
  45. static void RegisterObject(Context* context);
  46. /// Handle startup. Called by LogicComponent base class.
  47. void Start() override;
  48. /// Handle physics world update. Called by LogicComponent base class.
  49. void FixedUpdate(float timeStep) override;
  50. /// Movement controls. Assigned by the main program each frame.
  51. Controls controls_;
  52. private:
  53. /// Handle physics collision event.
  54. void HandleNodeCollision(StringHash eventType, VariantMap& eventData);
  55. /// Grounded flag for movement.
  56. bool onGround_;
  57. /// Jump flag.
  58. bool okToJump_;
  59. /// In air timer. Due to possible physics inaccuracy, character can be off ground for max. 1/10 second and still be allowed to move.
  60. float inAirTimer_;
  61. };