Game.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "client/Game.h"
  2. #include "client/utils/Utils.h"
  3. #include "rendering/Renderer.h"
  4. #include "common/utils/String.h"
  5. #include "common/utils/Random.h"
  6. #include "math/Quaternion.h"
  7. Game::Game(const Control& control, const Clock& fps, const Clock& tps, RenderSettings& renderSettings) :
  8. control(control), fps(fps), tps(tps), renderSettings(renderSettings), world(blockRegistry), worldRenderer(world),
  9. pointIndex(0), moveSpeed(0.25f), movedLength(0.0f), mode(Mode::AUTO) {
  10. Random r(0);
  11. float h = World::WORLD_SIZE * 0.75f;
  12. float mid = World::WORLD_SIZE * 0.5f;
  13. float randLength = World::WORLD_SIZE * 0.125f;
  14. pos.set(0, h, 0);
  15. lastPos.set(pos);
  16. for(uint i = 0; i < cameraPoints.getCapacity(); i++) {
  17. Vector offset(mid, h, mid);
  18. offset.add(Vector(r.nextFloat(randLength), r.nextFloat(randLength), r.nextFloat(randLength)));
  19. Vector v;
  20. v.setAngles(i * 360.0f / cameraPoints.getCapacity(), 0.0f).mul(mid * 0.5f).add(offset);
  21. cameraPoints.add( {v, 0.0f});
  22. }
  23. for(uint i = 0; i < cameraPoints.getLength(); i++) {
  24. cameraPoints[i].distance = distance(i, 20);
  25. }
  26. }
  27. void Game::tick() {
  28. lastPos = pos;
  29. if(mode == Mode::PLAYER) {
  30. Vector right(1.0f, 0.0f, 0.0f);
  31. Vector up(0.0f, 1.0f, 0.0f);
  32. Vector back(0.0f, 0.0f, -1.0f);
  33. const float speed = 1.0f;
  34. if(control.keys.down.isDown()) {
  35. pos.addMul(back, speed);
  36. }
  37. if(control.keys.up.isDown()) {
  38. pos.addMul(back, -speed);
  39. }
  40. if(control.keys.left.isDown()) {
  41. pos.addMul(right, -speed);
  42. }
  43. if(control.keys.right.isDown()) {
  44. pos.addMul(right, speed);
  45. }
  46. if(control.keys.jump.isDown()) {
  47. pos.addMul(up, speed);
  48. }
  49. if(control.keys.sneak.isDown()) {
  50. pos.addMul(up, -speed);
  51. }
  52. /*const float rotation = 5.0f;
  53. if(control.keys.camLeft.isDown()) {
  54. lengthAngle += rotation;
  55. }
  56. if(control.keys.camRight.isDown()) {
  57. lengthAngle -= rotation;
  58. }
  59. if(control.keys.camUp.isDown() && widthAngle - rotation > -90.0f) {
  60. widthAngle -= rotation;
  61. }
  62. if(control.keys.camDown.isDown() && widthAngle + rotation < 90.0f) {
  63. widthAngle += rotation;
  64. }*/
  65. } else if(mode == Mode::AUTO) {
  66. movedLength += moveSpeed;
  67. }
  68. if(control.keys.test.isDown()) {
  69. mode = Mode::PLAYER;
  70. }
  71. if(control.keys.test2.isDown()) {
  72. mode = Mode::AUTO;
  73. }
  74. if(control.keys.test4.getDownTime() == 1) {
  75. renderSettings.shadows = !renderSettings.shadows;
  76. }
  77. if(control.keys.test5.getDownTime() == 1) {
  78. renderSettings.ssao = !renderSettings.ssao;
  79. }
  80. }
  81. void Game::renderWorld(float lag, Renderer& renderer) const {
  82. if(mode == Mode::AUTO) {
  83. float leftLength = (movedLength - moveSpeed) + moveSpeed * lag;
  84. uint index = 0;
  85. while(leftLength >= cameraPoints[index].distance) {
  86. leftLength -= cameraPoints[index].distance;
  87. index = (index + 1) % cameraPoints.getLength();
  88. }
  89. uint prev = index;
  90. float t = leftLength / cameraPoints[index].distance;
  91. if(prev == 0) {
  92. prev = cameraPoints.getLength() - 1;
  93. } else {
  94. prev = (prev - 1) % cameraPoints.getLength();
  95. }
  96. uint currentA = (prev + 1) % cameraPoints.getLength();
  97. uint currentB = (prev + 2) % cameraPoints.getLength();
  98. uint next = (prev + 3) % cameraPoints.getLength();
  99. Vector tangentA = splineTangent(cameraPoints[prev].pos, cameraPoints[currentA].pos, cameraPoints[currentB].pos);
  100. Vector tangentB = splineTangent(cameraPoints[currentA].pos, cameraPoints[currentB].pos, cameraPoints[next].pos);
  101. Vector interpolatedPos = interpolate(cameraPoints[currentA].pos, cameraPoints[currentB].pos, tangentA, tangentB, t);
  102. renderer.update(interpolatedPos, Quaternion(Vector(1.0f, 0.0f, 0.0f), -30.0f));
  103. pos = interpolatedPos;
  104. } else if(mode == Mode::PLAYER) {
  105. Vector v(lastPos);
  106. v.addMul(pos, lag).addMul(lastPos, -lag);
  107. renderer.update(v, Quaternion(Vector(1.0f, 0.0f, 0.0f), -80.0f));
  108. }
  109. worldRenderer.render(lag, renderer);
  110. }
  111. void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) const {
  112. (void) lag;
  113. renderer.scale(2.0f).update();
  114. String s;
  115. fr.drawString(10, 10, s.append("FPS: ").append(fps.getUpdatesPerSecond())
  116. .append(" ").append("%0.8f", renderSettings.testBias).append(" ").append("%0.8f", renderSettings.testRadius));
  117. fr.drawString(10, 19, s.clear().append("TPS: ").append(tps.getUpdatesPerSecond()));
  118. s.clear();
  119. pos.toString(s);
  120. fr.drawString(10, 28, s);
  121. for(uint i = 0; i < cameraPoints.getLength(); i++) {
  122. s.clear().append(i + 1).append(": ");
  123. cameraPoints[i].pos.toString(s);
  124. fr.drawString(10, i * 9 + 37, s);
  125. }
  126. }
  127. bool Game::isRunning() const {
  128. return true;
  129. }
  130. Vector Game::splineTangent(const Vector& prev, const Vector& current, const Vector& next) const {
  131. Vector v(current);
  132. v.sub(prev).mul(0.5f).addMul(next, 0.5f).addMul(current, -0.5f);
  133. return v;
  134. }
  135. Vector Game::interpolate(const Vector& a, const Vector& b, const Vector& tanA, const Vector& tanB, float t) const {
  136. float t2 = t * t;
  137. float t3 = t2 * t;
  138. Vector v;
  139. v.addMul(a, 2.0f * t3 - 3.0f * t2 + 1.0f).addMul(b, -2.0f * t3 + 3.0f * t2)
  140. .addMul(tanA, t3 - 2.0f * t2 + t).addMul(tanB, t3 - t2);
  141. return v;
  142. }
  143. float Game::distance(uint index, uint splits) const {
  144. uint prev = index == 0 ? cameraPoints.getLength() - 1 : index - 1;
  145. uint currentA = (prev + 1) % cameraPoints.getLength();
  146. uint currentB = (prev + 2) % cameraPoints.getLength();
  147. uint next = (prev + 3) % cameraPoints.getLength();
  148. Vector tangentA = splineTangent(cameraPoints[prev].pos, cameraPoints[currentA].pos, cameraPoints[currentB].pos);
  149. Vector tangentB = splineTangent(cameraPoints[currentA].pos, cameraPoints[currentB].pos, cameraPoints[next].pos);
  150. Vector currentPos;
  151. Vector currentNext = interpolate(cameraPoints[currentA].pos, cameraPoints[currentB].pos, tangentA, tangentB, 0.0f);
  152. float sum = 0.0f;
  153. for(uint i = 0; i <= splits; i++) {
  154. currentPos = currentNext;
  155. float t = (i + 1.0f) / (splits + 1.0f);
  156. currentNext = interpolate(cameraPoints[currentA].pos, cameraPoints[currentB].pos, tangentA, tangentB, t);
  157. sum += currentPos.sub(currentNext).length();
  158. }
  159. return sum;
  160. }