ServerPlayer.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "server/entities/ServerPlayer.h"
  2. #include "utils/Logger.h"
  3. ServerPlayer::ServerPlayer(Server::Client& client) : client(client) {
  4. }
  5. void ServerPlayer::tick() {
  6. if(!history.canRead()) {
  7. skip = true;
  8. return;
  9. }
  10. skip = false;
  11. Entity::tick();
  12. ControllerPacket cp = history.read();
  13. Quaternion q = getRotation();
  14. Vector3 up(0.0f, 1.0f, 0.0f);
  15. Vector3 back = q * Vector3(0.0f, 0.0f, -1.0f);
  16. back[1] = 0.0f;
  17. back.normalize();
  18. Vector3 right = back.cross(up);
  19. Vector3 force;
  20. if(cp.has(ControllerPacket::Type::DOWN)) {
  21. force += back;
  22. }
  23. if(cp.has(ControllerPacket::Type::UP)) {
  24. force -= back;
  25. }
  26. if(cp.has(ControllerPacket::Type::LEFT)) {
  27. force -= right;
  28. }
  29. if(cp.has(ControllerPacket::Type::RIGHT)) {
  30. force += right;
  31. }
  32. if(force.squareLength() > 0.0f) {
  33. force.normalize();
  34. }
  35. addForce(force * 0.1f);
  36. if(cp.has(ControllerPacket::Type::JUMP) && isOnGround()) {
  37. addForce(up * (0.42f / 0.98f + 0.08f));
  38. }
  39. constexpr float rotationSpeed = 4.0f;
  40. if(cp.has(ControllerPacket::Type::CAM_LEFT)) {
  41. addLengthAngle(-rotationSpeed);
  42. }
  43. if(cp.has(ControllerPacket::Type::CAM_RIGHT)) {
  44. addLengthAngle(rotationSpeed);
  45. }
  46. if(cp.has(ControllerPacket::Type::CAM_UP)) {
  47. addWidthAngle(-rotationSpeed * 0.5f);
  48. }
  49. if(cp.has(ControllerPacket::Type::CAM_DOWN)) {
  50. addWidthAngle(rotationSpeed * 0.5f);
  51. }
  52. }
  53. void ServerPlayer::onChat(InPacket& in) {
  54. (void)in;
  55. }
  56. void ServerPlayer::send(OutPacket& out) {
  57. client.send(out);
  58. }
  59. void ServerPlayer::onControllerPacket(const ControllerPacket& cp) {
  60. history.write(cp);
  61. }