Game.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "Game.h"
  2. #include "rendering/Renderer.h"
  3. #include "rendering/wrapper/GLFW.h"
  4. #include "utils/String.h"
  5. float x = 0.0f;
  6. float y = 0.0f;
  7. float lastX = 0.0f;
  8. float lastY = 0.0f;
  9. float playerWidth = 80.0f;
  10. float playerHeight = 80.0f;
  11. float width = 800.0f;
  12. float height = 480.0f;
  13. float motionY = 0.0f;
  14. bool onGround = false;
  15. Game::Game(Controller& c, const Clock& fps, const Clock& tps) : controller(c), fps(fps), tps(tps) {
  16. c.mapKey(GLFW_KEY_A, Controller::A);
  17. c.mapKey(GLFW_KEY_S, Controller::B);
  18. c.mapKey(GLFW_KEY_X, Controller::X);
  19. c.mapKey(GLFW_KEY_Z, Controller::Y);
  20. c.mapKey(GLFW_KEY_Q, Controller::L);
  21. c.mapKey(GLFW_KEY_W, Controller::R);
  22. c.mapKey(GLFW_KEY_E, Controller::START);
  23. c.mapKey(GLFW_KEY_D, Controller::SELECT);
  24. c.mapKey(GLFW_KEY_LEFT, Controller::LEFT);
  25. c.mapKey(GLFW_KEY_RIGHT, Controller::RIGHT);
  26. c.mapKey(GLFW_KEY_UP, Controller::UP);
  27. c.mapKey(GLFW_KEY_DOWN, Controller::DOWN);
  28. }
  29. void Game::tick() {
  30. lastX = x;
  31. lastY = y;
  32. if(controller.isDown(Controller::LEFT)) {
  33. x -= 7;
  34. }
  35. if(controller.isDown(Controller::RIGHT)) {
  36. x += 7;
  37. }
  38. if(controller.isDown(Controller::A) && onGround) {
  39. motionY = -15.0f;
  40. onGround = false;
  41. }
  42. motionY += 0.5f;
  43. y += motionY;
  44. if(y > height - playerHeight) {
  45. y = height - playerHeight;
  46. motionY = 0.0f;
  47. onGround = true;
  48. }
  49. while(x > width) {
  50. x -= width + playerWidth;
  51. lastX -= width + playerWidth;
  52. }
  53. while(x < -playerWidth) {
  54. x += width + playerWidth;
  55. lastX += width + playerWidth;
  56. }
  57. }
  58. void Game::render(float lag, Renderer& renderer) const {
  59. renderer.translateTo(0.0f, 0.0f).update();
  60. renderer.drawRectangle(lastX + (x - lastX) * lag, lastY + (y - lastY) * lag, playerWidth, playerHeight, 0xFF0000FF);
  61. renderer.translateTo(0.0f, 0.0f).update();
  62. String s;
  63. s.append("FPS: ").append(fps.getUpdatesPerSecond());
  64. float y = 0.0f;
  65. renderer.setStringSize(2);
  66. y = renderer.drawString(0.0f, y, s);
  67. for(int i = 0; i < controller.getButtonAmount(); i++) {
  68. s.clear().append(controller.getName(i)).append(": ").append(controller.getDownTime(i)).append(" ").append(controller.getUpTime(i));
  69. y = renderer.drawString(0.0f, y, s);
  70. }
  71. }
  72. bool Game::isRunning() const {
  73. return !controller.isDown(Controller::SELECT);
  74. }