Game.cpp 2.2 KB

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