Game.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "client/Game.h"
  2. #include "client/utils/Utils.h"
  3. #include "rendering/Renderer.h"
  4. Game::Game(const Control& control, const Camera& camera, Ray& ray, const Clock& fps, const Clock& tps,
  5. RenderSettings& renderSettings) :
  6. control(control), camera(camera), ray(ray), fps(fps), tps(tps), renderSettings(renderSettings), lengthAngle(20.0f),
  7. widthAngle(35.0f), texture("resources/textures.png") {
  8. for(int x = -6; x <= 6; x++) {
  9. for(int y = -6; y <= 6; y++) {
  10. for(int z = -6; z <= 6; z++) {
  11. if(x * x + y * y + z * z < 16) {
  12. addCube(x, y, z,
  13. x * x + (y - 1) * (y - 1) + z * z >= 16,
  14. x * x + (y + 1) * (y + 1) + z * z >= 16 && !(x == 0 && y == 3 && z == 0),
  15. (x - 1) * (x - 1) + y * y + z * z >= 16,
  16. (x + 1) * (x + 1) + y * y + z * z >= 16,
  17. x * x + y * y + (z + 1) * (z + 1) >= 16,
  18. x * x + y * y + (z - 1) * (z - 1) >= 16);
  19. }
  20. }
  21. }
  22. }
  23. addCube(0, 4, 0, false, true, true, true, true, true);
  24. pos.set(0, 10, 0);
  25. m.build();
  26. }
  27. void Game::addCube(float x, float y, float z, bool bottom, bool top, bool left, bool right, bool front, bool back) {
  28. const float ERROR = 1.0f / 1024.0f;
  29. if(bottom) {
  30. m.add( {x - ERROR, y - ERROR, z - ERROR, 0.125f, 0.0f, 0, -1, 0});
  31. m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0f, 0, -1, 0});
  32. m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.125f, 0.0625f, 0, -1, 0});
  33. m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0f, 0, -1, 0});
  34. m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 0, -1, 0});
  35. m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.125f, 0.0625f, 0, -1, 0});
  36. }
  37. if(top) {
  38. m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 1, 0});
  39. m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 1, 0});
  40. m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.3125f, 0.0f, 0, 1, 0});
  41. m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.3125f, 0.0f, 0, 1, 0});
  42. m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 1, 0});
  43. m.add( {x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.3125f, 0.0625f, 0, 1, 0});
  44. }
  45. if(left) {
  46. m.add( {x - ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, -1, 0, 0});
  47. m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, -1, 0, 0});
  48. m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, -1, 0, 0});
  49. m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, -1, 0, 0});
  50. m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0f, -1, 0, 0});
  51. m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, -1, 0, 0});
  52. }
  53. if(right) {
  54. m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.25f, 0.0625f, 1, 0, 0});
  55. m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 1, 0, 0});
  56. m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 1, 0, 0});
  57. m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 1, 0, 0});
  58. m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 1, 0, 0});
  59. m.add( {x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 1, 0, 0});
  60. }
  61. if(front) {
  62. m.add( {x - ERROR, y - ERROR, z + 1 + ERROR, 0.1875f, 0.0625f, 0, 0, 1});
  63. m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 0, 1});
  64. m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 0, 0, 1});
  65. m.add( {x + 1 + ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.25f, 0.0f, 0, 0, 1});
  66. m.add( {x - ERROR, y + 1 + ERROR, z + 1 + ERROR, 0.1875f, 0.0f, 0, 0, 1});
  67. m.add( {x + 1 + ERROR, y - ERROR, z + 1 + ERROR, 0.25f, 0.0625f, 0, 0, 1});
  68. }
  69. if(back) {
  70. m.add( {x - ERROR, y - ERROR, z - ERROR, 0.25f, 0.0625f, 0, 0, -1});
  71. m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 0, -1});
  72. m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, 0, 0, -1});
  73. m.add( {x + 1 + ERROR, y + 1 + ERROR, z - ERROR, 0.1875f, 0.0f, 0, 0, -1});
  74. m.add( {x + 1 + ERROR, y - ERROR, z - ERROR, 0.1875f, 0.0625f, 0, 0, -1});
  75. m.add( {x - ERROR, y + 1 + ERROR, z - ERROR, 0.25f, 0.0f, 0, 0, -1});
  76. }
  77. }
  78. float testAngle = 0.0f;
  79. void Game::tick() {
  80. const float speed = 0.25f;
  81. if(control.keys.down.isDown()) {
  82. pos.addMul(camera.getFlatBack(), speed);
  83. }
  84. if(control.keys.up.isDown()) {
  85. pos.addMul(camera.getFlatBack(), -speed);
  86. }
  87. if(control.keys.left.isDown()) {
  88. pos.addMul(camera.getFlatRight(), -speed);
  89. }
  90. if(control.keys.right.isDown()) {
  91. pos.addMul(camera.getFlatRight(), speed);
  92. }
  93. if(control.keys.jump.isDown()) {
  94. pos.addMul(camera.getFlatUp(), speed);
  95. }
  96. if(control.keys.sneak.isDown()) {
  97. pos.addMul(camera.getFlatUp(), -speed);
  98. }
  99. const float rotation = 5.0f;
  100. if(control.keys.camLeft.isDown()) {
  101. lengthAngle += rotation;
  102. }
  103. if(control.keys.camRight.isDown()) {
  104. lengthAngle -= rotation;
  105. }
  106. if(control.keys.camUp.isDown() && widthAngle - rotation > -90.0f) {
  107. widthAngle -= rotation;
  108. }
  109. if(control.keys.camDown.isDown() && widthAngle + rotation < 90.0f) {
  110. widthAngle += rotation;
  111. }
  112. ray.store();
  113. ray.set(pos, lengthAngle, widthAngle);
  114. if(control.keys.test5.getDownTime() == 1) {
  115. renderSettings.shadows = !renderSettings.shadows;
  116. }
  117. /*if(control.keys.test.isDown()) {
  118. renderSettings.testRadius /= 0.95f;
  119. }
  120. if(control.keys.test2.isDown()) {
  121. renderSettings.testRadius *= 0.95f;
  122. }
  123. if(control.keys.test3.isDown()) {
  124. renderSettings.testBias /= 0.95f;
  125. }
  126. if(control.keys.test4.isDown()) {
  127. renderSettings.testBias *= 0.95f;
  128. }*/
  129. testAngle += 5.0f;
  130. }
  131. void Game::renderWorld(float lag, Renderer& renderer) const {
  132. (void) lag;
  133. (void) renderer;
  134. renderer.translateTo(0.0f, 0.0f, -0.5f)
  135. .rotateY(interpolate(lag, testAngle - 5.0, testAngle))
  136. .translate(-0.5f, 0.0f, -0.5f)
  137. .update();
  138. texture.bind(0);
  139. m.draw();
  140. }
  141. void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) const {
  142. (void) lag;
  143. renderer.scale(1.0f).update();
  144. char buffer[50];
  145. snprintf(buffer, 50, "FPS: %.2f", fps.getUpdatesPerSecond());
  146. fr.drawString(10, 10, buffer);
  147. snprintf(buffer, 50, "TPS: %.2f", tps.getUpdatesPerSecond());
  148. fr.drawString(10, 20, buffer);
  149. snprintf(buffer, 50, "Bias: %.6f", renderSettings.testBias);
  150. fr.drawString(10, 30, buffer);
  151. snprintf(buffer, 50, "Radius: %.6f", renderSettings.testRadius);
  152. fr.drawString(10, 40, buffer);
  153. snprintf(buffer, 50, "Shadows: %d", renderSettings.shadows);
  154. fr.drawString(10, 50, buffer);
  155. }
  156. bool Game::isRunning() const {
  157. return true;
  158. }