Game.cpp 3.2 KB

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