-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
254 lines (197 loc) · 4.27 KB
/
Game.cpp
File metadata and controls
254 lines (197 loc) · 4.27 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
#include "Game.h"
//Private functions
void Game::initVariables()
{
this->window = nullptr;
//Game Logic
this->endGame = false;
this->points = 0;
this->health = 5;
this->enemySpawnTimer = 0.f;
this->enemySpawnTimerMax = 10.f;
this->maxEnemies = 5;
}
void Game::initFonts()
{
if (!this->font.loadFromFile("arial.ttf"))
{
std::cout << "ERROR::GAME::INITFONTS::Error loading font";
}
}
void Game::initText()
{
this->uiText.setFont(this->font);
this->uiText.setCharacterSize(24);
this->uiText.setFillColor(sf::Color::White);
this->uiText.setString("init");
}
void Game::initEnemies()
{
this->enemy.setPosition(10.f, 10.f);
this->enemy.setSize(sf::Vector2f(100.f, 100.f));
this->enemy.setFillColor(sf::Color::Blue);
this->enemy.setOutlineColor(sf::Color::White);
this->enemy.setOutlineThickness(1.f);
}
void Game::initWindow()
{
this->videoMode.height = 600;
this->videoMode.width = 800;
this->window = new sf::RenderWindow(this->videoMode, "Simple Click Game", sf::Style::Titlebar | sf::Style::Close);
this->window->setFramerateLimit(60);
}
// Constructors / Destructors
Game::Game()
{
this->initVariables();
this->initWindow();
this->initFonts();
this->initText();
this->initEnemies();
}
Game::~Game()
{
delete this->window;
}
// Public functions
void Game::updateText()
{
std::stringstream ss;
ss << "Points: " << this->points << "\n"
<< " Health: " << this->health;
this->uiText.setString(ss.str());
}
void Game::updateMousePositions()
{
//Updates mouse position relative to window (Vector2i)
this->mousePosWindow = sf::Mouse::getPosition(*this->window);
this->mousePosView = this->window->mapPixelToCoords(this->mousePosWindow);
}
void Game::updateEnemies()
/*
Updates the Enemy Spawn Timer and Spawns an enemy when the enemy count is less than max.
Moves enemy position downwards.
Removes enemy at the bottom of the screen.
*/
{
if (this->enemies.size() < this->maxEnemies)
{
if (this->enemySpawnTimer >= this->enemySpawnTimerMax)
{
//Spawn Enemy and reset timer
this->spawnEnemy();
this->enemySpawnTimer = 0.f;
}
else
{
//Update timer
this->enemySpawnTimer += 1.f;
}
}
//Itereate and operate on each enemy
for (unsigned int i = 0; i < this->enemies.size(); i++) {
bool deleted = false;
//move enemy down
this->enemies[i].move(0.f, 1.f);
//checked if clicked upon
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
if (this->enemies[i].getGlobalBounds().contains(this->mousePosView))
{
deleted = true;
//gain points
points += 10;
}
}
//check if box hit bottom of screen
if (this->enemies[i].getPosition().y > this->window->getSize().y - this->enemies[i].getSize().y)
{
deleted = true;
//Lose points if boxes hit bottom of screen
points -= 10;
health--;
if (health <= 0)
{
endGame = true;
}
}
if (deleted == true)
{
this->enemies.erase(this->enemies.begin() + i);
}
}
}
void Game::update()
{
this->updateEvents();
if (this->endGame == false)
{
this->updateMousePositions();
this->updateEnemies();
this->updateText();
}
}
void Game::renderEnemies(sf::RenderTarget& target)
{
//Render each enemy
for (auto &e : this->enemies) {
target.draw(e);
}
}
void Game::renderText(sf::RenderTarget& target)
{
target.draw(this->uiText);
}
void Game::render()
{
this->window->clear();
//Draw Game Object
this->renderEnemies(*this->window);
this->renderText(*this->window);
this->window->display();
}
const bool Game::getEndGame() const
{
if (this->endGame == true)
{
return true;
}
else {
return false;
}
}
void Game::spawnEnemy()
{
/*
Add enemy with random color and position to vector
*/
this->enemy.setPosition(
static_cast<float>(rand() % static_cast<int>(this->window->getSize().x - this->enemy.getSize().x)),
0.f
);
this->enemy.setFillColor(sf::Color::Blue);
this->enemies.push_back(this->enemy);
}
//Accessors
const bool Game::getWindowIsOpen() const
{
return this->window->isOpen();
}
void Game::updateEvents()
{
//Event Polling
while (this->window->pollEvent(this->event))
{
switch (this->event.type)
{
case sf::Event::Closed:
this->window->close();
break;
case sf::Event::KeyPressed :
if (this->event.key.code == sf::Keyboard::Escape)
{
this->window->close();
}
break;
}
}
}