Game.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. }
  16. void Game::tick() {
  17. lastX = x;
  18. lastY = y;
  19. if(controller.left.isDown()) {
  20. x -= 7;
  21. }
  22. if(controller.right.isDown()) {
  23. x += 7;
  24. }
  25. if(controller.a.isDown() && onGround) {
  26. motionY = -15.0f;
  27. onGround = false;
  28. }
  29. motionY += 0.5f;
  30. y += motionY;
  31. if(y > height - playerHeight) {
  32. y = height - playerHeight;
  33. motionY = 0.0f;
  34. onGround = true;
  35. }
  36. while(x > width) {
  37. x -= width + playerWidth;
  38. lastX -= width + playerWidth;
  39. }
  40. while(x < -playerWidth) {
  41. x += width + playerWidth;
  42. lastX += width + playerWidth;
  43. }
  44. }
  45. void Game::render(float lag, Renderer& renderer) const {
  46. renderer.translateTo(0.0f, 0.0f).update();
  47. renderer.drawRectangle(lastX + (x - lastX) * lag, lastY + (y - lastY) * lag, playerWidth, playerHeight,
  48. Color4(255, 0, 0, 255));
  49. renderer.translateTo(0.0f, 0.0f).update();
  50. StringBuffer<100> s;
  51. s.append("FPS: ").append(fps.getUpdatesPerSecond());
  52. float y = 0.0f;
  53. renderer.setStringSize(2);
  54. y = renderer.drawString(0.0f, y, s);
  55. for(const Button& b : controller.list) {
  56. s.clear().append(b.getName()).append(": ").append(b.getDownTime()).append(" ").append(b.wasReleased());
  57. y = renderer.drawString(0.0f, y, s);
  58. }
  59. }
  60. bool Game::isRunning() const {
  61. return !controller.select.isDown();
  62. }