Game.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "client/Game.h"
  2. #include "client/GameClient.h"
  3. #include "client/World.h"
  4. #include "client/gui/StartGUI.h"
  5. #include "client/input/Controller.h"
  6. #include "client/rendering/Engine.h"
  7. #include "common/network/Packets.h"
  8. #include "common/network/toserver/PlayerUpdatePacket.h"
  9. #include "utils/Logger.h"
  10. #include "utils/Utils.h"
  11. Entity Game::player;
  12. typedef void (*State)();
  13. static State tickState;
  14. static State renderState;
  15. static BaseGUI baseGUI;
  16. static StartGUI startGUI;
  17. static void tickConnectedState() {
  18. Game::player.skip = false;
  19. World::tick();
  20. GameClient::consumeEvents();
  21. Quaternion q = Game::player.getRotation();
  22. Vector3 up(0.0f, 1.0f, 0.0f);
  23. Vector3 back = q * Vector3(0.0f, 0.0f, -1.0f);
  24. back[1] = 0.0f;
  25. back.normalize();
  26. Vector3 right = back.cross(up);
  27. constexpr float rotationSpeed = 4.0f;
  28. Vector3 force;
  29. if(Controller::isDown(Controller::down)) {
  30. force += back;
  31. }
  32. if(Controller::isDown(Controller::up)) {
  33. force -= back;
  34. }
  35. if(Controller::isDown(Controller::left)) {
  36. force -= right;
  37. }
  38. if(Controller::isDown(Controller::right)) {
  39. force += right;
  40. }
  41. if(force.squareLength() > 0.0f) {
  42. force.normalize();
  43. }
  44. Game::player.addForce(force * Game::player.speed);
  45. if(Controller::isDown(Controller::jump) && Game::player.isOnGround()) {
  46. Game::player.jump();
  47. }
  48. if(Controller::isDown(Controller::camLeft)) {
  49. Game::player.addLengthAngle(-rotationSpeed);
  50. }
  51. if(Controller::isDown(Controller::camRight)) {
  52. Game::player.addLengthAngle(rotationSpeed);
  53. }
  54. if(Controller::isDown(Controller::camUp)) {
  55. Game::player.addWidthAngle(-rotationSpeed * 0.5f);
  56. }
  57. if(Controller::isDown(Controller::camDown)) {
  58. Game::player.addWidthAngle(rotationSpeed * 0.5f);
  59. }
  60. PlayerUpdatePacket p(Game::player);
  61. OutPacket out = OutPacket::reliable(PlayerUpdatePacket::getSize());
  62. p.write(out);
  63. GameClient::send(out);
  64. }
  65. static void renderConnectedState() {
  66. }
  67. static void tickConnectState() {
  68. startGUI.tick();
  69. StartGUI::Address a;
  70. if(startGUI.getAddress(a)) {
  71. Error error = GameClient::connect(a, 11196, 3000);
  72. if(error.has()) {
  73. LOG_INFO(error.message);
  74. } else {
  75. LOG_INFO("connected");
  76. tickState = tickConnectedState;
  77. renderState = renderConnectedState;
  78. }
  79. }
  80. }
  81. static void renderConnectState() {
  82. startGUI.render();
  83. }
  84. bool Game::init() {
  85. Block::init();
  86. if(World::init()) {
  87. return true;
  88. }
  89. Controller::init();
  90. tickState = tickConnectState;
  91. renderState = renderConnectState;
  92. Game::player.setPosition(Vector3(0.0f, 2.0f, 0.0f));
  93. World::addEntity(&player);
  94. return false;
  95. }
  96. void Game::tick() {
  97. tickState();
  98. }
  99. void Game::renderWorld() {
  100. Engine::matrix.update(player.getRenderPosition(Engine::lag),
  101. player.getRenderRotation(Engine::lag));
  102. World::render();
  103. }
  104. void Game::renderOverlay() {
  105. renderState();
  106. Engine::matrix.identity().scale(2.0f).update();
  107. StringBuffer<100> s;
  108. s.append("FPS: &074")
  109. .append(Window::getFramesPerSecond())
  110. .append(" &999TPS: &722")
  111. .append(Window::getTicksPerSecond());
  112. Engine::renderer.renderString(Vector2(10.0f, 10.0f), s);
  113. s.clear().append("a = ").append(player.acceleration);
  114. Engine::renderer.renderString(Vector2(10.0f, 20.0f), s);
  115. s.clear().append("v = ").append(player.velocity);
  116. Engine::renderer.renderString(Vector2(10.0f, 30.0f), s);
  117. }
  118. void Game::onEntityUpdate(EntityUpdatePacket& p) {
  119. LOG_DEBUG("set");
  120. player.setPosition(p.position);
  121. player.velocity = p.velocity;
  122. }