Game.cpp 6.3 KB

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