Game.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "rendering/renderer/WorldRenderer.h"
  7. #include "utils/Logger.h"
  8. #include "utils/Utils.h"
  9. BlockRegistry Game::blockRegistry;
  10. World Game::world{blockRegistry};
  11. static WorldRenderer worldRenderer{Game::world};
  12. Controller Game::controller;
  13. typedef void (*State)();
  14. static State tickState;
  15. static State renderState;
  16. static BaseGUI baseGUI;
  17. static StartGUI startGUI;
  18. static Vector3 lastPos;
  19. static Vector3 pos;
  20. static Quaternion lastRotation;
  21. static Quaternion rotation;
  22. static void tickConnectedState() {
  23. GameClient::consumeEvents();
  24. }
  25. static void renderConnectedState() {
  26. }
  27. static void tickConnectState() {
  28. startGUI.tick();
  29. StartGUI::Address a;
  30. if(startGUI.getAddress(a)) {
  31. Error error = GameClient::connect(a, 11196, 3000);
  32. if(error.has()) {
  33. LOG_INFO(error.message);
  34. } else {
  35. LOG_INFO("connected");
  36. tickState = tickConnectedState;
  37. renderState = renderConnectedState;
  38. }
  39. }
  40. }
  41. static void renderConnectState() {
  42. startGUI.render();
  43. }
  44. bool Game::init() {
  45. if(worldRenderer.init()) {
  46. return true;
  47. }
  48. controller.init();
  49. tickState = tickConnectState;
  50. renderState = renderConnectState;
  51. pos = Vector3(0.0f, 30.0f, 0.0f);
  52. rotation = Quaternion(Vector3(1.0f, 0.0f, 0.0f), 30) * rotation;
  53. rotation = Quaternion(Vector3(0.0f, 1.0f, 0.0f), 30) * rotation;
  54. return false;
  55. }
  56. void Game::tick() {
  57. tickState();
  58. lastRotation = rotation;
  59. lastPos = pos;
  60. Vector3 right = rotation * Vector3(1.0f, 0.0f, 0.0f);
  61. Vector3 up = rotation * Vector3(0.0f, 1.0f, 0.0f);
  62. Vector3 back = rotation * Vector3(0.0f, 0.0f, -1.0f);
  63. const float speed = 2.0f;
  64. if(controller.down.isDown()) {
  65. pos += back * speed;
  66. }
  67. if(controller.up.isDown()) {
  68. pos -= back * speed;
  69. }
  70. if(controller.left.isDown()) {
  71. pos -= right * speed;
  72. }
  73. if(controller.right.isDown()) {
  74. pos += right * speed;
  75. }
  76. if(controller.jump.isDown()) {
  77. pos += up * speed;
  78. }
  79. if(controller.sneak.isDown()) {
  80. pos -= up * speed;
  81. }
  82. const float rotationSpeed = 5.0f;
  83. if(controller.camLeft.isDown()) {
  84. rotation = Quaternion(up, -rotationSpeed) * rotation;
  85. }
  86. if(controller.camRight.isDown()) {
  87. rotation = Quaternion(up, rotationSpeed) * rotation;
  88. }
  89. if(controller.camUp.isDown()) {
  90. rotation = Quaternion(right, -rotationSpeed) * rotation;
  91. }
  92. if(controller.camDown.isDown()) {
  93. rotation = Quaternion(right, rotationSpeed) * rotation;
  94. }
  95. }
  96. void Game::renderWorld() {
  97. Engine::matrix.update(Utils::interpolate(lastPos, pos, Engine::lag),
  98. lastRotation.lerp(Engine::lag, rotation));
  99. worldRenderer.render();
  100. }
  101. void Game::renderOverlay() {
  102. renderState();
  103. Engine::matrix.identity().scale(2.0f).update();
  104. StringBuffer<100> s;
  105. s.append("FPS: &074")
  106. .append(Engine::getFrameClock().getUpdatesPerSecond())
  107. .append(" &999TPS: &722")
  108. .append(Engine::getTickClock().getUpdatesPerSecond());
  109. Engine::renderer.renderString(Vector2(10.0f, 10.0f), s);
  110. }