-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
331 lines (277 loc) · 11.4 KB
/
game.cpp
File metadata and controls
331 lines (277 loc) · 11.4 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
//
// Created by dogfetus on 15.04.23.
//
#include <SFML/Graphics.hpp>
#include "game.h"
#include <random>
#include <iostream>
#include <string>
Game::Game() :
player(500, 500, window, config, player_texture),
map(window, config),
menu(window),
Game_handler([this]{Game_functions();})
{
std::string image_folder = "Images/";
window.create(sf::VideoMode(config.window.Width, config.window.Height), config.window.Title);
clock.restart();
Game_handler.launch();
player_texture.loadFromFile ( image_folder + "packman.png" );
enemy_texture.loadFromFile (image_folder + "Ghost.png" );
// map.LoadFromFile ("Map/Packmap.json", image_folder + "Map.png");
map.LoadFromString("#########################"
"#o..........#..........o#"
"#.###.#####.#.#####.###.#"
"#.# #.# #.#.# #.# #.#"
"#.###.#####.#.#####.###.#"
"#.......................#"
"#.###.#.#########.#.###.#"
"#.....#.....#.....#.....#"
"#####.#####.#.#####.#####"
" #.#...#.#.#...#.# "
" #.#.#...G...#.#.# "
"#####.#.#.##D##.#.#.#####"
"........#.#GGG#.#........"
"#####.#.#.#####.#.#.#####"
" #.#.....o.....#.# "
" #.#.#########.#.# "
"#####.#.#...#...#.#.#####"
"#.........#.#.#.........#"
"#.###.#####.#.#####.###.#"
"#...#.......P.......#...#"
"###.#.#.##.###.##.#.#.###"
"#.....#.....#.....#.....#"
"#.#########.#.#########.#"
"#o.....................o#"
"#########################");
//map.LoadFromString("########################## # ## ### ##### # ##### ### ## ### ##### # ##### ### ## ### ##### # ##### ### ## ## ### # ######### # ### ## # # # ###### ##### # ##### ##### # # # # # # # # # # G # # # ##### # # ##D## # # ##### # #GGG# # ##### # # ##### # # ##### # # # # # # ######### # # ##### # # # # # ###### # # # ## ### ##### # ##### ### ## # P . # #### # # ## ### ## # # #### # # # ## ######### # ######### ## ##########################");
menu.LoadFromFile({image_folder + "Title.png",
image_folder + "Play.png",
image_folder + "Reset.png",
image_folder + "Settings.png",
image_folder + "Exit.png"
});
window.setFramerateLimit(config.window.FrameRateLimit);
Score = 0;
player.setStartPos(map.GetPlayerSpawn());
numbers.loadFromFile(image_folder + "numbers.png");
Retrieve_points();
}
bool Game::is_running() {
float delta = clock.restart().asSeconds();
num_of_loop++;
//VSync
window.setVerticalSyncEnabled(config.window.VSync);
//interrupt
sf::Event event{};
while (window.pollEvent(event)) {
//closes program if 'x' is hit
if (event.type == sf::Event::Closed || menu.exit()){
window.close();
return false;
}
}
//Black background
window.clear(sf::Color::Black);
if (level_cleared)
menu.show();
//if game is paused
if (menu.isShowing()){
menu.pickAnOption();
menu.draw();
if (menu.reset() || (menu.play() && level_cleared)){
reset();
level_cleared = false;
}
}
// If game is not paused:
else{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::F5) || player.getLives() <= 0)
reset();
map.draw();
mutex1.lock();
for (auto point : points){
point->draw();
}
for (auto bst : boost){
bst->draw();
}
if (!player.isDead())
player.move(delta, map.GetTiles());
player.draw();
for (auto enemy : enemy_list) {
if(config.enemy.Move && !player.isDead())
enemy->move(delta, map.GetTiles());
else
enemy->reset();
enemy->regulateSpeed(player.isInvincible());
enemy->draw();
}
mutex1.unlock();
Draw_score();
flag.notify_all();
}
//buffer swap: Flips which buffer is shown. Front / Back.
window.display();
if (num_of_loop >= 20){
std::cout << 1.f/delta << std::endl;
num_of_loop = 0;
}
return true;
}
void Game::Game_functions() {
std::random_device RanDev;
std::mt19937_64 generator(RanDev());
std::uniform_int_distribution<int> random_width(0, config.window.Width-(int)config.enemy.Size);
std::uniform_int_distribution<int> random_height(0, config.window.Height-(int)config.enemy.Size);
std::uniform_int_distribution<int> random_color(0, 7);
std::mutex flag_mutex;
std::unique_lock<std::mutex> lock(flag_mutex);
sf::Color color;
while (window.isOpen()){
flag.wait(lock);
float spawnPosY;
float spawnPosX;
for (auto &pos : map.GetGhostSpawn()){
if (enemy_list.size() >= map.GetGhostSpawn().size())
continue;
switch (random_color(generator)){
case 0 : color = sf::Color::Yellow; break;
case 1 : color = sf::Color::Red; break;
case 2 : color = sf::Color::Green; break;
case 3 : color = sf::Color::Cyan; break;
case 4 : color = sf::Color::White; break;
case 5 :
case 6 : color = sf::Color::Blue; break;
case 7 : color = sf::Color::Magenta; break;
}
spawnPosX = pos.x;
spawnPosY = pos.y;
auto *enemy = new Enemy(spawnPosX,
spawnPosY,
window, config, enemy_texture, color);
enemy_list.emplace_back(enemy);
}
for (auto enemy = enemy_list.begin(); enemy != enemy_list.end();) {
if (player.intersects(*(*enemy)) &&
config.player.hitbox ){
if (player.isInvincible()) {
mutex1.lock();
player.grow((*enemy)->getPoints() * pointMultiplier);
Score = (int)player.getPoints();
pointMultiplier *= 2;
delete *enemy;
enemy = enemy_list.erase(enemy);
mutex1.unlock();
}
else {
player.die();
enemy++;
}
}
else {
enemy++;
if (!player.isInvincible())
pointMultiplier = 1;
}
}
for (auto point = points.begin(); point != points.end();){
mutex1.lock();
if (player.intersects(*(*point))){
player.grow((*point)->getPoints());
Score = (int)player.getPoints();
delete *point;
point = points.erase(point);
}else
point++;
mutex1.unlock();
}
for (auto bst = boost.begin(); bst != boost.end();){
mutex1.lock();
if (player.intersects(*(*bst))){
player.grow((*bst)->getPoints());
Score = (int)player.getPoints();
delete *bst;
bst = boost.erase(bst);
player.turnInvincible();
}else
bst++;
mutex1.unlock();
}
if (points.empty() && boost.empty())
level_cleared = true;
}
}
void Game::reset() {
// map.LoadFromFile("Map/Packmap.json", "Images/Map.png");
// map.LoadFromString(map.getMapAsString());
for (auto &enemy : enemy_list)
enemy->reset();
player.reset();
score.clear();
Score = 0;
Retrieve_points();
level_cleared = false;
}
void Game::stop() {
flag.notify_all();
Game_handler.wait();
}
void Game::Draw_score(){
std::string _score = std::to_string(Score);
float posx = 10;
float posy = 2;
int num_width = 221;
int num_height = 406;
int scnd_row_offset = 59;
for (int i = 0; i < _score.size(); i++){
if (_score.size() > score.size()){
sf::Sprite sprite;
sprite.setTexture(numbers);
sprite.setScale(.1,.1);
score.emplace_back(sprite);
}
switch (_score[i]) {
case '0': score[i].setTextureRect(sf::IntRect(0, 0, num_width, num_height)); break;
case '1': score[i].setTextureRect(sf::IntRect(num_width, 0, num_width, num_height)); break;
case '2': score[i].setTextureRect(sf::IntRect(num_width*2, 0, num_width, num_height)); break;
case '3': score[i].setTextureRect(sf::IntRect(num_width*3, 0, num_width, num_height)); break;
case '4': score[i].setTextureRect(sf::IntRect(num_width*4, 0, num_width, num_height)); break;
case '5': score[i].setTextureRect(sf::IntRect(0, num_height + scnd_row_offset, num_width, num_height)); break;
case '6': score[i].setTextureRect(sf::IntRect(num_width, num_height + scnd_row_offset, num_width, num_height)); break;
case '7': score[i].setTextureRect(sf::IntRect(num_width*2, num_height + scnd_row_offset, num_width, num_height)); break;
case '8': score[i].setTextureRect(sf::IntRect(num_width*3, num_height + scnd_row_offset, num_width, num_height)); break;
case '9': score[i].setTextureRect(sf::IntRect(num_width*4, num_height + scnd_row_offset, num_width, num_height)); break;
}
score[i].setPosition(posx + (float)(num_width * i * 0.1), posy);
window.draw(score[i]);
}
}
void Game::Retrieve_points() {
// remove previous data
for (auto point : points){
delete point;
}
points.clear();
for (auto fruit : boost){
delete fruit;
}
boost.clear();
// points are given a fixed size
for (auto& point : map.GetPointLocations()){
float pointSize = 9;
auto* entty = new Entity(point.x - pointSize/2, point.y - pointSize/2, window, config); // - 5 to place in middle of tile
entty->setPoints(10);
entty->setSize(pointSize);
entty->setColor(sf::Color(255,225,154));
points.emplace_back(entty);
}
for (auto& point : map.GetBoostLocations()){
float boostSize = 20;
auto* entty = new Entity(point.x - boostSize/2, point.y - boostSize/2, window, config); // - 5 to place in middle of tile
entty->setPoints(50);
entty->setSize(boostSize);
entty->setColor(sf::Color(255,225,154));
boost.emplace_back(entty);
}
}