Game.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "client/Game.h"
  2. #include "client/packets/WorldPackets.h"
  3. #include "common/network/Packets.h"
  4. #include "utils/Logger.h"
  5. #include "utils/Utils.h"
  6. BlockRegistry Game::blockRegistry;
  7. Game::Game(TextInput*& textInput, const Controller& controller,
  8. const Clock& fps, const Clock& tps, RenderSettings& settings,
  9. const Size& size, Client& client)
  10. : controller(controller), fps(fps), tps(tps), renderSettings(settings),
  11. size(size), client(client), tickState(&Game::tickConnectState),
  12. renderState(&Game::renderConnectState),
  13. baseGUI(size, textInput, controller), startGUI(baseGUI),
  14. world(blockRegistry), 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. }
  19. void Game::tick() {
  20. (this->*tickState)();
  21. lastRotation = rotation;
  22. lastPos = pos;
  23. Vector3 right = rotation * Vector3(1.0f, 0.0f, 0.0f);
  24. Vector3 up = rotation * Vector3(0.0f, 1.0f, 0.0f);
  25. Vector3 back = rotation * Vector3(0.0f, 0.0f, -1.0f);
  26. const float speed = 2.0f;
  27. if(controller.down.isDown()) {
  28. pos += back * speed;
  29. }
  30. if(controller.up.isDown()) {
  31. pos -= back * speed;
  32. }
  33. if(controller.left.isDown()) {
  34. pos -= right * speed;
  35. }
  36. if(controller.right.isDown()) {
  37. pos += right * speed;
  38. }
  39. if(controller.jump.isDown()) {
  40. pos += up * speed;
  41. }
  42. if(controller.sneak.isDown()) {
  43. pos -= up * speed;
  44. }
  45. const float rotationSpeed = 5.0f;
  46. if(controller.camLeft.isDown()) {
  47. rotation = Quaternion(up, -rotationSpeed) * rotation;
  48. }
  49. if(controller.camRight.isDown()) {
  50. rotation = Quaternion(up, rotationSpeed) * rotation;
  51. }
  52. if(controller.camUp.isDown()) {
  53. rotation = Quaternion(right, -rotationSpeed) * rotation;
  54. }
  55. if(controller.camDown.isDown()) {
  56. rotation = Quaternion(right, rotationSpeed) * rotation;
  57. }
  58. }
  59. void Game::renderWorld(float lag, ShaderMatrix& sm) {
  60. sm.update(Utils::interpolate(lastPos, pos, lag),
  61. lastRotation.lerp(lag, rotation));
  62. worldRenderer.render(lag, sm);
  63. }
  64. void Game::renderOverlay(float lag, ShaderMatrix& sm, Renderer& r) {
  65. (this->*renderState)(lag, sm, r);
  66. sm.identity().scale(2.0f).update();
  67. StringBuffer<100> s;
  68. s.append("FPS: &074")
  69. .append(fps.getUpdatesPerSecond())
  70. .append(" &999TPS: &722")
  71. .append(tps.getUpdatesPerSecond());
  72. r.renderString(Vector2(10.0f, 10.0f), s);
  73. }
  74. bool Game::isRunning() const {
  75. return true;
  76. }
  77. void Game::onConnect() {
  78. }
  79. void Game::onDisconnect() {
  80. }
  81. void Game::onPacket(InPacket& in) {
  82. uint16 id;
  83. if(in.readU16(id)) {
  84. return;
  85. }
  86. switch(id) {
  87. case S_CHAT:
  88. {
  89. StringBuffer<256> s;
  90. in.readString(s);
  91. puts(s);
  92. break;
  93. }
  94. case S_WORLD_SEGMENT: WorldPackets::receiveChunk(world, in); break;
  95. }
  96. }
  97. void Game::tickConnectState() {
  98. startGUI.tick();
  99. StartGUI::Address a;
  100. if(startGUI.getAddress(a)) {
  101. if(client.connect(a, 11196, 3000)) {
  102. LOG_INFO(client.getError());
  103. } else {
  104. LOG_INFO("connected");
  105. tickState = &Game::tickConnectedState;
  106. renderState = &Game::renderConnectedState;
  107. }
  108. }
  109. }
  110. void Game::tickConnectedState() {
  111. client.consumeEvents(*this);
  112. }
  113. void Game::renderConnectState(float lag, ShaderMatrix& sm, Renderer& r) {
  114. startGUI.render(lag, sm, r);
  115. }
  116. void Game::renderConnectedState(float lag, ShaderMatrix& sm, Renderer& r) {
  117. (void)lag;
  118. (void)sm;
  119. (void)r;
  120. }