Player.cpp 2.5 KB

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