Game.cpp 6.9 KB

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