Game.cpp 6.7 KB

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