-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
303 lines (242 loc) · 9.23 KB
/
Copy pathmain.cpp
File metadata and controls
303 lines (242 loc) · 9.23 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
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window/Event.hpp>
#include <iostream>
#include "arena.cpp"
#include "globals.h"
#include "pacman_movement.h"
#include "pacman_draw.h"
#include "xp_orbs.h"
#include "powerups.h"
#include "sounds.h"
#include "ghost_visuals.h"
#include "ghosts.h"
#include "victory_screen.h"
int main() {
unsigned map_width_variable = (unsigned)baseMap[0].size();
unsigned map_height_variable = (unsigned)baseMap.size();
const float ui_offset_value = 40.0f;
sf::RenderWindow window(sf::VideoMode(sf::Vector2u(
map_width_variable * (unsigned)TILE_SIZE,
map_height_variable * (unsigned)TILE_SIZE + (unsigned)ui_offset_value)),
"Pac-Man Core");
sf::Font font("C:/Windows/Fonts/arial.ttf");
sf::SoundBuffer blop_sound_buffer = makeBlopSound();
sf::SoundBuffer powerup_sound_buffer = makePowerupSound();
sf::SoundBuffer shield_sound_buffer = makeShieldSound();
sf::SoundBuffer ghost_eat_sound_buffer = makeGhostEatSound();
sf::SoundBuffer death_sound_buffer = makeDeathSound();
sf::SoundBuffer win_sound_buffer = makeWinSound();
sf::Sound blop_sound(blop_sound_buffer);
sf::Sound powerup_sound(powerup_sound_buffer);
sf::Sound shield_sound(shield_sound_buffer);
sf::Sound ghost_eat_sound(ghost_eat_sound_buffer);
sf::Sound death_sound(death_sound_buffer);
sf::Sound win_sound(win_sound_buffer);
Entity pacman;
std::vector<std::shared_ptr<Ghost>> ghosts = initializeGhosts(ui_offset_value);
int number_of_lives = 3;
int current_score = 0;
bool shield_is_active = false;
float shield_timer_value = 0.f;
bool power_is_active = false;
float power_timer_value = 0.f;
sf::Vector2f pacman_spawn_position;
pacman_spawn_position.x = 14 * TILE_SIZE + TILE_SIZE / 2;
pacman_spawn_position.y = 5 * TILE_SIZE + TILE_SIZE / 2 + ui_offset_value;
std::vector<sf::Vector2f> orbs = spawnOrbs(ui_offset_value);
bool keep_playing_flag = true;
while (keep_playing_flag == true) {
pacman.pos = pacman_spawn_position;
pacman.currentDir = Direction::Left;
pacman.queuedDir = Direction::Left;
pacman.color = sf::Color::Yellow;
current_score = 0;
number_of_lives = 3;
shield_is_active = false;
shield_timer_value = 0.f;
power_is_active = false;
power_timer_value = 0.f;
ghosts = initializeGhosts(ui_offset_value);
orbs = spawnOrbs(ui_offset_value);
std::vector<sf::Vector2f> valid_spawn_spots;
unsigned r = 0;
while (r < baseMap.size()) {
unsigned c = 0;
while (c < baseMap[r].size()) {
bool is_good_spot = true;
if (baseMap[r][c] == '#') { is_good_spot = false; }
if (baseMap[r][c] == 'x') { is_good_spot = false; }
if (baseMap[r][c] == '-') { is_good_spot = false; }
if (is_good_spot == true) {
sf::Vector2f spot;
spot.x = c * TILE_SIZE + TILE_SIZE / 2;
spot.y = r * TILE_SIZE + TILE_SIZE / 2 + ui_offset_value;
valid_spawn_spots.push_back(spot);
}
c = c + 1;
}
r = r + 1;
}
std::srand((unsigned)std::time(nullptr));
std::vector<Powerup> powerups_list;
if (valid_spawn_spots.size() > 0) {
int random_idx = std::rand() % (int)valid_spawn_spots.size();
Powerup health_pu;
health_pu.pos = valid_spawn_spots[random_idx];
health_pu.type = PowerupType::Health;
health_pu.active = true;
powerups_list.push_back(health_pu);
valid_spawn_spots.erase(valid_spawn_spots.begin() + random_idx);
}
if (valid_spawn_spots.size() > 0) {
int random_idx = std::rand() % (int)valid_spawn_spots.size();
Powerup power_pu;
power_pu.pos = valid_spawn_spots[random_idx];
power_pu.type = PowerupType::Power;
power_pu.active = true;
powerups_list.push_back(power_pu);
valid_spawn_spots.erase(valid_spawn_spots.begin() + random_idx);
}
if (valid_spawn_spots.size() > 0) {
int random_idx = std::rand() % (int)valid_spawn_spots.size();
Powerup shield_pu;
shield_pu.pos = valid_spawn_spots[random_idx];
shield_pu.type = PowerupType::Shield;
shield_pu.active = true;
powerups_list.push_back(shield_pu);
valid_spawn_spots.erase(valid_spawn_spots.begin() + random_idx);
}
sf::Clock clock;
GameState current_game_state = GameState::Playing;
float animation_time = 0.f;
while (window.isOpen() && current_game_state == GameState::Playing) {
float dt = clock.restart().asSeconds();
if (dt > 0.1f) {
dt = 0.1f;
}
animation_time = animation_time + dt;
while (const std::optional<sf::Event> ev = window.pollEvent()) {
if (ev->is<sf::Event::Closed>()) {
window.close();
}
}
handlePacmanInput(pacman);
moveEntity(pacman, PACMAN_SPEED, dt, ui_offset_value, map_width_variable);
updateGhosts(ghosts, pacman, dt, ui_offset_value, animation_time);
bool was_hit = checkGhostCollision(pacman, ghosts, shield_is_active, power_is_active,
ghost_eat_sound, death_sound, current_score);
if (was_hit == true) {
number_of_lives = number_of_lives - 1;
if (number_of_lives <= 0) {
current_game_state = GameState::GameOver;
break;
}
pacman.pos = pacman_spawn_position;
}
int orbs_count_before = (int)orbs.size();
collectOrbs(orbs, pacman.pos, current_score);
int orbs_count_after = (int)orbs.size();
if (orbs_count_after < orbs_count_before) {
if (blop_sound.getStatus() != sf::Sound::Status::Playing) {
blop_sound.play();
}
}
if (orbs.size() == 0) {
current_game_state = GameState::GameWon;
win_sound.play();
break;
}
int pu_index = 0;
while (pu_index < (int)powerups_list.size()) {
if (powerups_list[pu_index].active == false) {
pu_index = pu_index + 1;
continue;
}
float dist_to_powerup = calcDist(pacman.pos, powerups_list[pu_index].pos);
if (dist_to_powerup < TILE_SIZE * 0.7f) {
powerups_list[pu_index].active = false;
if (powerups_list[pu_index].type == PowerupType::Health) {
if (number_of_lives < 3) {
number_of_lives = number_of_lives + 1;
}
powerup_sound.play();
}
else if (powerups_list[pu_index].type == PowerupType::Power) {
power_is_active = true;
power_timer_value = 8.0f;
frightenGhosts(ghosts, 8.0f);
powerup_sound.play();
}
else if (powerups_list[pu_index].type == PowerupType::Shield) {
shield_is_active = true;
shield_timer_value = 10.0f;
shield_sound.play();
}
}
pu_index = pu_index + 1;
}
if (power_is_active == true) {
power_timer_value = power_timer_value - dt;
if (power_timer_value <= 0.f) {
power_is_active = false;
}
}
if (shield_is_active == true) {
shield_timer_value = shield_timer_value - dt;
if (shield_timer_value <= 0.f) {
shield_is_active = false;
}
}
window.clear(sf::Color::Black);
drawArena(window, ui_offset_value, animation_time);
drawOrbs(window, orbs);
int draw_pu_index = 0;
while (draw_pu_index < (int)powerups_list.size()) {
if (powerups_list[draw_pu_index].active == false) {
draw_pu_index = draw_pu_index + 1;
continue;
}
if (powerups_list[draw_pu_index].type == PowerupType::Health) {
drawHealthPowerup(window, powerups_list[draw_pu_index].pos, animation_time);
}
else if (powerups_list[draw_pu_index].type == PowerupType::Power) {
drawPowerPowerup(window, powerups_list[draw_pu_index].pos, animation_time);
}
else if (powerups_list[draw_pu_index].type == PowerupType::Shield) {
drawShieldPowerup(window, powerups_list[draw_pu_index].pos, animation_time);
}
draw_pu_index = draw_pu_index + 1;
}
drawPacman(window, pacman.pos, pacman.color, pacman.currentDir, animation_time);
drawGhostsWrapper(window, ghosts, animation_time);
if (shield_is_active == true) {
drawShieldAura(window, pacman.pos, animation_time);
}
drawScore(window, font, current_score, number_of_lives);
window.display();
}
if (window.isOpen()) {
bool did_win = false;
if (current_game_state == GameState::GameWon) {
did_win = true;
}
EndScreen end_screen(font, did_win);
EndChoice player_choice = end_screen.run(window);
if (player_choice == EndChoice::PlayAgain) {
if (window.isOpen() == false) {
window.create(sf::VideoMode(sf::Vector2u(map_width_variable * (unsigned)TILE_SIZE,
map_height_variable * (unsigned)TILE_SIZE +
(unsigned)ui_offset_value)),
"Pac-Man Core");
}
continue;
} else {
keep_playing_flag = false;
}
} else {
keep_playing_flag = false;
}
}
return 0;
}