Game.cpp 3.2 KB

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