Browse Source

Player as direct subclass from CircleShape

also apply transforms of custom Transformables when drawing
master
noerw 1 year ago
parent
commit
28e2667d41
4 changed files with 26 additions and 41 deletions
  1. 5
    4
      game.cpp
  2. 6
    5
      mainmenu.cpp
  3. 12
    25
      player.cpp
  4. 3
    7
      player.hpp

+ 5
- 4
game.cpp View File

@@ -47,10 +47,11 @@ namespace Proto4 {
return AppState::Game;
}

void Game::draw(sf::RenderTarget &window, sf::RenderStates states) const {
window.setView(mainView);
window.draw(level);
window.draw(player);
void Game::draw(sf::RenderTarget &target, sf::RenderStates states) const {
states.transform *= getTransform();
target.setView(mainView);
target.draw(level, states);
target.draw(player, states);
}

bool Game::reset() {

+ 6
- 5
mainmenu.cpp View File

@@ -48,11 +48,12 @@ namespace Proto4 {
return AppState::MainMenu;
}

void MainMenu::draw(sf::RenderTarget &window,
void MainMenu::draw(sf::RenderTarget &target,
sf::RenderStates states) const {
window.setView(view);
window.draw(mainLine);
window.draw(entry1);
window.draw(entry2);
states.transform *= getTransform();
target.setView(view);
target.draw(mainLine, states);
target.draw(entry1, states);
target.draw(entry2, states);
}
}

+ 12
- 25
player.cpp View File

@@ -4,32 +4,19 @@
#include "vec2util.hpp"

namespace Proto4 {
Player::Player() {
triangle = sf::CircleShape(50, 3);
triangle.setFillColor(sf::Color(50, 150, 150));
triangle.setOrigin(50, 50);
}

void Player::draw(sf::RenderTarget &window, sf::RenderStates states) const {
window.draw(triangle);
}

void Player::setPosition(sf::Vector2f pos) {
triangle.setPosition(pos);
}

sf::Vector2f Player::getPosition() {
return triangle.getPosition();
Player::Player() : sf::CircleShape(30, 3) {
setFillColor(sf::Color(50, 150, 150));
setOrigin(30, 30);
}

void Player::update(sf::Time timeStep, sf::Vector2f mousePos) {
setRotation(timeStep.asSeconds(), mousePos);
move(timeStep.asSeconds(), keyboard2Acceleration());
updateRotation(timeStep.asSeconds(), mousePos);
updateSpeed(timeStep.asSeconds(), keyboard2Acceleration());
}

void Player::reset() {
speed = sf::Vector2f{0, 0};
triangle.setRotation(0);
setRotation(0.f);
}

sf::Vector2f Player::keyboard2Acceleration() {
@@ -55,7 +42,7 @@ namespace Proto4 {
return normalized(acceleration);
}

void Player::move(float timeStep, sf::Vector2f accel) {
void Player::updateSpeed(float timeStep, sf::Vector2f accel) {
accel *= acceleration * timeStep;
float decel = 1 - deceleration * timeStep;

@@ -72,16 +59,16 @@ namespace Proto4 {
if (currentSpeed > maxSpeed)
speed *= maxSpeed / currentSpeed;

triangle.move(speed * timeStep);
move(speed * timeStep);
}

void Player::setRotation(float timeStep, sf::Vector2f mousePos) {
sf::Vector2f playerPos = triangle.getPosition();
void Player::updateRotation(float timeStep, sf::Vector2f mousePos) {
sf::Vector2f playerPos = getPosition();
if (playerPos == mousePos) return;

//calc mouse<>player angle
float targetRotation = -180 - atan2(mousePos.x - playerPos.x, mousePos.y - playerPos.y) * 180 / PI;
float rot = triangle.getRotation();
float rot = getRotation();
float rotationDiff = std::remainder(targetRotation - rot + 180, 360);

//limit rotationspeed
@@ -96,6 +83,6 @@ namespace Proto4 {
if (rot < 0) rot += 360;
else if (rot > 360) rot -= 360;

triangle.setRotation(rot);
setRotation(rot);
}
}

+ 3
- 7
player.hpp View File

@@ -5,8 +5,7 @@
#include <SFML/Window.hpp>

namespace Proto4 {
class Player : public sf::Drawable, public sf::Transformable {
sf::CircleShape triangle;
class Player : public sf::CircleShape {
sf::Vector2f speed {0.f, 0.f};

const float acceleration = 2500;
@@ -15,15 +14,12 @@ namespace Proto4 {
const float maxRotationSpeed = 800;

sf::Vector2f keyboard2Acceleration();
void move(float timeStep, sf::Vector2f acceleration);
void setRotation(float timeStep, sf::Vector2f mousePos);
void updateSpeed(float timeStep, sf::Vector2f acceleration);
void updateRotation(float timeStep, sf::Vector2f mousePos);

public:
Player();
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