Game.cpp 7.0 KB

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