Game.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "Game.h"
  2. #include "rendering/Renderer.h"
  3. #include "utils/StringBuffer.h"
  4. #include "utils/Utils.h"
  5. Game::Game(Controller& c, const Clock& fps, const Clock& tps, const Size& size)
  6. : controller(c), fps(fps), tps(tps), size(size), physicsToggle(true),
  7. player(Vector2(50.0f, 50.0f), 5.0f) {
  8. }
  9. void Game::tick() {
  10. if(controller.start.wasReleased()) {
  11. physicsToggle = !physicsToggle;
  12. }
  13. player.preTick();
  14. if(physicsToggle) {
  15. // forces from the player
  16. if(controller.a.isDown() && player.onGround) {
  17. player.jumpTicks = 10;
  18. player.onGround = false;
  19. }
  20. if(controller.right.isDown()) {
  21. player.addForce(Vector2(2.5f, 0.0f));
  22. }
  23. if(controller.left.isDown()) {
  24. player.addForce(Vector2(-2.5f, 0.0f));
  25. }
  26. if(player.jumpTicks > 0) {
  27. player.jumpTicks--;
  28. player.addForce(Vector2(0.0f, 10.0f));
  29. }
  30. player.jumpTicks *= controller.a.isDown();
  31. // pseudo drag
  32. player.addForce(Vector2(-player.velocity[0] * 0.5f, 0.0f));
  33. } else {
  34. player.velocity[0] = 0.0f;
  35. if(controller.a.isDown() && player.onGround) {
  36. player.velocity[1] = 15.0f;
  37. player.onGround = false;
  38. }
  39. if(controller.right.isDown()) {
  40. player.velocity[0] = 5.0f;
  41. }
  42. if(controller.left.isDown()) {
  43. player.velocity[0] = -5.0f;
  44. }
  45. }
  46. player.tick();
  47. // collision detection
  48. // floor crush
  49. if(player.position[1] < 0.0f) {
  50. player.position[1] = 0.0f;
  51. player.velocity[1] = 0.0f;
  52. player.onGround = true;
  53. }
  54. // right wall
  55. if(player.position[0] + player.size[0] > size.width) {
  56. player.position[0] = size.width - player.size[0];
  57. // player.velocity[0] = 0.0f;
  58. }
  59. // left wall
  60. if(player.position[0] < 0.0f) {
  61. player.position[0] = 0.0f;
  62. // player.velocity[0] = 0.0f;
  63. }
  64. }
  65. void Game::render(float lag, Renderer& r) const {
  66. r.translateTo(0.0f, 0.0f)
  67. .scale(1.0f, -1.0f)
  68. .translateY(size.height)
  69. .update();
  70. Vector2 pos = Utils::interpolate(player.lastPosition, player.position, lag);
  71. r.drawRectangle(pos, player.size, Color4(0xFF, 0x00, 0x00, 0xFF));
  72. r.translateTo(0.0f, 0.0f).update();
  73. r.setStringSize(2);
  74. StringBuffer<100> s("FPS: ");
  75. s.append(fps.getUpdatesPerSecond()).append(" ");
  76. s.append("%6.2f", tps.getUpdatesPerSecond()).append(" ");
  77. s.append(physicsToggle ? "Force Physics" : "Velocity Physics");
  78. float y = 10.0f;
  79. y = r.drawString(10.0f, y, s);
  80. s.clear().append("a = ").append(player.acceleration);
  81. y = r.drawString(10.0f, y, s);
  82. s.clear().append("v = ").append(player.velocity);
  83. y = r.drawString(10.0f, y, s);
  84. }
  85. bool Game::isRunning() const {
  86. return !controller.select.isDown();
  87. }