Game.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. std::cout << controller.a.getDownTime() << "\n";
  17. if(controller.a.isDown() && player.onGround) {
  18. player.jumpTicks = 10;
  19. player.onGround = false;
  20. }
  21. if(controller.right.isDown()) {
  22. player.addForce(Vector2(2.5f, 0.0f));
  23. }
  24. if(controller.left.isDown()) {
  25. player.addForce(Vector2(-2.5f, 0.0f));
  26. }
  27. if(player.jumpTicks > 0) {
  28. player.jumpTicks--;
  29. player.addForce(Vector2(0.0f, 10.0f));
  30. }
  31. player.jumpTicks *= controller.a.isDown();
  32. // pseudo drag
  33. player.addForce(Vector2(-player.velocity[0] * 0.5f, 0.0f));
  34. } else {
  35. player.velocity[0] = 0.0f;
  36. if(controller.a.isDown() && player.onGround) {
  37. player.velocity[1] = 15.0f;
  38. player.onGround = false;
  39. }
  40. if(controller.right.isDown()) {
  41. player.velocity[0] = 5.0f;
  42. }
  43. if(controller.left.isDown()) {
  44. player.velocity[0] = -5.0f;
  45. }
  46. }
  47. player.tick();
  48. // collision detection
  49. // floor crush
  50. if(player.position[1] < 0.0f) {
  51. player.position[1] = 0.0f;
  52. player.velocity[1] = 0.0f;
  53. player.onGround = true;
  54. }
  55. // right wall
  56. if(player.position[0] + player.size[0] > size.width) {
  57. player.position[0] = size.width - player.size[0];
  58. // player.velocity[0] = 0.0f;
  59. }
  60. // left wall
  61. if(player.position[0] < 0.0f) {
  62. player.position[0] = 0.0f;
  63. // player.velocity[0] = 0.0f;
  64. }
  65. }
  66. void Game::render(float lag, Renderer& r) const {
  67. r.translateTo(0.0f, 0.0f)
  68. .scale(1.0f, -1.0f)
  69. .translateY(size.height)
  70. .update();
  71. Vector2 pos = Utils::interpolate(player.lastPosition, player.position, lag);
  72. r.drawRectangle(pos, player.size, Color4(0xFF, 0x00, 0x00, 0xFF));
  73. r.translateTo(0.0f, 0.0f).update();
  74. r.setStringSize(2);
  75. StringBuffer<100> s("FPS: ");
  76. s.append(fps.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. }