Skip to content

Commit 0ed161e

Browse files
author
Martin Pelak
committed
Added a container, removed CRTP
1 parent 95929b0 commit 0ed161e

14 files changed

+137
-38
lines changed

Begin.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "Begin.h"

Begin.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#include "Frame.h"
3+
4+
class Begin : public Frame
5+
{
6+
public:
7+
void tick(const Time currentTime) override
8+
{
9+
10+
}
11+
};
12+

Bullet.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
#include "GameComponent.h"
33

4-
class Bullet : public GameComponent<Bullet>
4+
class Bullet : public GameComponent
55
{
66
public:
77
const Sprite& sprite;
@@ -10,16 +10,21 @@ class Bullet : public GameComponent<Bullet>
1010
this->size = size;
1111
}
1212

13-
void tick(const Time currentTime)
13+
void draw()
1414
{
15-
this->position.y -= 4;
16-
this->angle += 0.1f;
17-
1815
DrawSprite(sprite, position.x, position.y, size.width, size.height, angle, 0xffffffff);
1916
}
2017

2118
void setPosition(const Position position)
2219
{
2320
this->position = position;
2421
}
22+
23+
void tick(const Time currentTime)
24+
{
25+
this->position.y -= 4;
26+
this->angle += 0.1f;
27+
28+
this->draw();
29+
}
2530
};

Drawable.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
class Drawable
3+
{
4+
public:
5+
using Time = int;
6+
virtual void tick(const Time currentTime) = 0;
7+
};
8+

Enemy.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "GameComponent.h"
33
#include <numbers>
44

5-
class Enemy : public GameComponent<Enemy>
5+
class Enemy : public GameComponent
66
{
77
private:
88
static int s_seed;
@@ -29,15 +29,20 @@ class Enemy : public GameComponent<Enemy>
2929
this->init();
3030
}
3131

32+
void draw()
33+
{
34+
DrawSprite(sprite, position.x, position.y, size.width, size.height, angle, 0xffffffff);
35+
}
36+
3237
void tick(const Time time)
3338
{
3439
int dx = 0, dy = 0;
3540
const int n1 = time + seed * seed + seed * seed * seed;
3641
const int n2 = time + seed + seed * seed + seed * seed * seed * 3;
3742
if (((n1 >> 6) & 0x7) == 0x7)
3843
{
39-
dx += (1 - cos((n1 & 0x7f) / 64.0f * 2.f * std::numbers::pi)) * (20 + ((seed * seed) % 9));
40-
dy += (sin((n1 & 0x7f) / 64.0f * 2.f * std::numbers::pi)) * (20 + ((seed * seed) % 9));
44+
dx = (1 - cos((n1 & 0x7f) / 64.0f * 2.f * std::numbers::pi)) * (20 + ((seed * seed) % 9));
45+
dy = (sin((n1 & 0x7f) / 64.0f * 2.f * std::numbers::pi)) * (20 + ((seed * seed) % 9));
4146
}
4247

4348
if (((n2 >> 8) & 0xf) == 0xf)
@@ -47,6 +52,6 @@ class Enemy : public GameComponent<Enemy>
4752

4853
this->position = this->basePosition + Position(dx, dy);
4954

50-
DrawSprite(sprite, position.x, position.y, size.width, size.height, angle, 0xffffffff);
55+
this->draw();
5156
}
5257
};

Frame.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "Frame.h"

Frame.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
#include "Drawable.h"
3+
4+
class Frame: public Drawable
5+
{
6+
};
7+

GameComponent.h

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include "Drawable.h"
23
#include "Sprite.h"
34

45
struct Coordinates
@@ -40,12 +41,10 @@ struct Size
4041

4142
using Position = Coordinates;
4243
using Delta = Coordinates;
43-
using Time = int;
4444
using Angle = float;
4545
using Seed = int;
4646

47-
template <typename T1>
48-
class GameComponent
47+
class GameComponent: public Drawable
4948
{
5049
public:
5150
Coordinates position;
@@ -54,10 +53,5 @@ class GameComponent
5453
Angle angle{};
5554

5655
public:
57-
GameComponent() {};
58-
void tick(const Time currentTime)
59-
{
60-
T1& derived = static_cast<T1&>(*this);
61-
derived.tick(currentTime);
62-
}
56+
virtual void tick(const Time currentTime) = 0;
6357
};

Player.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "GameComponent.h"
33
#include <numbers>
44

5-
class Player : public GameComponent<Player>
5+
class Player : public GameComponent
66
{
77
public:
88
const Sprite& sprite;
@@ -12,16 +12,16 @@ class Player : public GameComponent<Player>
1212
this->size = size;
1313
}
1414

15-
void tick(const Time time)
16-
{
17-
this->detectMovement();
18-
19-
DrawSprite(sprite, position.x, position.y, size.width, size.height, std::numbers::pi + sin(time * 0.1) * 0.1, 0xffffffff);
20-
}
21-
2215
void detectMovement() noexcept
2316
{
2417
position.x += IsKeyDown(VK_LEFT) ? -7 : IsKeyDown(VK_RIGHT) ? 7 : 0;
2518
position.y += IsKeyDown(VK_UP) ? -7 : IsKeyDown(VK_DOWN) ? 7 : 0;
2619
}
20+
21+
void tick(const Time time) override
22+
{
23+
this->detectMovement();
24+
25+
DrawSprite(sprite, position.x, position.y, size.width, size.height, std::numbers::pi + sin(time * 0.1) * 0.1, 0xffffffff);
26+
}
2727
};

Sprite.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Sprite
2020
};
2121

2222
// Sprites is a singleton class
23-
// It loads letters (a-z), numbers (0-9), invaders (littele and big) and the bullet
23+
// It loads letters (a-z), numbers (0-9), invaders (little and big) and the bullet
2424
class Sprites
2525
{
2626
private:

Text.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22
#include "GameComponent.h"
3-
class Text : public GameComponent<Text>
3+
class Text : public GameComponent
44
{
55
private:
66
std::string_view text;

leet.vcxproj

+6
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,11 @@
100100
</Link>
101101
</ItemDefinitionGroup>
102102
<ItemGroup>
103+
<ClCompile Include="Begin.cpp" />
103104
<ClCompile Include="Bullet.cpp" />
105+
<ClCompile Include="Drawable.cpp" />
104106
<ClCompile Include="Enemy.cpp" />
107+
<ClCompile Include="Frame.cpp" />
105108
<ClCompile Include="GameComponent.cpp" />
106109
<ClCompile Include="lib\leetlib.cpp" />
107110
<ClCompile Include="main.cpp" />
@@ -110,8 +113,11 @@
110113
<ClCompile Include="Text.cpp" />
111114
</ItemGroup>
112115
<ItemGroup>
116+
<ClInclude Include="Begin.h" />
113117
<ClInclude Include="Bullet.h" />
118+
<ClInclude Include="Drawable.h" />
114119
<ClInclude Include="Enemy.h" />
120+
<ClInclude Include="Frame.h" />
115121
<ClInclude Include="GameComponent.h" />
116122
<ClInclude Include="lib\leetlib.h" />
117123
<ClInclude Include="Player.h" />

leet.vcxproj.filters

+21
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<Filter Include="Source Files\GameComponents">
1212
<UniqueIdentifier>{f081bdaa-a4b6-4044-9881-c8e39f123edf}</UniqueIdentifier>
1313
</Filter>
14+
<Filter Include="Source Files\Frames">
15+
<UniqueIdentifier>{1728aaa0-800d-46c9-b9c6-5b87818ec57b}</UniqueIdentifier>
16+
</Filter>
1417
</ItemGroup>
1518
<ItemGroup>
1619
<ClCompile Include="main.cpp">
@@ -37,6 +40,15 @@
3740
<ClCompile Include="Text.cpp">
3841
<Filter>Source Files\GameComponents</Filter>
3942
</ClCompile>
43+
<ClCompile Include="Begin.cpp">
44+
<Filter>Source Files\Frames</Filter>
45+
</ClCompile>
46+
<ClCompile Include="Frame.cpp">
47+
<Filter>Source Files\Frames</Filter>
48+
</ClCompile>
49+
<ClCompile Include="Drawable.cpp">
50+
<Filter>Source Files</Filter>
51+
</ClCompile>
4052
</ItemGroup>
4153
<ItemGroup>
4254
<ClInclude Include="lib\leetlib.h">
@@ -60,5 +72,14 @@
6072
<ClInclude Include="Text.h">
6173
<Filter>Source Files\GameComponents</Filter>
6274
</ClInclude>
75+
<ClInclude Include="Begin.h">
76+
<Filter>Source Files\Frames</Filter>
77+
</ClInclude>
78+
<ClInclude Include="Frame.h">
79+
<Filter>Source Files\Frames</Filter>
80+
</ClInclude>
81+
<ClInclude Include="Drawable.h">
82+
<Filter>Source Files</Filter>
83+
</ClInclude>
6384
</ItemGroup>
6485
</Project>

main.cpp

+49-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,57 @@
11
#include "lib/leetlib.h"
2-
#include <math.h>
32
#include <algorithm>
3+
#include <deque>
4+
#include <math.h>
45

56
#include "Bullet.h"
67
#include "Enemy.h"
8+
#include "GameComponent.h"
79
#include "Player.h"
810
#include "Sprite.h"
9-
#include "GameComponent.h"
1011
#include "Text.h"
1112

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+
1255
void Game()
1356
{
1457
Sprites& sprites = Sprites::getSprites();
@@ -17,13 +60,13 @@ void Game()
1760
Player player = { sprites.player, Position(400, 550), Size(50, 50) };
1861

1962
// 10 bullets
20-
std::vector<Bullet> bullets = { 10, { sprites.bullet, Size(10, 10) } };
63+
Container<Bullet> bullets = { 10, { sprites.bullet, Size(10, 10) } };
2164

2265
// 50 enemies
23-
std::vector<Enemy> enemies = { 50, { sprites.enemy } };
66+
Container<Enemy> enemies = { 50, { sprites.enemy } };
2467

2568
// Title
26-
Text header = { "space invaders 2d", sprites.alphabet, Position{80, 30} };
69+
Text header = { "space invaders 2d", sprites.alphabet, Position(80, 30) };
2770

2871
int time = 0;
2972
auto bulletToFire = bullets.begin();
@@ -50,11 +93,7 @@ void Game()
5093
if (counter == 0)
5194
{
5295
bulletToFire->setPosition(player.position);
53-
++bulletToFire;
54-
if (bulletToFire == bullets.end())
55-
{
56-
bulletToFire = bullets.begin();
57-
}
96+
bullets.next(bulletToFire);
5897

5998
counter = 15;
6099
}

0 commit comments

Comments
 (0)