Browse Source

add Player::reset(), fix build

master
noerw 1 year ago
parent
commit
cb7b113351
3 changed files with 25 additions and 41 deletions
  1. 1
    1
      game.cpp
  2. 21
    37
      player.cpp
  3. 3
    3
      player.hpp

+ 1
- 1
game.cpp View File

@@ -52,7 +52,7 @@ namespace Proto4 {
}

bool Game::reset() {
player = Player();
player.reset();
state = GameState::Uninitialized;
return true;
}

+ 21
- 37
player.cpp View File

@@ -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);
}

+ 3
- 3
player.hpp View File

@@ -11,7 +11,7 @@ namespace Proto4 {

const float acceleration = 2500;
const float deceleration = 3;
const float maxSpeed = 650;
const float maxSpeed = 850;
const float maxRotationSpeed = 800;

sf::Vector2f keyboard2Acceleration();
@@ -20,9 +20,9 @@ namespace Proto4 {

public:
Player();
virtual void draw(sf::RenderTarget &window,
sf::RenderStates states) const;
virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
void update(sf::Time timeStep, sf::Vector2f mousePosition);
void reset();
virtual void setPosition(sf::Vector2f pos);
virtual sf::Vector2f getPosition();
};

Loading…
Cancel
Save