Player.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "client/Player.h"
  2. #include "client/Chat.h"
  3. #include "client/Controls.h"
  4. #include "client/Main.h"
  5. #include "client/World.h"
  6. #include "math/View.h"
  7. static BufferedValue<Vector3> position = Vector3(8.0f, 30.0f, 8.0f);
  8. static BufferedValue<float> widthAngle = 0.0f;
  9. static BufferedValue<float> lengthAngle = 0.0f;
  10. static Vector3 velocity;
  11. static Vector3 acceleration;
  12. static bool onGround = false;
  13. static View view;
  14. static void tickInput() {
  15. Vector3 back = view.getBack();
  16. back[1] = 0.0f;
  17. back.normalize();
  18. Vector3 right = view.getRight();
  19. right[1] = 0.0f;
  20. right.normalize();
  21. Vector3 force;
  22. if(Controls::isDown(Controls::down)) {
  23. force += back;
  24. }
  25. if(Controls::isDown(Controls::up)) {
  26. force -= back;
  27. }
  28. if(Controls::isDown(Controls::left)) {
  29. force -= right;
  30. }
  31. if(Controls::isDown(Controls::right)) {
  32. force += right;
  33. }
  34. if(force.squareLength() > 0.0f) {
  35. force.normalize();
  36. }
  37. acceleration += force * 0.1f;
  38. if(Controls::isDown(Controls::jump) && onGround) {
  39. acceleration[1] += (0.42f / 0.98f + 0.08f);
  40. }
  41. if(Window::isCursorTrapped()) {
  42. Vector2 diff = (Window::Controls::getLastMousePosition() -
  43. Window::Controls::getMousePosition()) *
  44. 0.1f;
  45. widthAngle += diff[1];
  46. lengthAngle += diff[0];
  47. if(widthAngle > 89.0f) {
  48. widthAngle = 89.0f;
  49. }
  50. if(widthAngle < -89.0f) {
  51. widthAngle = -89.0f;
  52. }
  53. }
  54. }
  55. void Player::tick() {
  56. position.update();
  57. widthAngle.update();
  58. lengthAngle.update();
  59. view.updateDirections(lengthAngle, widthAngle);
  60. if(!Chat::isActive()) {
  61. tickInput();
  62. }
  63. velocity += acceleration;
  64. velocity *= Vector3(0.686f, 0.98f, 0.686f);
  65. acceleration = Vector3(0.0f, -0.08f, 0.0f);
  66. Box box(Vector3(0.8f, 0.8f, 0.8f));
  67. box = box.offset(position - Vector3(0.4f, 0.0f, 0.4f));
  68. Vector3 move = World::limitMove(box, velocity);
  69. if(move[1] + position[1] < 0.0f) {
  70. move[1] = -position[1];
  71. }
  72. position += move;
  73. onGround = move[1] == 0.0f && velocity[1] < 0.0f;
  74. velocity = move;
  75. }
  76. Vector3 Player::getPosition(float lag) {
  77. return position.get(lag);
  78. }
  79. float Player::getLengthAngle(float lag) {
  80. return lengthAngle.get(lag);
  81. }
  82. float Player::getWidthAngle(float lag) {
  83. return widthAngle.get(lag);
  84. }
  85. Vector3 Player::getLook(float lag) {
  86. view.updateDirections(lengthAngle.get(lag), widthAngle.get(lag));
  87. return view.getFront();
  88. }