Game.cpp 4.3 KB

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