Game.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. 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. 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. Vector Game::pointUntilDistance(float leftDistance, uint index, uint splits) const {
  170. Vector a;
  171. Vector b;
  172. Vector tanA;
  173. Vector tanB;
  174. getPointsAndTangents(index, a, b, tanA, tanB);
  175. Vector currentPos;
  176. Vector currentNext = interpolate(a, b, tanA, tanB, 0.0f);
  177. float sum = 0.0f;
  178. uint i = 0;
  179. while(leftDistance > sum) {
  180. currentPos = currentNext;
  181. float t = (i + 1.0f) / (splits + 1.0f);
  182. currentNext = interpolate(a, b, tanA, tanB, t);
  183. float l = currentPos.sub(currentNext).length();
  184. sum += l;
  185. i++;
  186. }
  187. return currentNext;
  188. }
  189. void Game::getPointsAndTangents(uint index, Vector& a, Vector& b, Vector& tanA, Vector& tanB) const {
  190. uint prev = index == 0 ? cameraPoints.getLength() - 1 : index - 1;
  191. uint currentA = (prev + 1) % cameraPoints.getLength();
  192. uint currentB = (prev + 2) % cameraPoints.getLength();
  193. uint next = (prev + 3) % cameraPoints.getLength();
  194. a.set(cameraPoints[currentA].pos);
  195. b.set(cameraPoints[currentB].pos);
  196. tanA = splineTangent(cameraPoints[prev].pos, a, b);
  197. tanB = splineTangent(a, b, cameraPoints[next].pos);
  198. }