Character.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include <Atomic/Input/Controls.h>
  25. #include <Atomic/Scene/LogicComponent.h>
  26. using namespace Atomic;
  27. const int CTRL_FORWARD = 1;
  28. const int CTRL_BACK = 2;
  29. const int CTRL_LEFT = 4;
  30. const int CTRL_RIGHT = 8;
  31. const int CTRL_JUMP = 16;
  32. const float MOVE_FORCE = 0.8f;
  33. const float INAIR_MOVE_FORCE = 0.02f;
  34. const float BRAKE_FORCE = 0.2f;
  35. const float JUMP_FORCE = 7.0f;
  36. const float YAW_SENSITIVITY = 0.1f;
  37. const float INAIR_THRESHOLD_TIME = 0.1f;
  38. /// Character component, responsible for physical movement according to controls, as well as animation.
  39. class Character : public LogicComponent
  40. {
  41. ATOMIC_OBJECT(Character, LogicComponent)
  42. public:
  43. /// Construct.
  44. Character(Context* context);
  45. /// Register object factory and attributes.
  46. static void RegisterObject(Context* context);
  47. /// Handle startup. Called by LogicComponent base class.
  48. virtual void Start();
  49. /// Handle physics world update. Called by LogicComponent base class.
  50. virtual void FixedUpdate(float timeStep);
  51. /// Movement controls. Assigned by the main program each frame.
  52. Controls controls_;
  53. private:
  54. /// Handle physics collision event.
  55. void HandleNodeCollision(StringHash eventType, VariantMap& eventData);
  56. /// Grounded flag for movement.
  57. bool onGround_;
  58. /// Jump flag.
  59. bool okToJump_;
  60. /// In air timer. Due to possible physics inaccuracy, character can be off ground for max. 1/10 second and still be allowed to move.
  61. float inAirTimer_;
  62. };