Game.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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, Color4(255, 0, 0, 255));
  48. renderer.translateTo(0.0f, 0.0f).update();
  49. StringBuffer<100> s;
  50. s.append("FPS: ").append(fps.getUpdatesPerSecond());
  51. float y = 0.0f;
  52. renderer.setStringSize(2);
  53. y = renderer.drawString(0.0f, y, s);
  54. for(const Button& b : controller.list) {
  55. s.clear().append(b.getName()).append(": ").append(b.getDownTime()).append(" ").append(b.wasReleased());
  56. y = renderer.drawString(0.0f, y, s);
  57. }
  58. }
  59. bool Game::isRunning() const {
  60. return !controller.select.isDown();
  61. }