Game.cpp 8.0 KB

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