-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
291 lines (253 loc) · 6.07 KB
/
Game.cpp
File metadata and controls
291 lines (253 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "Game.h"
// Private Function
//--------------------------------------------------------------------------------------
void Game::initVariables()
{
this->window = nullptr;
// Game logic
this->endGame = false;
this->point = 0;
this->health = 10;
this->enemySpawnTimeMax = 20.f;
this->enemySpawnTimer = this->enemySpawnTimeMax;
this->maxEnemies = 10;
this->mouseHeld = false;
}
void Game::initWindow()
{
this->videoMode.height = 600;
this->videoMode.width = 800;
this->window = new sf::RenderWindow(this->videoMode, "Game 01", sf::Style::Titlebar | sf::Style::Close);
this->window->setFramerateLimit(60);
}
void Game::initFonts()
{
if (this->font.loadFromFile("Fonts/000webfont Regular.ttf"))
{
std::cout << "ERROR::GAME::INITFONTS::Failed to load font!" << 'n';
}
}
void Game::initText()
{
this->uiText.setFont(this->font);
this->uiText.setCharacterSize(32);
this->uiText.setFillColor(sf::Color::White);
this->uiText.setString("NONE");
}
void Game::initEnemies()
{
this->enemy.setPosition(10.f, 10.f);
this->enemy.setSize(sf::Vector2f(100.f, 100.f));
this->enemy.setFillColor(sf::Color::Cyan);
}
// Construtors / Destructor
//--------------------------------------------------------------------------------------
Game::Game()
{
this->initVariables();
this->initWindow();
this->initFonts();
this->initText();
this->initEnemies();
}
Game::~Game()
{
delete this->window;
}
// Accessor
//--------------------------------------------------------------------------------------
const bool Game::running() const
{
return this->window->isOpen();
}
const bool Game::getEndGame() const
{
return this->endGame;
}
// Public function
//--------------------------------------------------------------------------------------
void Game::spawnEnemy()
{
/*
@ return void
Spawns enemies and set their type and color. Spawn them at random postion
-Set random type (diff);
-Set a random positions;
-Set a random colors;
-Add enemy to vector.
-Remove enemies at the end of the screen //TODO
*/
this->enemy.setPosition(
static_cast<float>(rand() % static_cast<int>(this->window->getSize().x - this->enemy.getSize().x)),
0.f
);
// Randomize enemy type
int type = rand() % 5;
switch (type)
{
case 0:
this->enemy.setSize(sf::Vector2f(10.f, 10.f));
this->enemy.setFillColor(sf::Color::Magenta);
break;
case 1:
this->enemy.setSize(sf::Vector2f(30.f, 30.f));
this->enemy.setFillColor(sf::Color::Blue);
break;
case 2:
this->enemy.setSize(sf::Vector2f(50.f, 50.f));
this->enemy.setFillColor(sf::Color::Cyan);
break;
case 3:
this->enemy.setSize(sf::Vector2f(70.f, 70.f));
this->enemy.setFillColor(sf::Color::Red);
break;
case 4:
this->enemy.setSize(sf::Vector2f(100.f, 100.f));
this->enemy.setFillColor(sf::Color::Green);
break;
default:
this->enemy.setSize(sf::Vector2f(100.f, 100.f));
this->enemy.setFillColor(sf::Color::Yellow);
break;
}
//Spawn the enemy
this->enemies.push_back(enemy);
}
void Game::pollEvent()
{
// Event polling
while (this->window->pollEvent(this->ev))
{
switch (this->ev.type)
{
case sf::Event::Closed():
this->window->close();
break;
case sf::Event::KeyPressed:
if (this->ev.key.code == sf::Keyboard::Escape)
this->window->close();
break;
}
}
}
void Game::updateMousePosition()
{
/**
@ return void
Update the mouse position:
- Mouse position relative to window (Vector2i)
*/
this->mousePosWindow = sf::Mouse::getPosition(*this->window);
this->mousePosView = this->window->mapPixelToCoords(this->mousePosWindow);
}
void Game::updateText()
{
std::stringstream ss;
ss << "Point: " << this->point << '\n'
<< "Health: " << this->health << '\n';
this->uiText.setString(ss.str());
}
void Game::updateEnemies()
{
/*
@ return void
Update the enemy spawn timer and spawn enemies
when the total amount of enemies is smaller than maximum.
Move the enemies downwards.
Remove the enemies at the edge of the screen.
*/
// Update the timer for enemy spawning
if (this->enemies.size() < this->maxEnemies)
{
if (this->enemySpawnTimer >= this->enemySpawnTimeMax)
{
this->spawnEnemy();
this->enemySpawnTimer = 0.f;
}
else
this->enemySpawnTimer += 1.f;
}
// Moving and updating enemies
for (int i = 0; i < this->enemies.size(); i++)
{
bool deleted = false;
this->enemies[i].move(0.f, 2.f);
if (this->enemies[i].getPosition().y > this->window->getSize().y)
{
this->enemies.erase(this->enemies.begin() + i);
this->health -= 1;
std::cout << "Health: " << health << '\n';
}
}
// Check if clicked upon
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
if (this->mouseHeld == false)
{
this->mouseHeld = true;
bool deleted = false;
for (size_t i = 0; i < this->enemies.size() && deleted == false; i++)
{
if (this->enemies[i].getGlobalBounds().contains(this->mousePosView))
{
// Gain point
if (this->enemies[i].getFillColor() == sf::Color::Magenta)
{
this->point += 10;
this->health += 20;
}
else if (this->enemies[i].getFillColor() == sf::Color::Blue)
this->point += 7;
else if (this->enemies[i].getFillColor() == sf::Color::Cyan)
this->point += 5;
else if (this->enemies[i].getFillColor() == sf::Color::Red)
this->point += 3;
else if (this->enemies[i].getFillColor() == sf::Color::Green)
this->point += 1;
std::cout << "Point: " << point << '\n';
// Delete the enemy
deleted = true;
this->enemies.erase(this->enemies.begin() + i);
}
}
}
}
else
{
this->mouseHeld = false;
}
}
void Game::update()
{
this->pollEvent();
if (this->endGame == false)
{
this->updateMousePosition();
this->updateText();
this->updateEnemies();
}
// End game condition
if (this->health <= 0)
{
this->endGame = true;
}
}
void Game::renderText(sf::RenderTarget& target)
{
target.draw(this->uiText);
}
void Game::renderEnemies(sf::RenderTarget& target)
{
for (auto& e : this->enemies)
{
target.draw(e);
}
}
void Game::render()
{
this->window->clear();
// Draw game objects
this->renderEnemies(*this->window);
this->renderText(*this->window);
this->window->display();
}