|
|
@@ -27,33 +27,30 @@ namespace Proto4 { |
|
|
|
move(timeStep.asSeconds(), keyboard2Acceleration()); |
|
|
|
} |
|
|
|
|
|
|
|
void Player::reset() { |
|
|
|
speed = sf::Vector2f{0, 0}; |
|
|
|
triangle.setRotation(0); |
|
|
|
} |
|
|
|
|
|
|
|
sf::Vector2f Player::keyboard2Acceleration() { |
|
|
|
sf::Vector2f acceleration = sf::Vector2f(0, 0); |
|
|
|
|
|
|
|
if ( |
|
|
|
sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || |
|
|
|
sf::Keyboard::isKeyPressed(sf::Keyboard::A) |
|
|
|
) { |
|
|
|
acceleration.x -= 100.f; |
|
|
|
} |
|
|
|
) acceleration.x -= 100.f; |
|
|
|
if ( |
|
|
|
sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || |
|
|
|
sf::Keyboard::isKeyPressed(sf::Keyboard::D) |
|
|
|
) { |
|
|
|
acceleration.x += 100.f; |
|
|
|
} |
|
|
|
) acceleration.x += 100.f; |
|
|
|
if ( |
|
|
|
sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || |
|
|
|
sf::Keyboard::isKeyPressed(sf::Keyboard::W) |
|
|
|
) { |
|
|
|
acceleration.y -= 100.f; |
|
|
|
} |
|
|
|
) acceleration.y -= 100.f; |
|
|
|
if ( |
|
|
|
sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || |
|
|
|
sf::Keyboard::isKeyPressed(sf::Keyboard::S) |
|
|
|
) { |
|
|
|
acceleration.y += 100.f; |
|
|
|
} |
|
|
|
) acceleration.y += 100.f; |
|
|
|
|
|
|
|
return normalized(acceleration); |
|
|
|
} |
|
|
@@ -64,23 +61,16 @@ namespace Proto4 { |
|
|
|
|
|
|
|
// x / y accelerate & decelerate separately |
|
|
|
// to allow for exponential deceleration |
|
|
|
if (accel.y == 0.f) { |
|
|
|
speed.y *= decel; |
|
|
|
} else { |
|
|
|
speed.y += accel.y; |
|
|
|
} |
|
|
|
|
|
|
|
if (accel.x == 0.f) { |
|
|
|
speed.x *= decel; |
|
|
|
} else { |
|
|
|
speed.x += accel.x; |
|
|
|
} |
|
|
|
if (accel.y == 0.f) speed.y *= decel; |
|
|
|
else speed.y += accel.y; |
|
|
|
|
|
|
|
if (accel.x == 0.f) speed.x *= decel; |
|
|
|
else speed.x += accel.x; |
|
|
|
|
|
|
|
// normalize with speed limit * timeStep |
|
|
|
float currentSpeed = magnitude(speed); |
|
|
|
if (currentSpeed > maxSpeed) { |
|
|
|
if (currentSpeed > maxSpeed) |
|
|
|
speed *= maxSpeed / currentSpeed; |
|
|
|
} |
|
|
|
|
|
|
|
triangle.move(speed * timeStep); |
|
|
|
} |
|
|
@@ -93,24 +83,18 @@ namespace Proto4 { |
|
|
|
float targetRotation = -180 - atan2(mousePos.x - playerPos.x, mousePos.y - playerPos.y) * 180 / PI; |
|
|
|
float rot = triangle.getRotation(); |
|
|
|
float rotationDiff = std::remainder(targetRotation - rot + 180, 360); |
|
|
|
|
|
|
|
//limit rotationspeed |
|
|
|
if (rotationDiff > maxRotationSpeed * timeStep) { |
|
|
|
if (rotationDiff > maxRotationSpeed * timeStep) |
|
|
|
rot -= maxRotationSpeed * timeStep; |
|
|
|
} |
|
|
|
else if (rotationDiff < -maxRotationSpeed * timeStep) { |
|
|
|
else if (rotationDiff < -maxRotationSpeed * timeStep) |
|
|
|
rot += maxRotationSpeed * timeStep; |
|
|
|
} |
|
|
|
else { |
|
|
|
else |
|
|
|
rot = targetRotation; |
|
|
|
} |
|
|
|
|
|
|
|
//bring the value back down into the interval [0, 360] |
|
|
|
if (rot < 0) { |
|
|
|
rot += 360; |
|
|
|
} |
|
|
|
else if (rot > 360) { |
|
|
|
rot -= 360; |
|
|
|
} |
|
|
|
if (rot < 0) rot += 360; |
|
|
|
else if (rot > 360) rot -= 360; |
|
|
|
|
|
|
|
triangle.setRotation(rot); |
|
|
|
} |