-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
278 lines (234 loc) · 7.99 KB
/
Game.cpp
File metadata and controls
278 lines (234 loc) · 7.99 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
#pragma once
#include <iostream>
#include "Game.hpp"
#include "Enums.hpp"
#include "ResourceManager.hpp"
#include "Consts.hpp"
#include "Blinky.hpp"
#include "Inky.hpp"
#include "Pinky.hpp"
#include "Clyde.hpp"
#include "Zlatko.hpp"
Game::Game()
:window(sf::VideoMode({ RESOLUTION_X, RESOLUTION_Y }), "Pac-Man"),
grid(std::vector<std::string>{
"XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"X............XX............X",
"X.XXXX.XXXXX.XX.XXXXX.XXXX.X",
"X@XXXX.XXXXX.XX.XXXXX.XXXX@X",
"X.XXXX.XXXXX.XX.XXXXX.XXXX.X",
"X..........................X",
"X.XXXX.XX.XXXXXXXX.XX.XXXX.X",
"X.XXXX.XX.XXXXXXXX.XX.XXXX.X",
"X......XX....XX....XX......X",
"XXXXXX.XXXXX XX XXXXX.XXXXXX",
"XXXXXX.XXXXX XX XXXXX.XXXXXX",
"XXXXXX.XX XX.XXXXXX",
"XXXXXX.XX XXX XXX XX.XXXXXX",
"XXXXXX.XX X X XX.XXXXXX",
" . X X . ",
"XXXXXX.XX X X XX.XXXXXX",
"XXXXXX.XX XXXXXXXX XX.XXXXXX",
"XXXXXX.XX XX.XXXXXX",
"XXXXXX.XX XXXXXXXX XX.XXXXXX",
"XXXXXX.XX XXXXXXXX XX.XXXXXX",
"X............XX............X",
"X.XXXX.XXXXX.XX.XXXXX.XXXX.X",
"X.XXXX.XXXXX.XX.XXXXX.XXXX.X",
"X@..XX....... .......XX..@X",
"XXX.XX.XX.XXXXXXXX.XX.XX.XXX",
"XXX.XX.XX.XXXXXXXX.XX.XX.XXX",
"X......XX....XX....XX......X",
"X.XXXXXXXXXX.XX.XXXXXXXXXX.X",
"X.XXXXXXXXXX.XX.XXXXXXXXXX.X",
"X..........................X",
"XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
}),
pacman(&grid)
{
window.setFramerateLimit(60);
// Init times for switcing ghost modes
// Reverse order
float values[] = { 1000000, 5, 20, 5, 20, 7, 20, 7 };
for (auto x : values)
this->timeForStateSwitch.push(x);
// Init ghosts , order is really important to match GhostArray enum
ghosts.push_back(new Blinky(&grid)); // Blinky(RED) == 0
ghosts.push_back(new Pinky(&grid)); // Pinky(PINK) == 1
ghosts.push_back(new Clyde(&grid)); // Cylde(ORANGE) == 2
ghosts.push_back(new Inky(&grid)); // Inky(Blue) == 3
ghosts.push_back(new Zlatko(&grid)); // Zlatko(Green) == 4
}
void Game::run()
{
sf::Clock clock;
this->pause();
while (window.isOpen())
{
float dt = clock.restart().asSeconds();
handleEvents();
if (running) {
update(dt);
}
render();
std::cout << globalGhostState << std::endl;
//std::cout << "Direction: " << direction << " Queued: " << queuedDirection << std::endl;
//std::cout << "Blinky: " << ghosts[0]->getState() << std::endl;
//std::cout << "Pacman: " << pacman.getState() << std::endl;
}
}
void Game::handleEvents()
{
while (const std::optional event = window.pollEvent())
{
if (event->is<sf::Event::Closed>())
window.close();
// KEYBOARD
if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>()) {
// GRID TOGGLE
if (keyPressed->scancode == sf::Keyboard::Scancode::G) {
showGrid = !showGrid;
continue;
}
// PAUSE TOGGLE
if (keyPressed->scancode == sf::Keyboard::Scancode::Space) {
togglePause();
continue;
}
// MOVEMENT
DirectionEnum newDirection = DirectionEnum::NONE;
if (keyPressed->scancode == sf::Keyboard::Scancode::A) {
newDirection = DirectionEnum::LEFT;
}
if (keyPressed->scancode == sf::Keyboard::Scancode::D) {
newDirection = DirectionEnum::RIGHT;
}
if (keyPressed->scancode == sf::Keyboard::Scancode::S) {
newDirection = DirectionEnum::DOWN;
}
if (keyPressed->scancode == sf::Keyboard::Scancode::W) {
newDirection = DirectionEnum::UP;
}
// Ignore multiple clicks in same direction
if (newDirection == pacman.getDirection()) {
pacman.setQueuedDirection(DirectionEnum::NONE);
}
else {
pacman.setQueuedDirection(newDirection);
}
}
}
}
void Game::render()
{
window.clear();
// Grid
grid.render(window , showGrid);
// Pacman
pacman.render(window);
// Ghosts should dissapear in ending sequence
if (!endingSequence) {
for (auto& ghost : ghosts) {
ghost->render(window);
}
}
//Score
sf::Text score_text(ResourceManager::instance().getFont("resources/Emulogic-zrEw.ttf"));
score_text.setString("Score:" + std::to_string(pacman.getScore()));
score_text.setCharacterSize(24);
score_text.setPosition({ 15.f, 15.f });
window.draw(score_text);
window.display();
}
void Game::update(float delta)
{
// Stop everything , do ending animation, then stop everything
if (endingSequence) {
static float lastAnimationDuration = 2.4;
lastAnimationDuration -= delta;
if (lastAnimationDuration < 0) {
this->pause();
}
pacman.updateAnimation(delta);
return;
}
cycleGlobalGhostState(delta);
// Update tile
pacman.updateActorTile(grid);
for (auto& ghost : ghosts) {
ghost->updateActorTile(grid);
}
// Update Animation
pacman.updateAnimation(delta);
for (auto& ghost : ghosts) {
ghost->updateAnimation(delta);
}
// Update Ghost pathfinding
for (auto& ghost : ghosts) {
ghost->updateTarget(pacman, ghosts);
ghost->updateNextDirection();
}
static float pinkyTimer = 5, inkyTimer = 10, clydeTimer = 15;
pinkyTimer -= delta; inkyTimer -= delta; clydeTimer -= delta;
// Update Movement
pacman.updateMovement(this);
ghosts[int(GhostArray::BLINKY)]->updateMovement(this);
ghosts[int(GhostArray::ZLATKO)]->updateMovement(this);
if (pinkyTimer < 0) {
ghosts[int(GhostArray::PINKY)]->updateMovement(this);
}
if (inkyTimer < 0) {
ghosts[int(GhostArray::INKY)]->updateMovement(this);
}
if (clydeTimer < 0) {
ghosts[int(GhostArray::CLYDE)]->updateMovement(this);
}
}
void Game::triggerFright()
{
// If already in fright state, just reset timer
if (globalGhostState == GhostState::FRIGHTENED) {
frightTimeRemaining = FRIGHT_TIME;
return;
}
frightTimeRemaining = FRIGHT_TIME;
globalGhostStateBeforeFright = globalGhostState;
globalGhostState = GhostState::FRIGHTENED;
pacman.setState(PacmanState::FRIGHTENED);
}
void Game::cycleGlobalGhostState(float delta)
{
// Fright state overpowers other states
// Make sure fright state lasts FRIGHT_TIME
if (globalGhostState == GhostState::FRIGHTENED) {
frightTimeRemaining -= delta;
if (frightTimeRemaining < 0) {
globalGhostState = globalGhostStateBeforeFright;
pacman.setState(PacmanState::ALIVE);
}
}
// Update global state
if (!timeForStateSwitch.empty() && globalGhostState != GhostState::FRIGHTENED) {
float time = timeForStateSwitch.top(); timeForStateSwitch.pop();
time -= delta;
if (time < 0) {
if (globalGhostState == GhostState::CHASE)
globalGhostState = GhostState::SCATTER;
else if (globalGhostState == GhostState::SCATTER)
globalGhostState = GhostState::CHASE;
}
else {
timeForStateSwitch.push(time);
}
}
for (auto& ghost : ghosts) {
// Unless dead ghosts should follow global state
if (ghost->getState() != GhostState::DEAD) {
// Reverse directions when flipping states
if (ghost->getState() != globalGhostState) {
ghost->setState(globalGhostState);
ghost->setQueuedDirection(getReverse(ghost->getDirection()));
}
}
}
}