Character.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include <Urho3D/Input/Controls.h>
  5. #include <Urho3D/Scene/LogicComponent.h>
  6. using namespace Urho3D;
  7. const unsigned CTRL_FORWARD = 1;
  8. const unsigned CTRL_BACK = 2;
  9. const unsigned CTRL_LEFT = 4;
  10. const unsigned CTRL_RIGHT = 8;
  11. const unsigned CTRL_JUMP = 16;
  12. const float MOVE_FORCE = 0.8f;
  13. const float INAIR_MOVE_FORCE = 0.02f;
  14. const float BRAKE_FORCE = 0.2f;
  15. const float JUMP_FORCE = 7.0f;
  16. const float YAW_SENSITIVITY = 0.1f;
  17. const float INAIR_THRESHOLD_TIME = 0.1f;
  18. /// Character component, responsible for physical movement according to controls, as well as animation.
  19. class Character : public LogicComponent
  20. {
  21. URHO3D_OBJECT(Character, LogicComponent);
  22. public:
  23. /// Construct.
  24. explicit Character(Context* context);
  25. /// Register object factory and attributes.
  26. static void RegisterObject(Context* context);
  27. /// Handle startup. Called by LogicComponent base class.
  28. void Start() override;
  29. /// Handle physics world update. Called by LogicComponent base class.
  30. void FixedUpdate(float timeStep) override;
  31. /// Movement controls. Assigned by the main program each frame.
  32. Controls controls_;
  33. private:
  34. /// Handle physics collision event.
  35. void HandleNodeCollision(StringHash eventType, VariantMap& eventData);
  36. /// Grounded flag for movement.
  37. bool onGround_;
  38. /// Jump flag.
  39. bool okToJump_;
  40. /// In air timer. Due to possible physics inaccuracy, character can be off ground for max. 1/10 second and still be allowed to move.
  41. float inAirTimer_;
  42. };