Client.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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(16, 16, 16);
  13. lengthAngle = 60;
  14. widthAngle = 60;
  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.5f;
  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::render3DShadowTick(float lag)
  94. {
  95. camera.update(lag);
  96. Engine::setWorldViewMatrix(camera.getViewMatrix());
  97. shader.setToIdentity();
  98. Engine::setWorldModelMatrix(shader.getModelMatrix());
  99. chunkRenderer.renderTick(shader, camera, directRenderer, lag);
  100. entity.renderTick(shader, camera, directRenderer, lag);
  101. }
  102. void Client::render3DTick(float lag)
  103. {
  104. fps.update();
  105. render3DShadowTick(lag);
  106. }
  107. void Client::render2DTick(float lag)
  108. {
  109. shader.setToIdentity();
  110. Engine::setOverlayModelMatrix(shader.getModelMatrix());
  111. string wusi;
  112. wusi = "FPS: " + to_string(fps.getUpdatesPerSecond());
  113. float y = directRenderer.drawString(10, 10, true, wusi);
  114. wusi = "TPS: " + to_string(tps.getUpdatesPerSecond());
  115. directRenderer.drawString(10, y, true, wusi);
  116. }
  117. void Client::onKeyEvent(int key, int scancode, int action, int mods)
  118. {
  119. if(action == GLFW_PRESS)
  120. {
  121. keyManager.press(key);
  122. }
  123. else if(action == GLFW_RELEASE)
  124. {
  125. keyManager.release(key);
  126. }
  127. }
  128. double Client::transformMouse(double x)
  129. {
  130. if(x >= -MOUSE_BORDER && x <= MOUSE_BORDER)
  131. {
  132. return x * x * x;
  133. }
  134. else if(x >= MOUSE_BORDER)
  135. {
  136. const double k = (3 * MOUSE_BORDER * MOUSE_BORDER);
  137. const double y = MOUSE_BORDER * MOUSE_BORDER * MOUSE_BORDER;
  138. const double d = y - k * MOUSE_BORDER;
  139. return k * x + d;
  140. }
  141. else
  142. {
  143. const double k = (3 * MOUSE_BORDER * MOUSE_BORDER);
  144. const double y = -MOUSE_BORDER * MOUSE_BORDER * MOUSE_BORDER;
  145. const double d = y - k * MOUSE_BORDER;
  146. return k * x + d;
  147. }
  148. }
  149. void Client::onMouseMove(double x, double y)
  150. {
  151. if(mouseTrapped)
  152. {
  153. diffMouseX = transformMouse(oldMouseX - x) * (1 / (MOUSE_BORDER * MOUSE_BORDER * 3));
  154. diffMouseY = transformMouse(oldMouseY - y) * (1 / (MOUSE_BORDER * MOUSE_BORDER * 3));
  155. oldMouseX = x;
  156. oldMouseY = y;
  157. }
  158. }
  159. void Client::onMouseClick(int button, int action, int mods)
  160. {
  161. if(action == GLFW_PRESS)
  162. {
  163. mouseManager.press(button);
  164. }
  165. else if(action == GLFW_RELEASE)
  166. {
  167. mouseManager.release(button);
  168. }
  169. }