Game.cpp 3.6 KB

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