Game.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. Game::world.tick();
  22. }
  23. static void renderConnectedState() {
  24. }
  25. static void tickConnectState() {
  26. startGUI.tick();
  27. StartGUI::Address a;
  28. if(startGUI.getAddress(a)) {
  29. Error error = GameClient::connect(a, 11196, 3000);
  30. if(error.has()) {
  31. LOG_INFO(error.message);
  32. } else {
  33. LOG_INFO("connected");
  34. tickState = tickConnectedState;
  35. renderState = renderConnectedState;
  36. }
  37. }
  38. }
  39. static void renderConnectState() {
  40. startGUI.render();
  41. }
  42. bool Game::init() {
  43. if(worldRenderer.init()) {
  44. return true;
  45. }
  46. controller.init();
  47. tickState = tickConnectState;
  48. renderState = renderConnectState;
  49. player.setPosition(Vector3(0.0f, 30.0f, 0.0f));
  50. world.addPlayer(&player);
  51. return false;
  52. }
  53. void Game::tick() {
  54. tickState();
  55. Quaternion q = player.getRotation();
  56. Vector3 up(0.0f, 1.0f, 0.0f);
  57. Vector3 back = q * Vector3(0.0f, 0.0f, -1.0f);
  58. back[1] = 0.0f;
  59. back.normalize();
  60. Vector3 right = back.cross(up);
  61. constexpr float speed = 0.1f;
  62. if(controller.down.isDown()) {
  63. player.addForce(back * speed);
  64. }
  65. if(controller.up.isDown()) {
  66. player.addForce(back * -speed);
  67. }
  68. if(controller.left.isDown()) {
  69. player.addForce(right * -speed);
  70. }
  71. if(controller.right.isDown()) {
  72. player.addForce(right * speed);
  73. }
  74. if(controller.jump.isDown() && player.isOnGround()) {
  75. player.addForce(up * 0.5f);
  76. }
  77. if(controller.sneak.isDown()) {
  78. player.addForce(up * -speed);
  79. }
  80. constexpr float rotationSpeed = 4.0f;
  81. if(controller.camLeft.isDown()) {
  82. player.addLengthAngle(-rotationSpeed);
  83. }
  84. if(controller.camRight.isDown()) {
  85. player.addLengthAngle(rotationSpeed);
  86. }
  87. if(controller.camUp.isDown()) {
  88. player.addWidthAngle(-rotationSpeed * 0.5f);
  89. }
  90. if(controller.camDown.isDown()) {
  91. player.addWidthAngle(rotationSpeed * 0.5f);
  92. }
  93. }
  94. void Game::renderWorld() {
  95. Engine::matrix.update(player.getRenderPosition(Engine::lag),
  96. player.getRenderRotation(Engine::lag));
  97. worldRenderer.render();
  98. }
  99. void Game::renderOverlay() {
  100. renderState();
  101. Engine::matrix.identity().scale(2.0f).update();
  102. StringBuffer<100> s;
  103. s.append("FPS: &074")
  104. .append(Engine::getFrameClock().getUpdatesPerSecond())
  105. .append(" &999TPS: &722")
  106. .append(Engine::getTickClock().getUpdatesPerSecond());
  107. Engine::renderer.renderString(Vector2(10.0f, 10.0f), s);
  108. }