Browse Source

make level fade away with timer

master
noerw 1 year ago
parent
commit
2b19d79509
3 changed files with 42 additions and 26 deletions
  1. 1
    0
      game.cpp
  2. 24
    18
      level.cpp
  3. 17
    8
      level.hpp

+ 1
- 0
game.cpp View File

@@ -34,6 +34,7 @@ namespace Proto4 {
34 34
             break;
35 35
 
36 36
         case GameState::Running:
37
+            level.update(timestep);
37 38
             player.update(timestep, window.mapPixelToCoords(sf::Mouse::getPosition(window), mainView));
38 39
             // mainView acts as camera. TODO: introduce lagging camera
39 40
             mainView.setCenter(player.getPosition());

+ 24
- 18
level.cpp View File

@@ -6,45 +6,51 @@ namespace Proto4 {
6 6
 
7 7
     Ground::Ground() {}
8 8
 
9
-    Ground::Ground(sf::Vector2f pos, float size, sf::Time timeout) {
10
-        shape = sf::CircleShape{size, 50};
11
-        shape.setFillColor(sf::Color(90, 100, 120));
12
-        shape.setOrigin(size, size);
13
-        shape.setPosition(pos);
9
+    Ground::Ground(sf::Vector2f pos, float size, sf::Time fadeDuration)
10
+        : sf::CircleShape(size, 50)
11
+        , Timer(fadeDuration) {
12
+        setFillColor(sf::Color(90, 100, 120));
13
+        setOrigin(size, size);
14
+        setPosition(pos);
14 15
     }
15 16
 
16
-    void Ground::update(float timestep) {}
17
-
18
-    void Ground::draw(sf::RenderTarget &window, sf::RenderStates states) const {
19
-        window.draw(shape);
17
+    // TODO: self destruct once 0?
18
+    void Ground::update(sf::Time timestep) {
19
+        Timer::update(timestep);
20
+        sf::Color col = getFillColor();
21
+        col.a = getPercent() * 255;
22
+        setFillColor(col);
20 23
     }
21 24
 
22 25
     /////////////////////////////////////////
23 26
 
24 27
     Level::Level() {}
25 28
     Level::Level(sf::Vector2f pos = sf::Vector2f(0, 0)) {
26
-        lastSpawn.position = pos;
27
-        Proto4::rotate(lastSpawn.direction, sf::Vector2f(1, 1), randomFloat() * 360.f);
29
+        setPosition(pos);
28 30
 
29 31
         // initialize with first ground
30 32
         spawn(400);
33
+        Proto4::rotate(lastSpawn.direction, sf::Vector2f(1, 1), randomFloat() * 360.f);
31 34
     }
32 35
 
33
-    void Level::update(float timestep) {}
36
+    void Level::update(sf::Time timestep) {
37
+        for (auto &ground : groundArea) {
38
+            ground.update(timestep);
39
+        }
40
+    }
34 41
 
35
-    void Level::draw(sf::RenderTarget &window, sf::RenderStates states) const {
42
+    void Level::draw(sf::RenderTarget &target, sf::RenderStates states) const {
43
+        states.transform *= getTransform();
36 44
         for (auto &ground : groundArea) {
37
-            window.draw(ground);
45
+            target.draw(ground, states);
38 46
         }
39 47
     }
40 48
 
41 49
     void Level::spawn(float size) {
42
-        float firstOffset = lastSpawn.size == 0 ? 0.f : 1.f;
43
-
44 50
         lastSpawn.direction = normalized(Proto4::rotate(lastSpawn.direction, randomFloat() * 160 - 80));
45
-        lastSpawn.position += lastSpawn.direction * (lastSpawn.size + firstOffset * 0.7f * size);
51
+        lastSpawn.position += lastSpawn.direction * (lastSpawn.size + 0.7f * size);
46 52
         lastSpawn.size = size;
47 53
 
48
-        groundArea.push_back(Ground(lastSpawn.position, size, sf::seconds(10000)));
54
+        groundArea.push_back(Ground(lastSpawn.position, size, sf::seconds(10)));
49 55
     }
50 56
 }

+ 17
- 8
level.hpp View File

@@ -1,20 +1,29 @@
1 1
 #pragma once
2 2
 
3
+#include <cmath>
3 4
 #include <SFML/Graphics.hpp>
4 5
 
5 6
 namespace Proto4 {
6 7
 
8
+    class Timer {
9
+        const sf::Time duration;
10
+        sf::Time elapsed = sf::seconds(0.f);
7 11
 
8
-    class Ground : public sf::Drawable, public sf::Transformable {
9
-        sf::CircleShape shape{};
12
+    public:
13
+        Timer() : duration(sf::seconds(10)) {}
14
+        Timer(sf::Time duration) : duration(duration) {}
15
+        void update(sf::Time timestep) { elapsed += timestep; }
16
+        float getPercent() {
17
+            return std::max(0.f, 1 - elapsed / duration);
18
+        }
19
+    };
10 20
 
21
+    class Ground : public sf::CircleShape, Timer {
11 22
     public:
12 23
         Ground();
13
-        Ground(sf::Vector2f pos, float size, sf::Time timeout);
14
-        // Ground(Killable parentEntity);
24
+        Ground(sf::Vector2f pos, float size, sf::Time fadeDuration);
15 25
 
16
-        virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
17
-        void update(float timestep);
26
+        void update(sf::Time timestep);
18 27
     };
19 28
 
20 29
 
@@ -32,8 +41,8 @@ namespace Proto4 {
32 41
         Level();
33 42
         Level(sf::Vector2f pos);
34 43
 
35
-        virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
36
-        void update(float timestep);
44
+        virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;
45
+        void update(sf::Time timestep);
37 46
         void spawn(float size);
38 47
     };
39 48
 }

Loading…
Cancel
Save