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 {
47 47
         return AppState::Game;
48 48
     }
49 49
 
50
-    void Game::draw(sf::RenderTarget &window, sf::RenderStates states) const {
51
-        window.setView(mainView);
52
-        window.draw(level);
53
-        window.draw(player);
50
+    void Game::draw(sf::RenderTarget &target, sf::RenderStates states) const {
51
+        states.transform *= getTransform();
52
+        target.setView(mainView);
53
+        target.draw(level, states);
54
+        target.draw(player, states);
54 55
     }
55 56
 
56 57
     bool Game::reset() {

+ 6
- 5
mainmenu.cpp View File

@@ -48,11 +48,12 @@ namespace Proto4 {
48 48
         return AppState::MainMenu;
49 49
     }
50 50
 
51
-    void MainMenu::draw(sf::RenderTarget &window,
51
+    void MainMenu::draw(sf::RenderTarget &target,
52 52
                         sf::RenderStates states) const {
53
-        window.setView(view);
54
-        window.draw(mainLine);
55
-        window.draw(entry1);
56
-        window.draw(entry2);
53
+        states.transform *= getTransform();
54
+        target.setView(view);
55
+        target.draw(mainLine, states);
56
+        target.draw(entry1, states);
57
+        target.draw(entry2, states);
57 58
     }
58 59
 }

+ 12
- 25
player.cpp View File

@@ -4,32 +4,19 @@
4 4
 #include "vec2util.hpp"
5 5
 
6 6
 namespace Proto4 {
7
-    Player::Player() {
8
-        triangle = sf::CircleShape(50, 3);
9
-        triangle.setFillColor(sf::Color(50, 150, 150));
10
-        triangle.setOrigin(50, 50);
11
-    }
12
-
13
-    void Player::draw(sf::RenderTarget &window, sf::RenderStates states) const {
14
-        window.draw(triangle);
15
-    }
16
-
17
-    void Player::setPosition(sf::Vector2f pos) {
18
-        triangle.setPosition(pos);
19
-    }
20
-
21
-    sf::Vector2f Player::getPosition() {
22
-        return triangle.getPosition();
7
+    Player::Player() : sf::CircleShape(30, 3) {
8
+        setFillColor(sf::Color(50, 150, 150));
9
+        setOrigin(30, 30);
23 10
     }
24 11
 
25 12
     void Player::update(sf::Time timeStep, sf::Vector2f mousePos) {
26
-        setRotation(timeStep.asSeconds(), mousePos);
27
-        move(timeStep.asSeconds(), keyboard2Acceleration());
13
+        updateRotation(timeStep.asSeconds(), mousePos);
14
+        updateSpeed(timeStep.asSeconds(), keyboard2Acceleration());
28 15
     }
29 16
 
30 17
     void Player::reset() {
31 18
         speed = sf::Vector2f{0, 0};
32
-        triangle.setRotation(0);
19
+        setRotation(0.f);
33 20
     }
34 21
 
35 22
     sf::Vector2f Player::keyboard2Acceleration() {
@@ -55,7 +42,7 @@ namespace Proto4 {
55 42
         return normalized(acceleration);
56 43
     }
57 44
 
58
-    void Player::move(float timeStep, sf::Vector2f accel) {
45
+    void Player::updateSpeed(float timeStep, sf::Vector2f accel) {
59 46
         accel *= acceleration * timeStep;
60 47
         float decel = 1 - deceleration * timeStep;
61 48
 
@@ -72,16 +59,16 @@ namespace Proto4 {
72 59
         if (currentSpeed > maxSpeed)
73 60
             speed *= maxSpeed / currentSpeed;
74 61
 
75
-        triangle.move(speed * timeStep);
62
+        move(speed * timeStep);
76 63
     }
77 64
 
78
-    void Player::setRotation(float timeStep, sf::Vector2f mousePos) {
79
-        sf::Vector2f playerPos = triangle.getPosition();
65
+    void Player::updateRotation(float timeStep, sf::Vector2f mousePos) {
66
+        sf::Vector2f playerPos = getPosition();
80 67
         if (playerPos == mousePos) return;
81 68
 
82 69
         //calc mouse<>player angle
83 70
         float targetRotation = -180 - atan2(mousePos.x - playerPos.x, mousePos.y - playerPos.y) * 180 / PI;
84
-        float rot = triangle.getRotation();
71
+        float rot = getRotation();
85 72
         float rotationDiff = std::remainder(targetRotation - rot + 180, 360);
86 73
 
87 74
         //limit rotationspeed
@@ -96,6 +83,6 @@ namespace Proto4 {
96 83
         if (rot < 0) rot += 360;
97 84
         else if (rot > 360) rot -= 360;
98 85
 
99
-        triangle.setRotation(rot);
86
+        setRotation(rot);
100 87
     }
101 88
 }

+ 3
- 7
player.hpp View File

@@ -5,8 +5,7 @@
5 5
 #include <SFML/Window.hpp>
6 6
 
7 7
 namespace Proto4 {
8
-    class Player : public sf::Drawable, public sf::Transformable {
9
-        sf::CircleShape triangle;
8
+    class Player : public sf::CircleShape {
10 9
         sf::Vector2f speed {0.f, 0.f};
11 10
 
12 11
         const float acceleration = 2500;
@@ -15,15 +14,12 @@ namespace Proto4 {
15 14
         const float maxRotationSpeed = 800;
16 15
 
17 16
         sf::Vector2f keyboard2Acceleration();
18
-        void move(float timeStep, sf::Vector2f acceleration);
19
-        void setRotation(float timeStep, sf::Vector2f mousePos);
17
+        void updateSpeed(float timeStep, sf::Vector2f acceleration);
18
+        void updateRotation(float timeStep, sf::Vector2f mousePos);
20 19
 
21 20
     public:
22 21
         Player();
23
-        virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
24 22
         void update(sf::Time timeStep, sf::Vector2f mousePosition);
25 23
         void reset();
26
-        virtual void setPosition(sf::Vector2f pos);
27
-        virtual sf::Vector2f getPosition();
28 24
     };
29 25
 }

Loading…
Cancel
Save