-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghost.cpp
More file actions
339 lines (308 loc) · 9.59 KB
/
ghost.cpp
File metadata and controls
339 lines (308 loc) · 9.59 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
#include "Headers/ghost.h"
#include "Headers/game_map.h"
bool caught(pm_data pacman, ghost_data i_ghost)
{
if(circles_intersect(i_ghost.x, i_ghost.y, ENTITIES_RADIUS, pacman.x, pacman.y, ENTITIES_RADIUS))
{
return 1;
}
return 0;
}
void reset_ghost_pos(ghost_data &i_ghost)
{
i_ghost.x = i_ghost.home[0] * CELL_SIZE;
i_ghost.y = i_ghost.home[1] * CELL_SIZE;
}
float get_optimal_path(ghost_data i_ghost, int i_direction)
{
int x = i_ghost.x;
int y = i_ghost.y;
switch (i_direction)
{
case 0: // right
x += GHOST_SPEED;
break;
case 1: // up
y -= GHOST_SPEED;
break;
case 2: // left
x -= GHOST_SPEED;
break;
case 3: // down
y += GHOST_SPEED;
break;
default:
break;
}
return float(sqrt(pow(x - i_ghost.target[0], 2) + pow(y - i_ghost.target[1], 2)));
}
void update_target(ghost_data &i_ghost, pm_data i_pacman, ghost_data &i_ghost_red)
{
if(i_ghost.use_door == 1)//have not left the house
{
i_ghost.target[0] = i_ghost.exit[0];
i_ghost.target[1] = i_ghost.exit[1];
int target_x = i_ghost.target[0] * CELL_SIZE;
int target_y = i_ghost.target[1] * CELL_SIZE;
if(i_ghost.x == target_x && i_ghost.y == target_y)
{
i_ghost.use_door = 0;
}
}
else
{
if(i_ghost.mode == SCATTER)
{
switch(i_ghost.id)
{
case 1://red
{
i_ghost.target[0] = CELL_SIZE * (CELL_NUM_WIDTH - 1);
i_ghost.target[1] = 0;
break;
}
case 2://blue
{
i_ghost.target[0] = CELL_SIZE * (CELL_NUM_WIDTH - 1);
i_ghost.target[1] = CELL_SIZE * (CELL_NUM_HEIGHT - 1);
break;
}
case 3://orange
{
i_ghost.target[0] = 0;
i_ghost.target[1] = CELL_SIZE * (CELL_NUM_HEIGHT - 1);
break;
}
case 4://pink
{
i_ghost.target[0] = 0;
i_ghost.target[1] = 0;
break;
}
default:
break;
}
}
else if(i_ghost.mode == CHASE)
{
switch (i_ghost.id)
{
case 1://red
{
i_ghost.target[0] = i_pacman.x;
i_ghost.target[1] = i_pacman.y;
break;
}
case 2://blue
{
i_ghost.target[0] = i_pacman.x;
i_ghost.target[1] = i_pacman.y;
switch (i_pacman.direction)
{
case 1://right
{
i_ghost.target[0] += GHOST_B_CHASE * CELL_SIZE;
break;
}
case 2://up
{
i_ghost.target[1] -= GHOST_B_CHASE * CELL_SIZE;
break;
}
case 3://left
{
i_ghost.target[0] -= GHOST_B_CHASE * CELL_SIZE;
break;
}
case 4://down
{
i_ghost.target[1] += GHOST_B_CHASE * CELL_SIZE;
break;
}
default:
break;
}
i_ghost.target[0] += i_ghost.target[0] - i_ghost_red.x;
i_ghost.target[1] += i_ghost.target[1] - i_ghost_red.y;
break;
}
case 3://orange
{
if(CELL_SIZE * GHOST_O_CHASE <= sqrt(pow(i_ghost.x - i_pacman.x, 2) + pow(i_ghost.y - i_pacman.y, 2)))
{
i_ghost.target[0] = i_pacman.x;
i_ghost.target[1] = i_pacman.y;
}
else
{
i_ghost.target[0] = 0;
i_ghost.target[1] = CELL_SIZE * (CELL_NUM_HEIGHT - 1);
}
break;
}
case 4://pink
{
i_ghost.target[0] = i_pacman.x;
i_ghost.target[1] = i_pacman.y;
switch (i_pacman.direction)
{
case 1://right
i_ghost.target[0] += GHOST_P_CHASE * CELL_SIZE;
break;
case 2://up
i_ghost.target[1] -= GHOST_P_CHASE * CELL_SIZE;
break;
case 3://left
i_ghost.target[0] -= GHOST_P_CHASE * CELL_SIZE;
break;
case 4://down
i_ghost.target[1] += GHOST_P_CHASE * CELL_SIZE;
break;
}
break;
}
default:
break;
}
}
}
}
void update_ghost(ghost_data &i_ghost, pm_data &i_pacman , ghost_data i_ghost_red, game_data &game, std::array<std::array<cell_data, CELL_NUM_HEIGHT>, CELL_NUM_WIDTH> &map)
{
bool move = 0;
int available_ways = 0;
int speed = GHOST_SPEED;
if(i_pacman.mode == ENERGIZED && i_ghost.mode != EATEN)
{
i_ghost.mode = SCARED;
speed = GHOST_SCARED_SPEED;
}
update_target(i_ghost, i_pacman, i_ghost_red);
bool walls[4];//0:right 1:up 2:left 3:down
walls[0] = map_collision(0, 0, i_ghost.use_door, i_ghost.x + speed, i_ghost.y, map);
walls[1] = map_collision(0, 0, i_ghost.use_door, i_ghost.x, i_ghost.y - speed, map);
walls[2] = map_collision(0, 0, i_ghost.use_door, i_ghost.x - speed, i_ghost.y, map);
walls[3] = map_collision(0, 0, i_ghost.use_door, i_ghost.x, i_ghost.y + speed, map);
if(i_ghost.mode != SCARED && i_ghost.mode != EATEN)
{
int optimal_direction = 4;
move = 1;
for(int a = 0; a < 4; a++)//get optimal direction
{
if(a == (i_ghost.direction + 2) % 4)
{
continue; //ghost cant turn back
}
else if(walls[a] == 0)
{
available_ways++;
if(optimal_direction == 4)
{
optimal_direction = a;
}
if(get_optimal_path(i_ghost, a) < get_optimal_path(i_ghost, optimal_direction))
{
optimal_direction = a;
}
}
}
if(available_ways > 1)//atleast 2 paths
{
i_ghost.direction = optimal_direction;
}
else
{
if (optimal_direction == 4)//ghost can turn back if stuck
{
i_ghost.direction = (i_ghost.direction + 2) % 4;
}
else//take the only path remaining
{
i_ghost.direction = optimal_direction;
}
}
}
else if(i_ghost.mode == SCARED)//scared mode
{
int random_direction = rand() % 4;
move = 1;
for(int a = 0; a < 4; a++)
{
if(a == (i_ghost.direction + 2) % 4)
{
continue; //ghost cant turn back
}
else if(walls[a] == 0)
{
available_ways++;
}
}
if(available_ways > 0)//atleast 1 path
{
while(walls[random_direction] == 1 || random_direction == (i_ghost.direction + 2) % 4)
{
random_direction = rand() % 4;
}
i_ghost.direction = random_direction;
}
else
{
//turn back if there's no other way
i_ghost.direction = (i_ghost.direction + 2) % 4;
}
}
if(move == 1)
{
switch (i_ghost.direction)
{
case 0://right
{
i_ghost.x += speed;
break;
}
case 1://up
{
i_ghost.y -= speed;
break;
}
case 2://left
{
i_ghost.x -= speed;
break;
}
case 3://down
{
i_ghost.y += speed;
break;
}
default:
break;
}
if (i_ghost.x <= -CELL_SIZE) // ghost using tunnel
{
i_ghost.x = CELL_NUM_WIDTH * CELL_SIZE - CELL_SIZE;
}
else if (i_ghost.x >= CELL_NUM_WIDTH * CELL_SIZE)
{
i_ghost.x = -CELL_SIZE;
}
}
if(caught(i_pacman, i_ghost) == 1)
{
if(i_ghost.mode != SCARED)//pacman dies
{
i_pacman.mode = DEAD;
game.pm_lives --;
}
else//home we go
{
play_sound_effect(EAT_GHOST);
draw_text("200", COLOR_WHITE, FONT_NAME, FONT_SIZE - 5, i_ghost.x, i_ghost.y - CELL_SIZE/3);
refresh_screen();
delay(700);
i_ghost.mode = EATEN;
reset_ghost_pos(i_ghost);
game.points += GHOST_PTS;
}
}
}