Game.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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;
  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), -10.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. if(control.keys.camUp.isDown()) {
  78. moveSpeed += 0.0125f;
  79. }
  80. if(control.keys.camDown.isDown()) {
  81. moveSpeed -= 0.0125f;
  82. if(moveSpeed < 0.0f) {
  83. moveSpeed = 0.0f;
  84. }
  85. }
  86. }
  87. if(control.keys.test.isDown()) {
  88. mode = Mode::PLAYER;
  89. }
  90. if(control.keys.test2.isDown()) {
  91. mode = Mode::AUTO;
  92. }
  93. if(control.keys.test4.getDownTime() == 1) {
  94. renderSettings.shadows = !renderSettings.shadows;
  95. }
  96. if(control.keys.test5.getDownTime() == 1) {
  97. renderSettings.ssao = !renderSettings.ssao;
  98. }
  99. }
  100. void Game::renderWorld(float lag, Renderer& renderer) const {
  101. if(mode == Mode::AUTO) {
  102. float leftLength = (movedLength - moveSpeed) + moveSpeed * lag;
  103. uint index = 0;
  104. while(leftLength >= cameraPoints[index].distance) {
  105. leftLength -= cameraPoints[index].distance;
  106. index = (index + 1) % cameraPoints.getLength();
  107. }
  108. float t = leftLength / cameraPoints[index].distance;
  109. Vector interpolatedPos = pointUntilDistance(leftLength, index, 4000);
  110. uint a = index == 0 ? cameraPoints.getLength() - 1 : index - 1;
  111. uint b = (a + 1) % cameraPoints.getLength();
  112. uint c = (a + 2) % cameraPoints.getLength();
  113. uint d = (a + 3) % cameraPoints.getLength();
  114. renderer.update(interpolatedPos, cameraPoints[b].q.squad(t, cameraPoints[a].q, cameraPoints[c].q, cameraPoints[d].q));
  115. //renderer.update(interpolatedPos, cameraPoints[index].q.slerp(t, cameraPoints[(index + 1) % cameraPoints.getLength()].q));
  116. pos = interpolatedPos;
  117. } else if(mode == Mode::PLAYER) {
  118. Vector v(lastPos);
  119. v.addMul(pos, lag).addMul(lastPos, -lag);
  120. renderer.update(v, lastRotation.slerp(lag, rotation));
  121. }
  122. worldRenderer.render(lag, renderer);
  123. }
  124. void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) const {
  125. (void) lag;
  126. renderer.scale(2.0f).update();
  127. String s;
  128. fr.drawString(10, 10, s.append("FPS: ").append(fps.getUpdatesPerSecond())
  129. .append(" ").append("%0.8f", renderSettings.testBias).append(" ").append("%0.8f", renderSettings.testRadius));
  130. fr.drawString(10, 19, s.clear().append("TPS: ").append(tps.getUpdatesPerSecond()));
  131. s.clear();
  132. pos.toString(s);
  133. fr.drawString(10, 28, s);
  134. for(uint i = 0; i < cameraPoints.getLength(); i++) {
  135. s.clear().append(i + 1).append(": ");
  136. cameraPoints[i].pos.toString(s);
  137. fr.drawString(10, i * 9 + 37, s);
  138. }
  139. }
  140. bool Game::isRunning() const {
  141. return true;
  142. }
  143. Vector Game::splineTangent(const Vector& prev, const Vector& current, const Vector& next) const {
  144. Vector v(current);
  145. v.sub(prev).mul(0.5f).addMul(next, 0.5f).addMul(current, -0.5f);
  146. return v;
  147. }
  148. Vector Game::interpolate(const Vector& a, const Vector& b, const Vector& tanA, const Vector& tanB, float t) const {
  149. float t2 = t * t;
  150. float t3 = t2 * t;
  151. Vector v;
  152. v.addMul(a, 2.0f * t3 - 3.0f * t2 + 1.0f).addMul(b, -2.0f * t3 + 3.0f * t2)
  153. .addMul(tanA, t3 - 2.0f * t2 + t).addMul(tanB, t3 - t2);
  154. return v;
  155. }
  156. float Game::distance(uint index, uint splits) const {
  157. Vector a;
  158. Vector b;
  159. Vector tanA;
  160. Vector tanB;
  161. getPointsAndTangents(index, a, b, tanA, tanB);
  162. Vector currentPos;
  163. Vector currentNext = interpolate(a, b, tanA, tanB, 0.0f);
  164. float sum = 0.0f;
  165. for(uint i = 0; i <= splits; i++) {
  166. currentPos = currentNext;
  167. float t = (i + 1.0f) / (splits + 1.0f);
  168. currentNext = interpolate(a, b, tanA, tanB, t);
  169. float l = currentPos.sub(currentNext).length();
  170. sum += l;
  171. }
  172. return sum;
  173. }
  174. Vector Game::pointUntilDistance(float leftDistance, uint index, uint splits) const {
  175. Vector a;
  176. Vector b;
  177. Vector tanA;
  178. Vector tanB;
  179. getPointsAndTangents(index, a, b, tanA, tanB);
  180. Vector currentPos;
  181. Vector currentNext = interpolate(a, b, tanA, tanB, 0.0f);
  182. float sum = 0.0f;
  183. uint i = 0;
  184. while(leftDistance > sum) {
  185. currentPos = currentNext;
  186. float t = (i + 1.0f) / (splits + 1.0f);
  187. currentNext = interpolate(a, b, tanA, tanB, t);
  188. float l = currentPos.sub(currentNext).length();
  189. sum += l;
  190. i++;
  191. }
  192. return currentNext;
  193. }
  194. void Game::getPointsAndTangents(uint index, Vector& a, Vector& b, Vector& tanA, Vector& tanB) const {
  195. uint prev = index == 0 ? cameraPoints.getLength() - 1 : index - 1;
  196. uint currentA = (prev + 1) % cameraPoints.getLength();
  197. uint currentB = (prev + 2) % cameraPoints.getLength();
  198. uint next = (prev + 3) % cameraPoints.getLength();
  199. a.set(cameraPoints[currentA].pos);
  200. b.set(cameraPoints[currentB].pos);
  201. tanA = splineTangent(cameraPoints[prev].pos, a, b);
  202. tanB = splineTangent(a, b, cameraPoints[next].pos);
  203. }