Game.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <cmath>
  2. #include <vector>
  3. #include <fstream>
  4. #include "client/Game.h"
  5. #include "rendering/Renderer.h"
  6. #include "common/utils/String.h"
  7. #include "common/utils/Random.h"
  8. #include "gaming-core/math/Quaternion.h"
  9. #include "gaming-core/utils/Utils.h"
  10. #include "rendering/wrapper/GLWrapper.h"
  11. #include "rendering/wrapper/GLFWWrapper.h"
  12. Game::Game(const Control& control, const Clock& fps, const Clock& tps, RenderSettings& settings, const Size& size) :
  13. control(control), fps(fps), tps(tps), renderSettings(settings), size(size), world(blockRegistry), worldRenderer(world) {
  14. pos = Vector3(16.0f, 24.0f, 0.0f);
  15. }
  16. void Game::tick() {
  17. lastRotation = rotation;
  18. lastPos = pos;
  19. Vector3 right = rotation * Vector3(1.0f, 0.0f, 0.0f);
  20. Vector3 up = rotation * Vector3(0.0f, 1.0f, 0.0f);
  21. Vector3 back = rotation * Vector3(0.0f, 0.0f, -1.0f);
  22. const float speed = 1.0f;
  23. if(control.keys.down.isDown()) {
  24. pos += back * speed;
  25. }
  26. if(control.keys.up.isDown()) {
  27. pos -= back * speed;
  28. }
  29. if(control.keys.left.isDown()) {
  30. pos -= right * speed;
  31. }
  32. if(control.keys.right.isDown()) {
  33. pos += right * speed;
  34. }
  35. if(control.keys.jump.isDown()) {
  36. pos += up * speed;
  37. }
  38. if(control.keys.sneak.isDown()) {
  39. pos -= up * speed;
  40. }
  41. const float rotationSpeed = 5.0f;
  42. if(control.keys.camLeft.isDown()) {
  43. rotation = Quaternion(up, -rotationSpeed) * rotation;
  44. }
  45. if(control.keys.camRight.isDown()) {
  46. rotation = Quaternion(up, rotationSpeed) * rotation;
  47. }
  48. if(control.keys.camUp.isDown()) {
  49. rotation = Quaternion(right, -rotationSpeed) * rotation;
  50. }
  51. if(control.keys.camDown.isDown()) {
  52. rotation = Quaternion(right, rotationSpeed) * rotation;
  53. }
  54. }
  55. void Game::renderWorld(float lag, Renderer& renderer) const {
  56. renderer.update(Utils::interpolate(lastPos, pos, lag), lastRotation.lerp(lag, rotation));
  57. worldRenderer.render(lag, renderer);
  58. }
  59. void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) const {
  60. (void) lag;
  61. renderer.scale(2.0f).update();
  62. StringBuffer<100> s;
  63. s.append("FPS: ").append(fps.getUpdatesPerSecond()).append(" TPS: ").append(tps.getUpdatesPerSecond());
  64. fr.drawString(10, 10, s);
  65. }
  66. bool Game::isRunning() const {
  67. return true;
  68. }