Game.cpp 3.2 KB

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