Browse Source

add stub for Level

master
noerw 1 year ago
parent
commit
4cb31fbe86
8 changed files with 309 additions and 147 deletions
  1. 39
    0
      IDEAS.md
  2. 1
    1
      Makefile
  3. 12
    0
      game.cpp
  4. 2
    0
      game.hpp
  5. 50
    0
      level.cpp
  6. 39
    0
      level.hpp
  7. 16
    0
      random.hpp
  8. 150
    146
      vec2util.hpp

+ 39
- 0
IDEAS.md View File

@@ -0,0 +1,39 @@
1
+- gameobject:
2
+    - has associated groundspace
3
+    - can spawn new game objects with timers
4
+    - player, enemies, powerups, bullets(?)
5
+
6
+- groundspace
7
+    - limits players area to move, over bounds -> game over
8
+        - everything else may leave groundspace
9
+    - circular, fades out after time
10
+    - size & timeout depends on associated enemy HP
11
+        - overrideable
12
+        - some helper spaces stay indefinitely
13
+    - idea: multiple floors: player falls down -> parallax layers rotate
14
+
15
+- enemies
16
+    - spawn new enemies after time in patterns (dependend on HP?)
17
+        - spawn in semirandomized direction (not backwards), buffered by current groundspace
18
+   - types:
19
+       - stationshooter (different patterns -> colorcoding)
20
+       - (shooting)follower: always facing player (different speeds?)
21
+       - fastmover: 
22
+       - grower: fast to kill but grows until dead
23
+
24
+- player actions
25
+    - primary & secondary gun (fast/slow bullets?, raygun?)
26
+    - speed boost: cost: attracts other gameobjects
27
+    - make room: grow distance & size of gameobjects 
28
+    - slow bullet that creates groundspace around it
29
+
30
+- level
31
+    - based on patterns spawned by enemies
32
+    - maybe: not random patterns but predefined selection per level
33
+    - increasing difficulty:
34
+       - incresing angle of random nextspawn vectors
35
+       - reduced groundspace per spawn
36
+       - faster groundspace timeout 
37
+       - faster spawn rate
38
+
39
+- powerups?

+ 1
- 1
Makefile View File

@@ -8,7 +8,7 @@ LDFLAGS = -L "SFML21/lib" -lsfml-system -lsfml-window -lsfml-graphics
8 8
 # for Windows using MinGW
9 9
 #LDFLAGS = -L "SFML21/lib" -lmingw32 -lsfml-main -lsfml-system -lsfml-window -lsfml-graphics
10 10
 
11
-OBJ = game.o player.o background.o mainmenu.o main.o 
11
+OBJ = level.o game.o player.o background.o mainmenu.o main.o
12 12
 
13 13
 game: $(OBJ)
14 14
 	$(CC) $(CFLAGS) -o game.bin $(OBJ) $(LDFLAGS)

+ 12
- 0
game.cpp View File

@@ -48,6 +48,7 @@ namespace Proto4 {
48 48
 
49 49
     void Game::draw(sf::RenderTarget &window, sf::RenderStates states) const {
50 50
         window.setView(mainView);
51
+        window.draw(level);
51 52
         window.draw(player);
52 53
     }
53 54
 
@@ -59,6 +60,17 @@ namespace Proto4 {
59 60
 
60 61
     bool Game::start() {
61 62
         player.setPosition(mainView.getCenter());
63
+        level = Level(mainView.getCenter());
64
+        level.spawn(200);
65
+        level.spawn(200);
66
+        level.spawn(200);
67
+        level.spawn(700);
68
+        level.spawn(100);
69
+        level.spawn(100);
70
+        level.spawn(100);
71
+        level.spawn(100);
72
+        level.spawn(100);
73
+        level.spawn(100);
62 74
         state = GameState::Running;
63 75
         return true;
64 76
     }

+ 2
- 0
game.hpp View File

@@ -4,6 +4,7 @@
4 4
 #include <SFML/Window.hpp>
5 5
 
6 6
 #include "player.hpp"
7
+#include "level.hpp"
7 8
 
8 9
 #pragma once
9 10
 
@@ -29,6 +30,7 @@ namespace Proto4 {
29 30
         sf::View mainView{};
30 31
         GameState state = GameState::Uninitialized;
31 32
         Player player{};
33
+        Level level{};
32 34
         uint32_t score = 0;
33 35
     };
34 36
 }

+ 50
- 0
level.cpp View File

@@ -0,0 +1,50 @@
1
+#include "level.hpp"
2
+#include "vec2util.hpp"
3
+#include "random.hpp"
4
+
5
+namespace Proto4 {
6
+
7
+    Ground::Ground() {}
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);
14
+    }
15
+
16
+    void Ground::update(float timestep) {}
17
+
18
+    void Ground::draw(sf::RenderTarget &window, sf::RenderStates states) const {
19
+        window.draw(shape);
20
+    }
21
+
22
+    /////////////////////////////////////////
23
+
24
+    Level::Level() {}
25
+    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);
28
+
29
+        // initialize with first ground
30
+        spawn(400);
31
+    }
32
+
33
+    void Level::update(float timestep) {}
34
+
35
+    void Level::draw(sf::RenderTarget &window, sf::RenderStates states) const {
36
+        for (auto &ground : groundArea) {
37
+            window.draw(ground);
38
+        }
39
+    }
40
+
41
+    void Level::spawn(float size) {
42
+        float firstOffset = lastSpawn.size == 0 ? 0.f : 1.f;
43
+
44
+        lastSpawn.direction = normalized(Proto4::rotate(lastSpawn.direction, randomFloat() * 160 - 80));
45
+        lastSpawn.position += lastSpawn.direction * (lastSpawn.size + firstOffset * 0.7f * size);
46
+        lastSpawn.size = size;
47
+
48
+        groundArea.push_back(Ground(lastSpawn.position, size, sf::seconds(10000)));
49
+    }
50
+}

+ 39
- 0
level.hpp View File

@@ -0,0 +1,39 @@
1
+#pragma once
2
+
3
+#include <SFML/Graphics.hpp>
4
+
5
+namespace Proto4 {
6
+
7
+
8
+    class Ground : public sf::Drawable, public sf::Transformable {
9
+        sf::CircleShape shape{};
10
+
11
+    public:
12
+        Ground();
13
+        Ground(sf::Vector2f pos, float size, sf::Time timeout);
14
+        // Ground(Killable parentEntity);
15
+
16
+        virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
17
+        void update(float timestep);
18
+    };
19
+
20
+
21
+    struct SpawnState {
22
+        sf::Vector2f position;
23
+        float size = 0;
24
+        sf::Vector2f direction;
25
+    };
26
+
27
+    class Level : public sf::Drawable, public sf::Transformable {
28
+        std::vector<Ground> groundArea;
29
+        SpawnState lastSpawn{};
30
+
31
+    public:
32
+        Level();
33
+        Level(sf::Vector2f pos);
34
+
35
+        virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
36
+        void update(float timestep);
37
+        void spawn(float size);
38
+    };
39
+}

+ 16
- 0
random.hpp View File

@@ -0,0 +1,16 @@
1
+#pragma once
2
+
3
+#include <random>
4
+
5
+namespace Proto4 {
6
+    namespace Random {
7
+        std::random_device r;
8
+        std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
9
+        std::mt19937 eng{seed};
10
+        std::uniform_real_distribution<> dist(0, 1);
11
+    }
12
+
13
+    inline float randomFloat() {
14
+        return Random::dist(Random::eng);
15
+    }
16
+}

+ 150
- 146
vec2util.hpp View File

@@ -12,166 +12,170 @@
12 12
 
13 13
 typedef sf::Vector2f Vec2;
14 14
 
15
-/**
16
- * @brief dot
17
- * @param a vector
18
- * @param b vector
19
- * @return a . b
20
- */
21
-static inline float dot (const Vec2& a, const Vec2& b)
22
-{
23
-    return a.x*b.x + a.y*b.y;
24
-}
15
+namespace Proto4 {
16
+
17
+    /**
18
+    * @brief dot
19
+    * @param a vector
20
+    * @param b vector
21
+    * @return a . b
22
+    */
23
+    static inline float dot (const Vec2& a, const Vec2& b)
24
+    {
25
+        return a.x*b.x + a.y*b.y;
26
+    }
25 27
 
26
-/**
27
- * @brief magnitudeSq
28
- * @param v vector
29
- * @return |v|^2
30
- */
31
-static inline float magnitudeSq (const Vec2& v)
32
-{
33
-    return v.x*v.x + v.y*v.y;
34
-}
28
+    /**
29
+    * @brief magnitudeSq
30
+    * @param v vector
31
+    * @return |v|^2
32
+    */
33
+    static inline float magnitudeSq (const Vec2& v)
34
+    {
35
+        return v.x*v.x + v.y*v.y;
36
+    }
35 37
 
36
-/**
37
- * @brief magnitude
38
- * @param v vector
39
- * @return |v|
40
- */
41
-static inline float magnitude (const Vec2& v)
42
-{
43
-    return sqrt (v.x*v.x + v.y*v.y);
44
-}
38
+    /**
39
+    * @brief magnitude
40
+    * @param v vector
41
+    * @return |v|
42
+    */
43
+    static inline float magnitude (const Vec2& v)
44
+    {
45
+        return sqrt (v.x*v.x + v.y*v.y);
46
+    }
45 47
 
46 48
 
47
-/**
48
- * @brief normalize
49
- * @param v vector
50
- * v' = v / |v|
51
- */
52
-static inline void normalize (Vec2& v)
53
-{
54
-    float len = sqrt (v.x*v.x + v.y*v.y);
55
-    if (len != 0) {
56
-        v.x = v.x / len;
57
-        v.y = v.y / len;
49
+    /**
50
+    * @brief normalize
51
+    * @param v vector
52
+    * v' = v / |v|
53
+    */
54
+    static inline void normalize (Vec2& v)
55
+    {
56
+        float len = sqrt (v.x*v.x + v.y*v.y);
57
+        if (len != 0) {
58
+            v.x = v.x / len;
59
+            v.y = v.y / len;
60
+        }
58 61
     }
59
-}
60 62
 
61
-/**
62
- * @brief normalized (faster)
63
- * @param res vector
64
- * @param v vector
65
- * res = v / |v|
66
- */
67
-static inline void normalized (Vec2& res, const Vec2& v)
68
-{
69
-    float len = sqrt (v.x*v.x + v.y*v.y);
70
-    if (len != 0) {
71
-        res.x = v.x / len;
72
-        res.y = v.y / len;
63
+    /**
64
+    * @brief normalized (faster)
65
+    * @param res vector
66
+    * @param v vector
67
+    * res = v / |v|
68
+    */
69
+    static inline void normalized (Vec2& res, const Vec2& v)
70
+    {
71
+        float len = sqrt (v.x*v.x + v.y*v.y);
72
+        if (len != 0) {
73
+            res.x = v.x / len;
74
+            res.y = v.y / len;
75
+        }
73 76
     }
74
-}
75 77
 
76
-/**
77
- * @brief normalized
78
- * @param v vector
79
- * @return res = v / |v|
80
- */
81
-static inline Vec2 normalized (const Vec2& v)
82
-{
83
-    Vec2 res;
84
-    normalized(res, v);
85
-    return res;
86
-}
78
+    /**
79
+    * @brief normalized
80
+    * @param v vector
81
+    * @return res = v / |v|
82
+    */
83
+    static inline Vec2 normalized (const Vec2& v)
84
+    {
85
+        Vec2 res;
86
+        normalized(res, v);
87
+        return res;
88
+    }
87 89
 
88
-/**
89
- * @brief truncate (faster)
90
- * @param res vector
91
- * @param v vector
92
- * @param max magnitude
93
- * res = max * v / |v|
94
- */
95
-static inline void truncate (Vec2& res, const Vec2& v, float max)
96
-{
97
-    float len = sqrt (v.x*v.x + v.y*v.y);
98
-    if (len > max)
90
+    /**
91
+    * @brief truncate (faster)
92
+    * @param res vector
93
+    * @param v vector
94
+    * @param max magnitude
95
+    * res = max * v / |v|
96
+    */
97
+    static inline void truncate (Vec2& res, const Vec2& v, float max)
99 98
     {
100
-        res = normalized(v);
101
-        res *= max;
102
-    } else {
103
-        res = v;
99
+        float len = sqrt (v.x*v.x + v.y*v.y);
100
+        if (len > max)
101
+        {
102
+            res = normalized(v);
103
+            res *= max;
104
+        } else {
105
+            res = v;
106
+        }
104 107
     }
105
-}
106 108
 
107
-/**
108
- * @brief truncate
109
- * @param v vector
110
- * @param max magnitude
111
- * @return res = max * v / |v|
112
- */
113
-static inline Vec2 truncate (const Vec2& v, float max)
114
-{
115
-    Vec2 res;
116
-    truncate(res, v, max);
117
-    return res;
118
-}
109
+    /**
110
+    * @brief truncate
111
+    * @param v vector
112
+    * @param max magnitude
113
+    * @return res = max * v / |v|
114
+    */
115
+    static inline Vec2 truncate (const Vec2& v, float max)
116
+    {
117
+        Vec2 res;
118
+        truncate(res, v, max);
119
+        return res;
120
+    }
119 121
 
120
-/**
121
- * @brief lerp (faster)
122
- * @param res vector
123
- * @param a vector
124
- * @param b vector
125
- * @param t alpha value
126
- * res = (1-t)*a + t*b
127
- */
128
-static inline void lerp (Vec2& res, const Vec2& a, const Vec2& b, float t)
129
-{
130
-    res.x = (1-t)*a.x + t*b.x;
131
-    res.y = (1-t)*a.y + t*b.y;
132
-}
122
+    /**
123
+    * @brief lerp (faster)
124
+    * @param res vector
125
+    * @param a vector
126
+    * @param b vector
127
+    * @param t alpha value
128
+    * res = (1-t)*a + t*b
129
+    */
130
+    static inline void lerp (Vec2& res, const Vec2& a, const Vec2& b, float t)
131
+    {
132
+        res.x = (1-t)*a.x + t*b.x;
133
+        res.y = (1-t)*a.y + t*b.y;
134
+    }
133 135
 
134
-/**
135
- * @brief lerp
136
- * @param a vector
137
- * @param b vector
138
- * @param t alpha value
139
- * @return res = (1-t)*a + t*b
140
- */
141
-static inline Vec2 lerp (const Vec2& a, const Vec2& b, float t)
142
-{
143
-    Vec2 res;
144
-    lerp(res, a, b, t);
145
-    return res;
146
-}
136
+    /**
137
+    * @brief lerp
138
+    * @param a vector
139
+    * @param b vector
140
+    * @param t alpha value
141
+    * @return res = (1-t)*a + t*b
142
+    */
143
+    static inline Vec2 lerp (const Vec2& a, const Vec2& b, float t)
144
+    {
145
+        Vec2 res;
146
+        lerp(res, a, b, t);
147
+        return res;
148
+    }
147 149
 
148
-/**
149
- * @brief rotate
150
- * @param res vector
151
- * @param v vector
152
- * @param angle
153
- * R   = |cos -sin|
154
- *       |sin  cos|
155
- * res =  v * R
156
- */
157
-static inline void rotate (Vec2& res, const Vec2& v, float angle)
158
-{
159
-    const float rad = DEG2RAD * angle;
160
-    const float sin0 = sin(rad);
161
-    const float cos0 = cos(rad);
162
-    res.x = v.x * cos0 + v.y * -sin0;
163
-    res.y = v.x * sin0 + v.y * cos0;
164
-}
150
+    /**
151
+    * @brief rotate
152
+    * @param res vector
153
+    * @param v vector
154
+    * @param angle
155
+    * R   = |cos -sin|
156
+    *       |sin  cos|
157
+    * res =  v * R
158
+    */
159
+    static inline void rotate (Vec2& res, const Vec2& v, float angle)
160
+    {
161
+        const float rad = DEG2RAD * angle;
162
+        const float sin0 = sin(rad);
163
+        const float cos0 = cos(rad);
164
+        res.x = v.x * cos0 + v.y * -sin0;
165
+        res.y = v.x * sin0 + v.y * cos0;
166
+    }
167
+
168
+    /**
169
+    * @brief rotate
170
+    * @param v vector
171
+    * @param angle
172
+    * @return res = v * R
173
+    */
174
+    static inline Vec2 rotate (Vec2& v, float angle)
175
+    {
176
+        Vec2 res;
177
+        rotate(res, v, angle);
178
+        return res;
179
+    }
165 180
 
166
-/**
167
- * @brief rotate
168
- * @param v vector
169
- * @param angle
170
- * @return res = v * R
171
- */
172
-static inline Vec2 rotate (Vec2& v, float angle)
173
-{
174
-    Vec2 res;
175
-    rotate(res, v, angle);
176
-    return res;
177 181
 }

Loading…
Cancel
Save