Game.cpp 2.3 KB

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