Game.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <cmath>
  2. #include "client/Game.h"
  3. #include "client/utils/Utils.h"
  4. #include "rendering/Renderer.h"
  5. #include "common/utils/String.h"
  6. #include "common/utils/Random.h"
  7. Game::Game(const Control& control, const Camera& camera, Ray& ray, const Clock& fps, const Clock& tps,
  8. RenderSettings& renderSettings) :
  9. control(control), camera(camera), ray(ray), fps(fps), tps(tps), renderSettings(renderSettings), lengthAngle(240.0f),
  10. widthAngle(20.0f), world(blockRegistry), worldRenderer(world), pointIndex(0), moveSpeed(0.25f), movedLength(0.0f),
  11. mode(Mode::AUTO) {
  12. Random r(0);
  13. float h = World::WORLD_SIZE * 0.75f;
  14. float mid = World::WORLD_SIZE * 0.5f;
  15. float randLength = World::WORLD_SIZE * 0.125f;
  16. pos.set(0, h, 0);
  17. for(uint i = 0; i < cameraPoints.getCapacity(); i++) {
  18. Vector offset(mid, h, mid);
  19. offset.add(Vector(r.nextFloat(randLength), r.nextFloat(randLength), r.nextFloat(randLength)));
  20. Vector v;
  21. v.setAngles(i * 360.0f / cameraPoints.getCapacity(), 0.0f).mul(mid * 0.5f).add(offset);
  22. cameraPoints.add( {v, 0.0f});
  23. }
  24. for(uint i = 0; i < cameraPoints.getLength(); i++) {
  25. cameraPoints[i].distance = distance(i, 20);
  26. }
  27. }
  28. void Game::tick() {
  29. if(mode == Mode::PLAYER) {
  30. const float speed = 0.25f;
  31. if(control.keys.down.isDown()) {
  32. pos.addMul(camera.getFlatBack(), speed);
  33. }
  34. if(control.keys.up.isDown()) {
  35. pos.addMul(camera.getFlatBack(), -speed);
  36. }
  37. if(control.keys.left.isDown()) {
  38. pos.addMul(camera.getFlatRight(), -speed);
  39. }
  40. if(control.keys.right.isDown()) {
  41. pos.addMul(camera.getFlatRight(), speed);
  42. }
  43. if(control.keys.jump.isDown()) {
  44. pos.addMul(camera.getFlatUp(), speed);
  45. }
  46. if(control.keys.sneak.isDown()) {
  47. pos.addMul(camera.getFlatUp(), -speed);
  48. }
  49. const float rotation = 5.0f;
  50. if(control.keys.camLeft.isDown()) {
  51. lengthAngle += rotation;
  52. }
  53. if(control.keys.camRight.isDown()) {
  54. lengthAngle -= rotation;
  55. }
  56. if(control.keys.camUp.isDown() && widthAngle - rotation > -90.0f) {
  57. widthAngle -= rotation;
  58. }
  59. if(control.keys.camDown.isDown() && widthAngle + rotation < 90.0f) {
  60. widthAngle += rotation;
  61. }
  62. ray.store();
  63. ray.set(pos, lengthAngle, widthAngle);
  64. } else if(mode == Mode::AUTO) {
  65. movedLength += moveSpeed;
  66. }
  67. if(control.keys.test.getDownTime() == 1) {
  68. mode = Mode::PLAYER;
  69. }
  70. if(control.keys.test2.getDownTime() == 1) {
  71. mode = Mode::AUTO;
  72. }
  73. if(control.keys.test5.getDownTime() == 1) {
  74. renderSettings.ssao = !renderSettings.ssao;
  75. }
  76. }
  77. void Game::renderWorld(float lag, Renderer& renderer) const {
  78. if(mode == Mode::AUTO) {
  79. float leftLength = (movedLength - moveSpeed) + moveSpeed * lag;
  80. uint index = 0;
  81. while(leftLength >= cameraPoints[index].distance) {
  82. leftLength -= cameraPoints[index].distance;
  83. index = (index + 1) % cameraPoints.getLength();
  84. }
  85. uint prev = index;
  86. float t = leftLength / cameraPoints[index].distance;
  87. if(prev == 0) {
  88. prev = cameraPoints.getLength() - 1;
  89. } else {
  90. prev = (prev - 1) % cameraPoints.getLength();
  91. }
  92. uint currentA = (prev + 1) % cameraPoints.getLength();
  93. uint currentB = (prev + 2) % cameraPoints.getLength();
  94. uint next = (prev + 3) % cameraPoints.getLength();
  95. Vector tangentA = splineTangent(cameraPoints[prev].pos, cameraPoints[currentA].pos, cameraPoints[currentB].pos);
  96. Vector tangentB = splineTangent(cameraPoints[currentA].pos, cameraPoints[currentB].pos, cameraPoints[next].pos);
  97. Vector interpolatedPos = interpolate(cameraPoints[currentA].pos, cameraPoints[currentB].pos, tangentA, tangentB, t);
  98. pos.set(interpolatedPos);
  99. ray.set(interpolatedPos, lengthAngle, widthAngle);
  100. ray.store();
  101. }
  102. worldRenderer.render(lag, renderer);
  103. }
  104. void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) const {
  105. (void) lag;
  106. renderer.scale(1.0f).update();
  107. String s;
  108. fr.drawString(10, 10, s.append("FPS: ").append(fps.getUpdatesPerSecond()));
  109. fr.drawString(10, 19, s.clear().append("TPS: ").append(tps.getUpdatesPerSecond()));
  110. s.clear();
  111. pos.toString(s);
  112. fr.drawString(10, 28, s);
  113. for(uint i = 0; i < cameraPoints.getLength(); i++) {
  114. s.clear().append(i + 1).append(": ");
  115. cameraPoints[i].pos.toString(s);
  116. fr.drawString(10, i * 9 + 37, s);
  117. }
  118. }
  119. bool Game::isRunning() const {
  120. return true;
  121. }
  122. Vector Game::splineTangent(const Vector& prev, const Vector& current, const Vector& next) const {
  123. Vector v(current);
  124. v.sub(prev).mul(0.5f).addMul(next, 0.5f).addMul(current, -0.5f);
  125. return v;
  126. }
  127. Vector Game::interpolate(const Vector& a, const Vector& b, const Vector& tanA, const Vector& tanB, float t) const {
  128. float t2 = t * t;
  129. float t3 = t2 * t;
  130. Vector v;
  131. v.addMul(a, 2.0f * t3 - 3.0f * t2 + 1.0f).addMul(b, -2.0f * t3 + 3.0f * t2)
  132. .addMul(tanA, t3 - 2.0f * t2 + t).addMul(tanB, t3 - t2);
  133. return v;
  134. }
  135. float Game::distance(uint index, uint splits) const {
  136. uint prev = index == 0 ? cameraPoints.getLength() - 1 : index - 1;
  137. uint currentA = (prev + 1) % cameraPoints.getLength();
  138. uint currentB = (prev + 2) % cameraPoints.getLength();
  139. uint next = (prev + 3) % cameraPoints.getLength();
  140. Vector tangentA = splineTangent(cameraPoints[prev].pos, cameraPoints[currentA].pos, cameraPoints[currentB].pos);
  141. Vector tangentB = splineTangent(cameraPoints[currentA].pos, cameraPoints[currentB].pos, cameraPoints[next].pos);
  142. Vector currentPos;
  143. Vector currentNext = interpolate(cameraPoints[currentA].pos, cameraPoints[currentB].pos, tangentA, tangentB, 0.0f);
  144. float sum = 0.0f;
  145. for(uint i = 0; i <= splits; i++) {
  146. currentPos = currentNext;
  147. float t = (i + 1.0f) / (splits + 1.0f);
  148. currentNext = interpolate(cameraPoints[currentA].pos, cameraPoints[currentB].pos, tangentA, tangentB, t);
  149. sum += currentPos.sub(currentNext).length();
  150. }
  151. return sum;
  152. }