1
1
#include " lib/leetlib.h"
2
- #include < math.h>
3
2
#include < algorithm>
3
+ #include < deque>
4
+ #include < math.h>
4
5
5
6
#include " Bullet.h"
6
7
#include " Enemy.h"
8
+ #include " GameComponent.h"
7
9
#include " Player.h"
8
10
#include " Sprite.h"
9
- #include " GameComponent.h"
10
11
#include " Text.h"
11
12
13
+ template <typename T>
14
+ class Container : public std ::deque<T>
15
+ {
16
+ using iterator = typename std::deque<T>::iterator;
17
+ using size_type = typename std::deque<T>::size_type;
18
+ using parent = std::deque<T>;
19
+
20
+ public:
21
+ Container (const size_type count, const T& value)
22
+ {
23
+ for (size_type i = 0 ; i < count; ++i)
24
+ {
25
+ parent::push_back (value);
26
+ }
27
+ }
28
+
29
+ iterator begin () noexcept
30
+ {
31
+ return parent::begin ();
32
+ }
33
+
34
+ iterator end () noexcept
35
+ {
36
+ return parent::end ();
37
+ }
38
+
39
+ void next (iterator& iterator)
40
+ {
41
+ iterator = std::next (iterator);
42
+ if (iterator == parent::end ())
43
+ {
44
+ iterator = parent::begin ();
45
+ }
46
+ }
47
+
48
+ template <class ... Args>
49
+ T& emplace_back (Args&&... args)
50
+ {
51
+ return parent::emplace_back (std::forward<Args>(args)...);
52
+ }
53
+ };
54
+
12
55
void Game ()
13
56
{
14
57
Sprites& sprites = Sprites::getSprites ();
@@ -17,13 +60,13 @@ void Game()
17
60
Player player = { sprites.player , Position (400 , 550 ), Size (50 , 50 ) };
18
61
19
62
// 10 bullets
20
- std::vector <Bullet> bullets = { 10 , { sprites.bullet , Size (10 , 10 ) } };
63
+ Container <Bullet> bullets = { 10 , { sprites.bullet , Size (10 , 10 ) } };
21
64
22
65
// 50 enemies
23
- std::vector <Enemy> enemies = { 50 , { sprites.enemy } };
66
+ Container <Enemy> enemies = { 50 , { sprites.enemy } };
24
67
25
68
// Title
26
- Text header = { " space invaders 2d" , sprites.alphabet , Position{ 80 , 30 } };
69
+ Text header = { " space invaders 2d" , sprites.alphabet , Position ( 80 , 30 ) };
27
70
28
71
int time = 0 ;
29
72
auto bulletToFire = bullets.begin ();
@@ -50,11 +93,7 @@ void Game()
50
93
if (counter == 0 )
51
94
{
52
95
bulletToFire->setPosition (player.position );
53
- ++bulletToFire;
54
- if (bulletToFire == bullets.end ())
55
- {
56
- bulletToFire = bullets.begin ();
57
- }
96
+ bullets.next (bulletToFire);
58
97
59
98
counter = 15 ;
60
99
}
0 commit comments