Game.cpp 2.0 KB

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