Game.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "client/Game.h"
  2. #include "client/GameClient.h"
  3. #include "client/gui/StartGUI.h"
  4. #include "client/rendering/Engine.h"
  5. #include "common/network/Packets.h"
  6. #include "common/network/toserver/PlayerUpdatePacket.h"
  7. #include "rendering/renderer/WorldRenderer.h"
  8. #include "utils/Logger.h"
  9. #include "utils/Utils.h"
  10. World Game::world;
  11. static WorldRenderer worldRenderer{Game::world};
  12. Controller Game::controller;
  13. Entity Game::player;
  14. typedef void (*State)();
  15. static State tickState;
  16. static State renderState;
  17. static BaseGUI baseGUI;
  18. static StartGUI startGUI;
  19. static void tickConnectedState() {
  20. Game::player.skip = false;
  21. Game::world.tick();
  22. GameClient::consumeEvents();
  23. Quaternion q = Game::player.getRotation();
  24. Vector3 up(0.0f, 1.0f, 0.0f);
  25. Vector3 back = q * Vector3(0.0f, 0.0f, -1.0f);
  26. back[1] = 0.0f;
  27. back.normalize();
  28. Vector3 right = back.cross(up);
  29. constexpr float rotationSpeed = 4.0f;
  30. Vector3 force;
  31. if(Game::controller.down.isDown()) {
  32. force += back;
  33. }
  34. if(Game::controller.up.isDown()) {
  35. force -= back;
  36. }
  37. if(Game::controller.left.isDown()) {
  38. force -= right;
  39. }
  40. if(Game::controller.right.isDown()) {
  41. force += right;
  42. }
  43. if(force.squareLength() > 0.0f) {
  44. force.normalize();
  45. }
  46. Game::player.addForce(force * Game::player.speed);
  47. if(Game::controller.jump.isDown() && Game::player.isOnGround()) {
  48. Game::player.jump();
  49. }
  50. if(Game::controller.camLeft.isDown()) {
  51. Game::player.addLengthAngle(-rotationSpeed);
  52. }
  53. if(Game::controller.camRight.isDown()) {
  54. Game::player.addLengthAngle(rotationSpeed);
  55. }
  56. if(Game::controller.camUp.isDown()) {
  57. Game::player.addWidthAngle(-rotationSpeed * 0.5f);
  58. }
  59. if(Game::controller.camDown.isDown()) {
  60. Game::player.addWidthAngle(rotationSpeed * 0.5f);
  61. }
  62. PlayerUpdatePacket p(Game::player);
  63. OutPacket out = OutPacket::reliable(PlayerUpdatePacket::getSize());
  64. p.write(out);
  65. GameClient::send(out);
  66. }
  67. static void renderConnectedState() {
  68. }
  69. static void tickConnectState() {
  70. startGUI.tick();
  71. StartGUI::Address a;
  72. if(startGUI.getAddress(a)) {
  73. Error error = GameClient::connect(a, 11196, 3000);
  74. if(error.has()) {
  75. LOG_INFO(error.message);
  76. } else {
  77. LOG_INFO("connected");
  78. tickState = tickConnectedState;
  79. renderState = renderConnectedState;
  80. }
  81. }
  82. }
  83. static void renderConnectState() {
  84. startGUI.render();
  85. }
  86. bool Game::init() {
  87. Block::init();
  88. if(worldRenderer.init()) {
  89. return true;
  90. }
  91. controller.init();
  92. tickState = tickConnectState;
  93. renderState = renderConnectState;
  94. Game::player.setPosition(Vector3(0.0f, 2.0f, 0.0f));
  95. world.addEntity(&player);
  96. return false;
  97. }
  98. void Game::tick() {
  99. tickState();
  100. }
  101. void Game::renderWorld() {
  102. Engine::matrix.update(player.getRenderPosition(Engine::lag),
  103. player.getRenderRotation(Engine::lag));
  104. worldRenderer.render();
  105. }
  106. void Game::renderOverlay() {
  107. renderState();
  108. Engine::matrix.identity().scale(2.0f).update();
  109. StringBuffer<100> s;
  110. s.append("FPS: &074")
  111. .append(Engine::getFrameClock().getUpdatesPerSecond())
  112. .append(" &999TPS: &722")
  113. .append(Engine::getTickClock().getUpdatesPerSecond());
  114. Engine::renderer.renderString(Vector2(10.0f, 10.0f), s);
  115. s.clear().append("a = ").append(player.acceleration);
  116. Engine::renderer.renderString(Vector2(10.0f, 20.0f), s);
  117. s.clear().append("v = ").append(player.velocity);
  118. Engine::renderer.renderString(Vector2(10.0f, 30.0f), s);
  119. }
  120. void Game::onEntityUpdate(EntityUpdatePacket& p) {
  121. LOG_DEBUG("set");
  122. player.setPosition(p.position);
  123. player.velocity = p.velocity;
  124. }