Game.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "client/Game.h"
  2. #include "common/network/Packets.h"
  3. #include "gaming-core/utils/Utils.h"
  4. Game::Game(TextInput& textInput, const Controller& controller, const Clock& fps,
  5. const Clock& tps, RenderSettings& settings, const Size& size,
  6. Client& client)
  7. : textInput(textInput), controller(controller), fps(fps), tps(tps),
  8. renderSettings(settings), size(size), client(client),
  9. world(blockRegistry), worldRenderer(world), connected(false) {
  10. pos = Vector3(16.0f, 30.0f, -10.0f);
  11. rotation = Quaternion(Vector3(1.0f, 0.0f, 0.0f), 30) * rotation;
  12. rotation = Quaternion(Vector3(0.0f, 1.0f, 0.0f), 30) * rotation;
  13. textInput.setActive(true);
  14. }
  15. void Game::tick() {
  16. if(!connected && controller.down.wasReleased()) {
  17. if(client.connect("127.0.0.1", 11196, 3000)) {
  18. std::cout << client.getError() << '\n';
  19. } else {
  20. std::cout << "connected\n";
  21. }
  22. } else {
  23. client.consumeEvents(*this);
  24. }
  25. lastRotation = rotation;
  26. lastPos = pos;
  27. Vector3 right = rotation * Vector3(1.0f, 0.0f, 0.0f);
  28. Vector3 up = rotation * Vector3(0.0f, 1.0f, 0.0f);
  29. Vector3 back = rotation * Vector3(0.0f, 0.0f, -1.0f);
  30. const float speed = 2.0f;
  31. if(controller.down.isDown()) {
  32. pos += back * speed;
  33. }
  34. if(controller.up.isDown()) {
  35. pos -= back * speed;
  36. }
  37. if(controller.left.isDown()) {
  38. pos -= right * speed;
  39. }
  40. if(controller.right.isDown()) {
  41. pos += right * speed;
  42. }
  43. if(controller.jump.isDown()) {
  44. pos += up * speed;
  45. }
  46. if(controller.sneak.isDown()) {
  47. pos -= up * speed;
  48. }
  49. const float rotationSpeed = 5.0f;
  50. if(controller.camLeft.isDown()) {
  51. rotation = Quaternion(up, -rotationSpeed) * rotation;
  52. }
  53. if(controller.camRight.isDown()) {
  54. rotation = Quaternion(up, rotationSpeed) * rotation;
  55. }
  56. if(controller.camUp.isDown()) {
  57. rotation = Quaternion(right, -rotationSpeed) * rotation;
  58. }
  59. if(controller.camDown.isDown()) {
  60. rotation = Quaternion(right, rotationSpeed) * rotation;
  61. }
  62. }
  63. void Game::renderWorld(float lag, Renderer& renderer) {
  64. renderer.update(Utils::interpolate(lastPos, pos, lag),
  65. lastRotation.lerp(lag, rotation));
  66. worldRenderer.render(lag, renderer);
  67. }
  68. void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) {
  69. (void)lag;
  70. renderer.scale(2.0f).update();
  71. StringBuffer<100> s;
  72. s.append("FPS: &074")
  73. .append(fps.getUpdatesPerSecond())
  74. .append(" &999TPS: &722")
  75. .append(tps.getUpdatesPerSecond());
  76. fr.drawString(10, 10, s);
  77. s.clear();
  78. textInput.getInput(s);
  79. fr.drawString(10, 30, s);
  80. }
  81. bool Game::isRunning() const {
  82. return true;
  83. }
  84. void Game::onConnect() {
  85. }
  86. void Game::onDisconnect() {
  87. }
  88. void Game::onPacket(InPacket& in) {
  89. uint16 id;
  90. if(in.readU16(id)) {
  91. return;
  92. }
  93. switch(id) {
  94. case ServerPacket::CHAT:
  95. StringBuffer<256> s;
  96. in.readString(s);
  97. std::cout << s << '\n';
  98. break;
  99. }
  100. }