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 "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. Player Game::player;
  14. typedef void (*State)();
  15. static State tickState;
  16. static State renderState;
  17. static BaseGUI baseGUI;
  18. static StartGUI startGUI;
  19. static void tickConnectedState() {
  20. GameClient::consumeEvents();
  21. }
  22. static void renderConnectedState() {
  23. }
  24. static void tickConnectState() {
  25. startGUI.tick();
  26. StartGUI::Address a;
  27. if(startGUI.getAddress(a)) {
  28. Error error = GameClient::connect(a, 11196, 3000);
  29. if(error.has()) {
  30. LOG_INFO(error.message);
  31. } else {
  32. LOG_INFO("connected");
  33. tickState = tickConnectedState;
  34. renderState = renderConnectedState;
  35. }
  36. }
  37. }
  38. static void renderConnectState() {
  39. startGUI.render();
  40. }
  41. bool Game::init() {
  42. if(worldRenderer.init()) {
  43. return true;
  44. }
  45. controller.init();
  46. tickState = tickConnectState;
  47. renderState = renderConnectState;
  48. player.position = Vector3(0.0f, 30.0f, 0.0f);
  49. player.lastLengthAngle = 0.0f;
  50. player.lengthAngle = 0.0f;
  51. player.lastWidthAngle = 0.0f;
  52. player.widthAngle = 0.0f;
  53. world.addPlayer(&player);
  54. return false;
  55. }
  56. void Game::tick() {
  57. tickState();
  58. world.preTick();
  59. Quaternion q(Vector3(0.0f, 1.0f, 0.0f), player.lengthAngle);
  60. q *= Quaternion(Vector3(1.0f, 0.0f, 0.0f), player.widthAngle);
  61. Vector3 up(0.0f, 1.0f, 0.0f);
  62. Vector3 back = q * Vector3(0.0f, 0.0f, -1.0f);
  63. // back.setAngles(player.widthAngle, player.lengthAngle);
  64. // back[1] = 0.0f;
  65. // back.normalize();
  66. Vector3 right = back.cross(up);
  67. const float speed = 0.25f;
  68. if(controller.down.isDown()) {
  69. player.acceleration += back * speed;
  70. }
  71. if(controller.up.isDown()) {
  72. player.acceleration -= back * speed;
  73. }
  74. if(controller.left.isDown()) {
  75. player.acceleration -= right * speed;
  76. }
  77. if(controller.right.isDown()) {
  78. player.acceleration += right * speed;
  79. }
  80. if(controller.jump.isDown()) {
  81. player.acceleration += up * speed;
  82. }
  83. if(controller.sneak.isDown()) {
  84. player.acceleration -= up * speed;
  85. }
  86. const float rotationSpeed = 5.0f;
  87. if(controller.camLeft.isDown()) {
  88. player.lengthAngle -= rotationSpeed;
  89. }
  90. if(controller.camRight.isDown()) {
  91. player.lengthAngle += rotationSpeed;
  92. }
  93. if(controller.camUp.isDown() &&
  94. player.widthAngle - rotationSpeed > -90.0f) {
  95. player.widthAngle -= rotationSpeed;
  96. }
  97. if(controller.camDown.isDown() &&
  98. player.widthAngle + rotationSpeed < 90.0f) {
  99. player.widthAngle += rotationSpeed;
  100. }
  101. world.tick();
  102. }
  103. void Game::renderWorld() {
  104. Quaternion q(Vector3(0.0f, 1.0f, 0.0f),
  105. Utils::interpolate(player.lastLengthAngle, player.lengthAngle,
  106. Engine::lag));
  107. q *= Quaternion(Vector3(1.0f, 0.0f, 0.0f),
  108. Utils::interpolate(player.lastWidthAngle, player.widthAngle,
  109. Engine::lag));
  110. Engine::matrix.update(
  111. Utils::interpolate(player.lastPosition, player.position, Engine::lag),
  112. q);
  113. worldRenderer.render();
  114. }
  115. void Game::renderOverlay() {
  116. renderState();
  117. Engine::matrix.identity().scale(2.0f).update();
  118. StringBuffer<100> s;
  119. s.append("FPS: &074")
  120. .append(Engine::getFrameClock().getUpdatesPerSecond())
  121. .append(" &999TPS: &722")
  122. .append(Engine::getTickClock().getUpdatesPerSecond());
  123. Engine::renderer.renderString(Vector2(10.0f, 10.0f), s);
  124. }