very early prototype of a spaceshooter in SFML
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

player.cpp 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <cmath>
  2. #include "player.hpp"
  3. #include "vec2util.hpp"
  4. namespace Proto4 {
  5. Player::Player() : sf::CircleShape(30, 3) {
  6. setFillColor(sf::Color(50, 150, 150));
  7. setOrigin(30, 30);
  8. }
  9. void Player::update(sf::Time timeStep, sf::Vector2f mousePos) {
  10. updateRotation(timeStep.asSeconds(), mousePos);
  11. updateSpeed(timeStep.asSeconds(), keyboard2Acceleration());
  12. }
  13. void Player::reset() {
  14. speed = sf::Vector2f{0, 0};
  15. setRotation(0.f);
  16. }
  17. sf::Vector2f Player::keyboard2Acceleration() {
  18. sf::Vector2f acceleration = sf::Vector2f(0, 0);
  19. if (
  20. sf::Keyboard::isKeyPressed(sf::Keyboard::Left) ||
  21. sf::Keyboard::isKeyPressed(sf::Keyboard::A)
  22. ) acceleration.x -= 100.f;
  23. if (
  24. sf::Keyboard::isKeyPressed(sf::Keyboard::Right) ||
  25. sf::Keyboard::isKeyPressed(sf::Keyboard::D)
  26. ) acceleration.x += 100.f;
  27. if (
  28. sf::Keyboard::isKeyPressed(sf::Keyboard::Up) ||
  29. sf::Keyboard::isKeyPressed(sf::Keyboard::W)
  30. ) acceleration.y -= 100.f;
  31. if (
  32. sf::Keyboard::isKeyPressed(sf::Keyboard::Down) ||
  33. sf::Keyboard::isKeyPressed(sf::Keyboard::S)
  34. ) acceleration.y += 100.f;
  35. return normalized(acceleration);
  36. }
  37. void Player::updateSpeed(float timeStep, sf::Vector2f accel) {
  38. accel *= acceleration * timeStep;
  39. float decel = 1 - deceleration * timeStep;
  40. // x / y accelerate & decelerate separately
  41. // to allow for exponential deceleration
  42. if (accel.y == 0.f) speed.y *= decel;
  43. else speed.y += accel.y;
  44. if (accel.x == 0.f) speed.x *= decel;
  45. else speed.x += accel.x;
  46. // normalize with speed limit * timeStep
  47. float currentSpeed = magnitude(speed);
  48. if (currentSpeed > maxSpeed)
  49. speed *= maxSpeed / currentSpeed;
  50. move(speed * timeStep);
  51. }
  52. void Player::updateRotation(float timeStep, sf::Vector2f mousePos) {
  53. sf::Vector2f playerPos = getPosition();
  54. if (playerPos == mousePos) return;
  55. //calc mouse<>player angle
  56. float targetRotation = -180 - atan2(mousePos.x - playerPos.x, mousePos.y - playerPos.y) * 180 / PI;
  57. float rot = getRotation();
  58. float rotationDiff = std::remainder(targetRotation - rot + 180, 360);
  59. //limit rotationspeed
  60. if (rotationDiff > maxRotationSpeed * timeStep)
  61. rot -= maxRotationSpeed * timeStep;
  62. else if (rotationDiff < -maxRotationSpeed * timeStep)
  63. rot += maxRotationSpeed * timeStep;
  64. else
  65. rot = targetRotation;
  66. //bring the value back down into the interval [0, 360]
  67. if (rot < 0) rot += 360;
  68. else if (rot > 360) rot -= 360;
  69. setRotation(rot);
  70. }
  71. }