Client.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "Client.h"
  2. #include <iostream>
  3. #include "../block/Blocks.h"
  4. #include "../client/rendering/block/BlockRenderers.h"
  5. #include "rendering/entity/EntityRenderer.h"
  6. #include <cmath>
  7. using namespace std;
  8. Client::Client() : world(&chunkProvider)
  9. {
  10. BlockRegistry::registerBlocks();
  11. BlockRenderers::init();
  12. position.set(0, -9, 3);
  13. lengthAngle = 180;
  14. widthAngle = 0;
  15. camera.setPosition(position.getX(), position.getY(), position.getZ(), 0, 0);
  16. camera.storePosition();
  17. keyManager.map(KEY_LEFT, GLFW_KEY_A);
  18. keyManager.map(KEY_RIGHT, GLFW_KEY_D);
  19. keyManager.map(KEY_UP, GLFW_KEY_W);
  20. keyManager.map(KEY_DOWN, GLFW_KEY_S);
  21. keyManager.map(KEY_JUMP, GLFW_KEY_SPACE);
  22. keyManager.map(KEY_SNEAK, GLFW_KEY_LEFT_SHIFT);
  23. keyManager.map(KEY_TEST, GLFW_KEY_T);
  24. mouseManager.map(MOUSE_LEFT, GLFW_MOUSE_BUTTON_1);
  25. world.registerChunkListener(&chunkRenderer);
  26. world.updateDirtyChunks();
  27. Engine::setMouseTrapped(mouseTrapped);
  28. }
  29. Client::~Client()
  30. {
  31. }
  32. void Client::tick()
  33. {
  34. if(keyManager.isReleased(KEY_TEST))
  35. {
  36. mouseTrapped = !mouseTrapped;
  37. Engine::setMouseTrapped(mouseTrapped);
  38. }
  39. tps.update();
  40. camera.storePosition();
  41. float factor = 0.05f;
  42. if(keyManager.isDown(KEY_LEFT))
  43. {
  44. position.addMul(camera.getFlatLeft(), factor);
  45. }
  46. if(keyManager.isDown(KEY_RIGHT))
  47. {
  48. position.addMul(camera.getFlatRight(), factor);
  49. }
  50. if(keyManager.isDown(KEY_UP))
  51. {
  52. position.addMul(camera.getFlatFront(), factor);
  53. }
  54. if(keyManager.isDown(KEY_DOWN))
  55. {
  56. position.addMul(camera.getFlatBack(), factor);
  57. }
  58. if(keyManager.isDown(KEY_JUMP))
  59. {
  60. position.addMul(camera.getFlatUp(), factor);
  61. }
  62. if(keyManager.isDown(KEY_SNEAK))
  63. {
  64. position.addMul(camera.getFlatDown(), factor);
  65. }
  66. widthAngle += diffMouseY;
  67. if(widthAngle < -89)
  68. {
  69. widthAngle = -89;
  70. }
  71. else if(widthAngle > 89)
  72. {
  73. widthAngle = 89;
  74. }
  75. diffMouseY = 0;
  76. lengthAngle += diffMouseX;
  77. if(lengthAngle < 0)
  78. {
  79. lengthAngle += 360;
  80. camera.addToOldLengthAngle(360);
  81. }
  82. else if(lengthAngle > 360)
  83. {
  84. lengthAngle -= 360;
  85. camera.addToOldLengthAngle(-360);
  86. }
  87. diffMouseX = 0;
  88. camera.setPosition(position.getX(), position.getY(), position.getZ(), lengthAngle, widthAngle);
  89. entity.tick();
  90. mouseManager.tick();
  91. keyManager.tick();
  92. }
  93. void Client::render3DTick(float lag)
  94. {
  95. fps.update();
  96. camera.update(lag);
  97. Engine::setWorldViewMatrix(camera.getViewMatrix());
  98. shader.setToIdentity();
  99. Engine::setWorldModelMatrix(shader.getModelMatrix());
  100. chunkRenderer.renderTick(shader, camera, directRenderer, lag);
  101. entity.renderTick(shader, camera, directRenderer, lag);
  102. }
  103. void Client::render2DTick(float lag)
  104. {
  105. shader.setToIdentity();
  106. Engine::setOverlayModelMatrix(shader.getModelMatrix());
  107. string wusi;
  108. wusi = "FPS: " + to_string(fps.getUpdatesPerSecond());
  109. float y = directRenderer.drawString(10, 10, true, wusi);
  110. wusi = "TPS: " + to_string(tps.getUpdatesPerSecond());
  111. directRenderer.drawString(10, y, true, wusi);
  112. }
  113. void Client::onKeyEvent(int key, int scancode, int action, int mods)
  114. {
  115. if(action == GLFW_PRESS)
  116. {
  117. keyManager.press(key);
  118. }
  119. else if(action == GLFW_RELEASE)
  120. {
  121. keyManager.release(key);
  122. }
  123. }
  124. double Client::transformMouse(double x)
  125. {
  126. if(x >= -MOUSE_BORDER && x <= MOUSE_BORDER)
  127. {
  128. return x * x * x;
  129. }
  130. else if(x >= MOUSE_BORDER)
  131. {
  132. const double k = (3 * MOUSE_BORDER * MOUSE_BORDER);
  133. const double y = MOUSE_BORDER * MOUSE_BORDER * MOUSE_BORDER;
  134. const double d = y - k * MOUSE_BORDER;
  135. return k * x + d;
  136. }
  137. else
  138. {
  139. const double k = (3 * MOUSE_BORDER * MOUSE_BORDER);
  140. const double y = -MOUSE_BORDER * MOUSE_BORDER * MOUSE_BORDER;
  141. const double d = y - k * MOUSE_BORDER;
  142. return k * x + d;
  143. }
  144. }
  145. void Client::onMouseMove(double x, double y)
  146. {
  147. if(mouseTrapped)
  148. {
  149. diffMouseX = transformMouse(oldMouseX - x) * (1 / (MOUSE_BORDER * MOUSE_BORDER * 3));
  150. diffMouseY = transformMouse(oldMouseY - y) * (1 / (MOUSE_BORDER * MOUSE_BORDER * 3));
  151. oldMouseX = x;
  152. oldMouseY = y;
  153. }
  154. }
  155. void Client::onMouseClick(int button, int action, int mods)
  156. {
  157. if(action == GLFW_PRESS)
  158. {
  159. mouseManager.press(button);
  160. }
  161. else if(action == GLFW_RELEASE)
  162. {
  163. mouseManager.release(button);
  164. }
  165. }