Game.cpp 3.8 KB

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