Game.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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, 30.0f, -10.0f);
  7. rotation = Quaternion(Vector3(1.0f, 0.0f, 0.0f), 30) * rotation;
  8. rotation = Quaternion(Vector3(0.0f, 1.0f, 0.0f), 30) * rotation;
  9. }
  10. void Game::tick() {
  11. lastRotation = rotation;
  12. lastPos = pos;
  13. Vector3 right = rotation * Vector3(1.0f, 0.0f, 0.0f);
  14. Vector3 up = rotation * Vector3(0.0f, 1.0f, 0.0f);
  15. Vector3 back = rotation * Vector3(0.0f, 0.0f, -1.0f);
  16. const float speed = 8.0f;
  17. if(controller.down.isDown()) {
  18. pos += back * speed;
  19. }
  20. if(controller.up.isDown()) {
  21. pos -= back * speed;
  22. }
  23. if(controller.left.isDown()) {
  24. pos -= right * speed;
  25. }
  26. if(controller.right.isDown()) {
  27. pos += right * speed;
  28. }
  29. if(controller.jump.isDown()) {
  30. pos += up * speed;
  31. }
  32. if(controller.sneak.isDown()) {
  33. pos -= up * speed;
  34. }
  35. const float rotationSpeed = 5.0f;
  36. if(controller.camLeft.isDown()) {
  37. rotation = Quaternion(up, -rotationSpeed) * rotation;
  38. }
  39. if(controller.camRight.isDown()) {
  40. rotation = Quaternion(up, rotationSpeed) * rotation;
  41. }
  42. if(controller.camUp.isDown()) {
  43. rotation = Quaternion(right, -rotationSpeed) * rotation;
  44. }
  45. if(controller.camDown.isDown()) {
  46. rotation = Quaternion(right, rotationSpeed) * rotation;
  47. }
  48. }
  49. void Game::renderWorld(float lag, Renderer& renderer) {
  50. renderer.update(Utils::interpolate(lastPos, pos, lag), lastRotation.lerp(lag, rotation));
  51. worldRenderer.render(lag, renderer);
  52. }
  53. void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) {
  54. (void)lag;
  55. renderer.scale(2.0f).update();
  56. StringBuffer<100> s;
  57. s.append("FPS: &074").append(fps.getUpdatesPerSecond()).append(" &999TPS: &722").append(tps.getUpdatesPerSecond());
  58. fr.drawString(10, 10, s);
  59. }
  60. bool Game::isRunning() const {
  61. return true;
  62. }