Game.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "Game.h"
  2. #include "rendering/Renderer.h"
  3. #include "rendering/wrapper/GLFWWrapper.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(const Controller& c, const Clock& fps, const Clock& tps) : controller(c), fps(fps), tps(tps) {
  16. }
  17. void Game::tick() {
  18. lastX = x;
  19. lastY = y;
  20. if(controller.isDown(Button::LEFT)) {
  21. x -= 7;
  22. }
  23. if(controller.isDown(Button::RIGHT)) {
  24. x += 7;
  25. }
  26. if(controller.isDown(Button::A) && onGround) {
  27. motionY = -15.0f;
  28. onGround = false;
  29. }
  30. motionY += 0.5f;
  31. y += motionY;
  32. if(y > height - playerHeight) {
  33. y = height - playerHeight;
  34. motionY = 0.0f;
  35. onGround = true;
  36. }
  37. while(x > width) {
  38. x -= width + playerWidth;
  39. lastX -= width + playerWidth;
  40. }
  41. while(x < -playerWidth) {
  42. x += width + playerWidth;
  43. lastX += width + playerWidth;
  44. }
  45. }
  46. void Game::render(float lag, Renderer& renderer) const {
  47. renderer.translateTo(0.0f, 0.0f).scale(4).update();
  48. for(uint i = 0; i < controller.getButtonAmount(); i++) {
  49. String s(controller.getName(i));
  50. s.append(": ").append(controller.wasReleased(i)).append(" ").append(controller.getDownTime(i));
  51. renderer.drawString(0, 10 * i, s);
  52. }
  53. renderer.translateTo(0.0f, 0.0f).update();
  54. renderer.drawRectangle(lastX + (x - lastX) * lag, lastY + (y - lastY) * lag, playerWidth, playerHeight, 0x0000FFFF);
  55. }
  56. bool Game::isRunning() const {
  57. return !controller.isDown(Button::SELECT);
  58. }