-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGame.cpp
More file actions
379 lines (311 loc) · 8.21 KB
/
Copy pathGame.cpp
File metadata and controls
379 lines (311 loc) · 8.21 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "Game.h"
#include "GameInfo.h"
Game::Game()
{
// window initialization
this->window = new sf::RenderWindow(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Mario");
map.load("assets/image/map-01.png", sf::Vector2u(64, 64)); //contains objects for map
view.reset(sf::FloatRect(0.f, 0.f, WINDOW_WIDTH, WINDOW_HEIGHT));
gameInfo.reset(); //resets game stats
addMobs(); //adds enemies
//when mario eats mushroom
mushroomBuffer.loadFromFile(MUSHROOM_SOUND);
mushroomSound.setBuffer(mushroomBuffer);
}
Game::~Game()
{
delete this->window;
}
void Game::intersection(Mario& mario, Entity& entity) //handle collision of mario and mobs
{
Bonus b;
if (entity.getIsAlive()) {
if (mario.getSprite().getGlobalBounds().intersects(entity.getSprite().getGlobalBounds())) //if mario intersects mob
if (entity.getIsFriendly()) //if entity is friendly mario collects it
{
entity.dead();
mario.setBigMario(true);
mushroomSound.play();
gameInfo.increaseScoreBonus();
}
else //if entity is not friendly
{
if (abs(mario.bottom() - entity.top()) < 10 && abs(mario.left() - entity.left()) < 54 && entity.isKillable())
//mario jumped on the entity, entity dies
{
if (mario.getIsAlive())
{
mario.killingMove();
entity.dead();
stompBuffer.loadFromFile(STOMP_SOUND);
stompSound.setBuffer(stompBuffer);
stompSound.play();
gameInfo.increaseScoreBonus();
}
}
else //mario was hit by the entity, mario dies
{
if (entity.getIsAlive())
{
menu.setIsON(true); //if all lives lost, shows menu
mario.killingMove();
mario.killingMove();
mario.dead();
if (mario.lives == 2)
{
gameInfo.showLife2();
}
else if (mario.lives == 1)
{
gameInfo.showLife1();
}
if (mario.getIsAlive()) {
menu.setIsON(false); //if mario still has lives, doesn't show menu
view.setCenter(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); //center view on mario
}
}
}
}
}
}
void Game::updateSFMLEvents() //handles pause and exit
{
while (this->window->pollEvent(this->sfEvent))
{
if (this->sfEvent.type == sf::Event::Closed) //when player closes window
this->window->close();
if (this->sfEvent.type == sf::Event::KeyReleased) //when player presses esc, game pauses
{
if (sfEvent.key.code == Keyboard::Escape)
{
menu.setIsON(true);
}
}
}
}
void Game::update()
{
this->updateSFMLEvents(); //handles pause and exit
for (int i = 0; i < mobs.size(); i++)
{
if (abs((mario.getPosition().x - mobs.at(i).getPosition().x)) < WINDOW_WIDTH / 1.6) //for optimization, mobs are created only when mario is near
{
intersection(mario, mobs.at(i)); //checks if mario has touched mob
if (mobs.at(i).getIsAlive()) //handles movement of mobs; if mob hits brick direction reversed
{
int movingSide = map.collision(mobs.at(i), gameInfo);
if (movingSide == LEFT)
mobs.at(i).MovingDirectiongLeft();
else if (movingSide == RIGHT)
mobs.at(i).MovingDirectiongRight();
mobs.at(i).update();
}
}
}
int marioHit = map.collision(mario, gameInfo); //checks what mario is touching
if (marioHit == BOTTOM) //if mario is on the ground he can jump
{
mario.setCanJump(true);
}
if (marioHit == TOP)
{
mario.fallDown();
mario.setCanJump(false);
}
if (!mario.getIsAlive())
menu.setIsON(true);
if (marioHit == END_GAME && !won) //if mario reaches end game point
{
//resets mario's lives, opens window to input name to save score
//also prompts "You Win" screen
sf::Time delayTime = sf::milliseconds(200);
mario.isAlive = false;
window->clear();
window->setVisible(false);
mario.lives = 0;
mario.goToStart();
menu.isON();
gameInfo.saveResultToFile();
window->setVisible(true);
sf::sleep(delayTime);
won = true;
}
mario.update(map.getMapWidth());
Bonuses(); //update bonuses
}
void Game::render() //draws maps, mobs and handles view
{
this->window->clear();
window->draw(map);
//always prints game stats in center
gameInfo.followMario(view.getCenter().x);
menu.followMario(mario.getPosition().x);
gameInfo.draw(*window, view.getCenter().x);
if (mario.getIsAlive()) //draws mario
window->draw(mario);
drawMobs();
cameraMovement(); //handles current game view
window->setView(view);
this->window->display();
}
void Game::Menu(int center) //handles controls in menu
{
if (center != WINDOW_WIDTH / 2)
center = WINDOW_WIDTH / 2;
menu.followMario(center); //always draws menu in the center
menu.draw(*window, center); //draws menu
if (won) //if game won
{
mario.lives = 3;
menu.gameWon(center, *window);
menu.isON();
}
if (menu.GetPressedItem() == 2)
{
menu.drawBestResults(*window, center);
}
if (menu.GetPressedItem() == 3)
{
menu.drawHelpMenu(*window, center);
}
this->window->display();
while (window->pollEvent(sfEvent))
{
if (sfEvent.type == Event::Closed)
window->close();
if (sfEvent.type == Event::KeyReleased)
{
if (sfEvent.key.code == Keyboard::Up)
{
menu.MoveUp();
}
if (sfEvent.key.code == Keyboard::Down)
{
menu.MoveDown();
}
if (sfEvent.key.code == Keyboard::Enter)
{
if (menu.GetPressedItem() == 0)
{
if (mario.getIsAlive() == false)
{
menu.setPressedItem(1);
menu.loadResultsToArray();
}
else
menu.setIsON(false);
}
if (menu.GetPressedItem() == 1)
{
mario.reset();
menu.reset();
won = false;
mario.setBigMario(false);
addMobs();
view.reset(sf::FloatRect(0.f, 0.f, WINDOW_WIDTH, WINDOW_HEIGHT));
gameInfo.reset();
map.loadArrayFromArray("assets/txt/array.txt");
map.load("assets/image/map-01.png", sf::Vector2u(64, 64));
menu.setIsON(false);
}
if (menu.GetPressedItem() == 4)
{
window->close();
delete this->window;
exit(0);
}
}
}
}
}
void Game::run() //starts game
{
while (this->window->isOpen())
{
if (menu.isON())
Menu(view.getCenter().x); //draws menu
else {
this->update(); //handles pause, exit, collision
this->render(); //draws map, mobs, game info
gameInfo.countTime();
}
}
}
void Game::cameraMovement() //sets view on mario
{
if (mario.getPosition().x != WINDOW_WIDTH / 2)
view.setCenter({ mario.getPosition().x, WINDOW_HEIGHT / 2 });
}
void Game::repairSFMLTextures()
{
//SFML
//SFML error:
//Object has its texture after adding it to some container, texture gets lost and replaced with white texture.
//To solve this repairSFMLTexture() function was added to reload textures when our object are yet in container.
for (int i = 0; i < mobs.size(); i++)
{
mobs.at(i).repair();
}
}
void Game::drawMobs() //draws enemies
{
for (int i = 0; i < mobs.size(); i++)
{
if (mobs[i].getIsAlive())
window->draw(mobs.at(i));
}
}
void Game::Bonuses()
{
if (map.getBonus() == true)
{
Bonus bonus({ mario.getPosition().x - 100, mario.getPosition().y - 128 });
mobs.push_back(bonus);
repairSFMLTextures();
map.setBonus(false);
}
}
void Game::addMobs()
{
// Co-ordinates for mobs are read from file "mobs.txt"
// file structure
// mobName Posiion.x,Position.y
// example
// Turtle 0200,130
// Spikey 3600,400
std::string line;
float x, y;
std::fstream infile;
std::string mobName;
Entity* wsk;
mobs.clear();
infile.open("assets/txt/mobs.txt");
if (!infile) {
std::cout << "can not open file to read results from";
}
while (getline(infile, line)) {
{
mobName = line.substr(0, 6);
x = atof(line.substr(7, 4).c_str());
y = atof(line.substr(12, 3).c_str());
if (mobName.compare("Turtle") == 0)
wsk = new Turtle;
else if (mobName.compare("Spikey") == 0)
wsk = new Spikey;
else if (mobName.compare("FlyTur") == 0)
wsk = new FlyTur;
else if (mobName.compare("Plant1") == 0)
wsk = new Plant1;
else if (mobName.compare("Plant2") == 0)
wsk = new Plant2;
else
wsk = new Abysss; // if no mobName read from exist take Abysss
wsk->setPosition({ x, y });
mobs.push_back(*wsk);
//This line of code adds a copy of the object pointed to by wsk to the mobs vector.
//The push_back function is a method available for vectors, and it adds a new element to the end of the vector.
}
}
infile.close();
repairSFMLTextures();
}