Client.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "Client.h"
  2. #include <iostream>
  3. #include "../block/Blocks.h"
  4. #include "../client/rendering/block/BlockRenderers.h"
  5. #include "rendering/entity/EntityRenderer.h"
  6. #include <cmath>
  7. #include "rendering/gui/StartMenu.h"
  8. using namespace std;
  9. Client::Client()
  10. {
  11. BlockRegistry::registerBlocks();
  12. BlockRenderers::init();
  13. keyManager.map(KEY_LEFT, GLFW_KEY_A);
  14. keyManager.map(KEY_RIGHT, GLFW_KEY_D);
  15. keyManager.map(KEY_UP, GLFW_KEY_W);
  16. keyManager.map(KEY_DOWN, GLFW_KEY_S);
  17. keyManager.map(KEY_JUMP, GLFW_KEY_SPACE);
  18. keyManager.map(KEY_SNEAK, GLFW_KEY_LEFT_SHIFT);
  19. keyManager.map(KEY_TEST, GLFW_KEY_T);
  20. mouseManager.map(MOUSE_LEFT, GLFW_MOUSE_BUTTON_1);
  21. activeGUI = new StartMenu();
  22. }
  23. Client::~Client()
  24. {
  25. if(activeGUI != nullptr)
  26. {
  27. delete activeGUI;
  28. }
  29. }
  30. void Client::tick()
  31. {
  32. tps.update();
  33. diffMouseY = 0;
  34. diffMouseX = 0;
  35. mouseManager.tick();
  36. keyManager.tick();
  37. if(activeGUI != nullptr)
  38. {
  39. GUI* newGUI = activeGUI->tick(keyManager, mouseManager);
  40. if(newGUI != activeGUI)
  41. {
  42. delete activeGUI;
  43. activeGUI = newGUI;
  44. }
  45. }
  46. }
  47. void Client::render3DTick(float lag)
  48. {
  49. fps.update();
  50. }
  51. void Client::render2DTick(float lag)
  52. {
  53. if(activeGUI != nullptr)
  54. {
  55. activeGUI->render2DTick(shader, directRenderer, lag);
  56. }
  57. Engine::setMixMode();
  58. shader.setToIdentity();
  59. shader.updateModelMatrix();
  60. string wusi;
  61. wusi = "FPS: " + to_string(fps.getUpdatesPerSecond());
  62. float y = directRenderer.drawString(10, 10, true, wusi);
  63. wusi = "TPS: " + to_string(tps.getUpdatesPerSecond());
  64. directRenderer.drawString(10, y, true, wusi);
  65. }
  66. void Client::onKeyEvent(int key, int scancode, int action, int mods)
  67. {
  68. if(action == GLFW_PRESS)
  69. {
  70. keyManager.press(key);
  71. }
  72. else if(action == GLFW_RELEASE)
  73. {
  74. keyManager.release(key);
  75. }
  76. }
  77. double Client::transformMouse(double x)
  78. {
  79. if(x >= -MOUSE_BORDER && x <= MOUSE_BORDER)
  80. {
  81. return x * x * x;
  82. }
  83. else if(x >= MOUSE_BORDER)
  84. {
  85. const double k = (3 * MOUSE_BORDER * MOUSE_BORDER);
  86. const double y = MOUSE_BORDER * MOUSE_BORDER * MOUSE_BORDER;
  87. const double d = y - k * MOUSE_BORDER;
  88. return k * x + d;
  89. }
  90. else
  91. {
  92. const double k = (3 * MOUSE_BORDER * MOUSE_BORDER);
  93. const double y = -MOUSE_BORDER * MOUSE_BORDER * MOUSE_BORDER;
  94. const double d = y - k * MOUSE_BORDER;
  95. return k * x + d;
  96. }
  97. }
  98. void Client::onMouseMove(double x, double y)
  99. {
  100. if(mouseTrapped)
  101. {
  102. diffMouseX = transformMouse(oldMouseX - x) * (1 / (MOUSE_BORDER * MOUSE_BORDER * 3));
  103. diffMouseY = transformMouse(oldMouseY - y) * (1 / (MOUSE_BORDER * MOUSE_BORDER * 3));
  104. oldMouseX = x;
  105. oldMouseY = y;
  106. }
  107. }
  108. void Client::onMouseClick(int button, int action, int mods)
  109. {
  110. if(action == GLFW_PRESS)
  111. {
  112. mouseManager.press(button);
  113. }
  114. else if(action == GLFW_RELEASE)
  115. {
  116. mouseManager.release(button);
  117. }
  118. }