Game.cpp 6.7 KB

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