-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentities.cpp
More file actions
1180 lines (1012 loc) · 34.7 KB
/
entities.cpp
File metadata and controls
1180 lines (1012 loc) · 34.7 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "entities.h"
#include <cmath>
#include <vector>
#include <algorithm>
using namespace MazeConfig;
// ============== Entity Implementation ==============
Entity::Entity(double start_x, double start_y, const std::string &palette)
: x_(start_x), y_(start_y), dir_(DIR_NONE), desired_dir_(DIR_NONE), palette_(palette), speed_multiplier_(1.0) {}
void Entity::set_position(double x, double y)
{
x_ = x;
y_ = y;
}
void Entity::update(const Maze &maze, double delta_time)
{
move_in_direction(maze, delta_time);
}
void Entity::move_in_direction(const Maze &maze, double delta_time)
{
const int col = static_cast<int>(x_ / CELL_SIZE);
const int row = static_cast<int>(y_ / CELL_SIZE);
const double center_x = Maze::get_cell_center_x(col);
const double center_y = Maze::get_cell_center_y(row);
// Try to change direction if desired direction differs from current
if (desired_dir_ != DIR_NONE && desired_dir_ != dir_)
{
attempt_direction_change(maze, row, col, center_x, center_y);
}
// Move in current direction
attempt_movement(maze, center_x, center_y, delta_time);
}
void Entity::attempt_direction_change(const Maze &maze, int row, int col, double center_x, double center_y)
{
int next_col = col;
int next_row = row;
get_next_cell(desired_dir_, next_row, next_col);
const bool aligned = is_aligned_for_direction(desired_dir_, center_x, center_y);
if (aligned && maze.is_empty(next_row, next_col))
{
align_to_grid(desired_dir_, center_x, center_y);
dir_ = desired_dir_;
}
}
void Entity::attempt_movement(const Maze &maze, double center_x, double center_y, double delta_time)
{
if (dir_ == DIR_NONE)
return;
const auto [test_x, test_y] = get_next_position(dir_, delta_time);
if (maze.can_move_to(test_x, test_y))
{
x_ = test_x;
y_ = test_y;
snap_to_grid_if_close(center_x, center_y);
}
else
{
dir_ = DIR_NONE; // Stop if can't move
}
}
void Entity::get_next_cell(direction_t direction, int &row, int &col)
{
switch (direction)
{
case DIR_LEFT:
col--;
break;
case DIR_RIGHT:
col++;
break;
case DIR_UP:
row--;
break;
case DIR_DOWN:
row++;
break;
default:
break;
}
}
bool Entity::is_aligned_for_direction(direction_t direction, double center_x, double center_y) const
{
switch (direction)
{
case DIR_LEFT:
case DIR_RIGHT:
return fabs(y_ - center_y) < ALIGNMENT_TOLERANCE;
case DIR_UP:
case DIR_DOWN:
return fabs(x_ - center_x) < ALIGNMENT_TOLERANCE;
default:
return false;
}
}
void Entity::align_to_grid(direction_t direction, double center_x, double center_y)
{
switch (direction)
{
case DIR_LEFT:
case DIR_RIGHT:
y_ = center_y;
break;
case DIR_UP:
case DIR_DOWN:
x_ = center_x;
break;
default:
break;
}
}
std::pair<double, double> Entity::get_next_position(direction_t direction, double delta_time) const
{
double test_x = x_;
double test_y = y_;
double movement = get_current_speed() * delta_time; // pixels per second * seconds = pixels
switch (direction)
{
case DIR_LEFT:
test_x -= movement;
break;
case DIR_RIGHT:
test_x += movement;
break;
case DIR_UP:
test_y -= movement;
break;
case DIR_DOWN:
test_y += movement;
break;
default:
break;
}
return {test_x, test_y};
}
void Entity::snap_to_grid_if_close(double center_x, double center_y)
{
if ((dir_ == DIR_LEFT || dir_ == DIR_RIGHT) &&
fabs(y_ - center_y) < ALIGNMENT_TOLERANCE)
{
y_ = center_y;
}
if ((dir_ == DIR_UP || dir_ == DIR_DOWN) &&
fabs(x_ - center_x) < ALIGNMENT_TOLERANCE)
{
x_ = center_x;
}
}
// ============== Pacman Implementation ==============
Pacman::Pacman(double start_x, double start_y, SpriteSheet *sheet, const std::string &palette)
: Entity(start_x, start_y, palette), sheet_(sheet), anim_state_(AnimationState::OPEN), anim_timer_(0), is_in_power_mode_(false) {}
void Pacman::capture_input()
{
if (key_down(LEFT_KEY))
set_desired_direction(DIR_LEFT);
else if (key_down(RIGHT_KEY))
set_desired_direction(DIR_RIGHT);
else if (key_down(UP_KEY))
set_desired_direction(DIR_UP);
else if (key_down(DOWN_KEY))
set_desired_direction(DIR_DOWN);
}
void Pacman::update(const Maze &maze, double delta_time)
{
Entity::update(maze, delta_time);
handle_tunnel_wrapping(maze);
update_animation(delta_time);
}
void Pacman::update(const Maze &maze, GameState &game_state, double delta_time)
{
Entity::update(maze, delta_time);
handle_tunnel_wrapping(maze);
update_animation(delta_time);
// Check for token collection
game_state.check_token_collection(get_x(), get_y());
// Check for power pellet collection
game_state.check_power_pellet_collection(get_x(), get_y());
}
void Pacman::draw() const
{
if (!sheet_)
return;
const auto [sprite_col, sprite_row, flip_x, flip_y] = get_sprite_info();
sheet_->draw_sprite_at_pixel(get_palette(), sprite_col, sprite_row,
get_x(), get_y(), SPRITE_SCALE, flip_x, flip_y, true);
}
void Pacman::handle_tunnel_wrapping(const Maze &maze)
{
const int row = static_cast<int>(floor(get_y() / CELL_SIZE));
const int col = static_cast<int>(floor(get_x() / CELL_SIZE));
// Wrap from left edge to right edge
if (col < 0)
{
const double new_x = (row >= 0 && row < MAZE_ROWS && maze.is_empty(row, MAZE_COLS - 1)) ? Maze::get_cell_center_x(MAZE_COLS - 1) : Maze::get_cell_center_x(0);
set_position(new_x, get_y());
}
// Wrap from right edge to left edge
else if (col >= MAZE_COLS)
{
const double new_x = (row >= 0 && row < MAZE_ROWS && maze.is_empty(row, 0)) ? Maze::get_cell_center_x(0) : Maze::get_cell_center_x(MAZE_COLS - 1);
set_position(new_x, get_y());
}
}
void Pacman::update_animation(double delta_time)
{
anim_timer_ += delta_time;
if (anim_timer_ > ANIMATION_DURATION)
{
anim_state_ = static_cast<AnimationState>((static_cast<int>(anim_state_) + 1) % 3);
anim_timer_ = 0.0;
}
}
std::tuple<int, int, bool, bool> Pacman::get_sprite_info() const
{
const direction_t current_dir = get_direction();
const int anim_frame = static_cast<int>(anim_state_);
// Sprite coordinates for different animation frames
// Frame 0 (open): col 3, Frame 1 (closing): col 4, Frame 2 (closed): col 5
const int sprite_col = 3 + anim_frame;
switch (current_dir)
{
case DIR_RIGHT:
return {sprite_col, 6, false, false};
case DIR_LEFT:
return {sprite_col, 6, true, false};
case DIR_DOWN:
// Special case: closed state uses row 6 instead of 7
return {sprite_col, (anim_state_ == AnimationState::CLOSED) ? 6 : 7, false, false};
case DIR_UP:
// Special case: closed state uses row 6 instead of 7, with flip_y
return {sprite_col, (anim_state_ == AnimationState::CLOSED) ? 6 : 7, false, true};
default: // DIR_NONE
return {5, 6, false, false}; // Default to closed mouth
}
}
double Pacman::get_current_speed() const
{
// 10% speed boost during power mode, multiplied by difficulty multiplier
double base_speed = MazeConfig::SPEED * speed_multiplier_;
return is_in_power_mode_ ? base_speed * 1.1 : base_speed;
}
void Pacman::play_dying_animation(const Maze *maze, const GameState *game_state, const Ghost *ghost1, const Ghost *ghost2)
{
// Dying animation sprite coordinates
const int dying_coords[12][2] = {
{3, 0}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}, {4, 0}, {4, 1}, {4, 2}, {4, 3}, {4, 4}, {4, 5}};
for (int i = 0; i < 12; ++i)
{
clear_screen(COLOR_BLACK);
// Draw complete game scene during animation
if (maze)
{
maze->draw();
}
if (game_state)
{
game_state->draw_tokens();
game_state->draw_power_pellets();
game_state->draw_score();
}
// Draw ghosts if provided
if (ghost1)
{
ghost1->draw();
}
if (ghost2)
{
ghost2->draw();
}
// Draw Pacman dying frame
if (sheet_)
{
sheet_->draw_sprite_at_pixel(get_palette(), dying_coords[i][0], dying_coords[i][1],
get_x(), get_y(), SPRITE_SCALE, false, false, true);
}
refresh_screen(60);
delay(80); // ~80ms per frame for smooth animation
}
}
// ============== Ghost Implementation ==============
Ghost::Ghost(double start_x, double start_y, SpriteSheet *sheet, const std::string &palette, GhostAIType ai_type)
: Entity(start_x, start_y, palette), sheet_(sheet), anim_state_(AnimationState::FRAME_1),
anim_timer_(0), target_x_(0), target_y_(0), escape_target_x_(0), escape_target_y_(0),
current_state_(GhostState::CHASING), scared_timer_(0.0), flash_timer_(0.0),
cooldown_timer_(0.0),
home_x_(Maze::get_cell_center_x(MazeConfig::MAZE_COLS / 2)),
home_y_(Maze::get_cell_center_y(MazeConfig::MAZE_ROWS / 2)),
ai_type_(ai_type), random_target_dir_(DIR_RIGHT), random_dir_timer_(0.0),
show_score_popup_(false), popup_timer_(0.0), popup_x_(0.0), popup_y_(0.0) {}
void Ghost::update(const Maze &maze, double delta_time)
{
// Base update for movement
Entity::update(maze, delta_time);
handle_tunnel_wrapping(maze);
update_animation(delta_time);
}
void Ghost::update(const Maze &maze, double pacman_x, double pacman_y, double delta_time)
{
// Call the overloaded version with DIR_NONE (no direction info)
update(maze, pacman_x, pacman_y, DIR_NONE, delta_time);
}
void Ghost::update(const Maze &maze, double pacman_x, double pacman_y, direction_t pacman_dir, double delta_time)
{
target_x_ = pacman_x;
target_y_ = pacman_y;
// Update scared timer if in scared mode
if (current_state_ == GhostState::SCARED)
{
scared_timer_ += delta_time; // Use actual delta time
if (scared_timer_ >= scared_duration_actual_)
{
current_state_ = GhostState::CHASING;
scared_timer_ = 0.0;
flash_timer_ = 0.0; // Reset flash timer
}
// Update flash timer for warning animation
flash_timer_ += delta_time;
}
// Choose movement based on current state and AI type
switch (current_state_)
{
case GhostState::CHASING:
{
// Calculate distance to Pacman (needed for AI and force movement)
double distance_to_pacman = sqrt(pow(target_x_ - get_x(), 2) + pow(target_y_ - get_y(), 2));
// Only recalculate direction at intersections or when blocked
if (should_recalculate_direction(maze))
{
// AI-specific behavior
if (ai_type_ == GhostAIType::RANDOM_PATROL)
{
// Random patrol: wander randomly until close enough, then lock on
if (distance_to_pacman < LOCK_ON_DISTANCE)
{
// Close enough - lock on and chase
choose_direction_towards_target(maze);
}
else
{
// Too far - wander randomly
choose_direction_random_patrol(maze);
}
}
else if (ai_type_ == GhostAIType::AMBUSHER)
{
// Ambusher: aim ahead of Pacman until close, then chase
if (distance_to_pacman < LOCK_ON_DISTANCE)
{
// Close enough - chase directly
choose_direction_towards_target(maze);
}
else
{
// Aim ahead of Pacman
choose_direction_ambush(maze, pacman_dir);
}
}
}
// Normal movement with collision detection
Entity::update(maze, delta_time);
// If ghost is very close to target and not moving, force movement
if (distance_to_pacman < 25.0 && get_direction() == DIR_NONE)
{
// Force the ghost to move directly towards Pacman
double dx = target_x_ - get_x();
double dy = target_y_ - get_y();
double movement = get_current_speed() * delta_time;
if (std::abs(dx) > std::abs(dy) && std::abs(dx) > 1.0)
{
set_position(get_x() + (dx > 0 ? movement : -movement), get_y());
}
else if (std::abs(dy) > 1.0)
{
set_position(get_x(), get_y() + (dy > 0 ? movement : -movement));
}
}
handle_tunnel_wrapping(maze);
break;
}
case GhostState::SCARED:
{
// Only recalculate direction at intersections or when blocked
if (should_recalculate_direction(maze))
{
// Calculate distance to Pacman for smart fleeing behavior
double distance_to_pacman = sqrt(pow(target_x_ - get_x(), 2) + pow(target_y_ - get_y(), 2));
if (distance_to_pacman < ESCAPE_DISTANCE)
{
// Close to Pacman - flee directly away
choose_direction_away_from_target(maze);
}
else
{
// Far from Pacman - move randomly
choose_direction_random_patrol(maze);
}
}
// Normal movement with collision detection
Entity::update(maze, delta_time);
handle_tunnel_wrapping(maze);
break;
}
case GhostState::CAUGHT:
move_towards_home(maze);
// No collision detection - caught ghosts can pass through walls
break;
case GhostState::COOLDOWN:
// Stay at home and wait for cooldown to complete
cooldown_timer_ += delta_time;
if (cooldown_timer_ >= COOLDOWN_DURATION)
{
set_chasing_mode();
}
break;
}
update_animation(delta_time);
}
void Ghost::draw() const
{
if (!sheet_)
return;
const auto [sprite_col, sprite_row, flip_x, flip_y] = get_sprite_info();
// Determine palette based on ghost state
std::string palette_to_use = get_palette();
if (current_state_ == GhostState::CAUGHT || current_state_ == GhostState::COOLDOWN)
{
// Use black/blue/white palette when caught, returning home, or cooling down
palette_to_use = "BLACK_BLUE_WHITE";
}
else if (current_state_ == GhostState::SCARED)
{
double time_remaining = SCARED_DURATION - scared_timer_;
// Flash when less than 3 seconds remaining
if (time_remaining <= WARNING_TIME)
{
// Flash with 50% duty cycle every 0.33 seconds (3 times per second)
// 0.167 seconds normal, 0.167 seconds flashing = 50% duty cycle
bool should_flash = fmod(flash_timer_, 0.33) >= 0.167;
if (should_flash)
{
palette_to_use = "RED_WHITE_GREEN";
}
}
}
sheet_->draw_sprite_at_pixel(palette_to_use, sprite_col, sprite_row,
get_x(), get_y(), SPRITE_SCALE, flip_x, flip_y, true);
// Draw score popup if showing
if (show_score_popup_)
{
// "400" sprite is at (5, 3)
sheet_->draw_sprite_at_pixel("WHITE_GREEN_RED", 5, 3, popup_x_, popup_y_);
}
}
void Ghost::update_score_popup(double delta_time)
{
if (show_score_popup_)
{
popup_timer_ += delta_time;
if (popup_timer_ >= POPUP_DURATION)
{
show_score_popup_ = false;
popup_timer_ = 0.0;
}
}
}
void Ghost::trigger_score_popup(double x, double y)
{
show_score_popup_ = true;
popup_timer_ = 0.0;
popup_x_ = x;
popup_y_ = y;
}
double Ghost::get_current_speed() const
{
// Caught ghosts move faster to return home quickly, multiplied by difficulty multiplier
if (current_state_ == GhostState::CAUGHT)
{
return MazeConfig::SPEED * speed_multiplier_ * 1.5; // 50% faster when caught
}
// Normal speed for chasing and scared states, with difficulty multiplier
return MazeConfig::SPEED * speed_multiplier_;
}
void Ghost::choose_direction_towards_target(const Maze &maze)
{
// Use non-portal distance calculation for pathfinding
const auto [dx, dy] = get_non_portal_distance(target_x_, target_y_);
const direction_t current_dir = get_direction();
const direction_t opposite_dir = get_opposite_direction(current_dir);
// List of potential directions to try, prioritized by distance to target
std::vector<std::pair<direction_t, double>> directions;
// Calculate how much each direction helps us get closer
if (dx > 0)
directions.push_back({DIR_RIGHT, std::abs(dx)});
if (dx < 0)
directions.push_back({DIR_LEFT, std::abs(dx)});
if (dy > 0)
directions.push_back({DIR_DOWN, std::abs(dy)});
if (dy < 0)
directions.push_back({DIR_UP, std::abs(dy)});
// Sort by priority (larger distance difference = higher priority)
std::sort(directions.begin(), directions.end(),
[](const auto &a, const auto &b)
{ return a.second > b.second; });
// Try directions in order of priority, but avoid going backward unless necessary
for (const auto &[dir, priority] : directions)
{
if (dir != opposite_dir && can_move_in_direction(maze, dir))
{
set_desired_direction(dir);
return;
}
}
// If no forward direction works, try any valid direction (including backward)
for (const auto &[dir, priority] : directions)
{
if (can_move_in_direction(maze, dir))
{
set_desired_direction(dir);
return;
}
}
// Last resort: try any direction that's not a wall
const std::vector<direction_t> all_dirs = {DIR_RIGHT, DIR_LEFT, DIR_DOWN, DIR_UP};
for (direction_t dir : all_dirs)
{
if (can_move_in_direction(maze, dir))
{
set_desired_direction(dir);
return;
}
}
}
void Ghost::choose_direction_random_patrol(const Maze &maze)
{
// Update random direction timer
random_dir_timer_ += 1.0 / 60.0; // Approximate delta time
const direction_t current_dir = get_direction();
const direction_t opposite_dir = get_opposite_direction(current_dir);
// Change direction periodically or if stuck
if (random_dir_timer_ >= RANDOM_DIR_CHANGE_TIME || current_dir == DIR_NONE || !can_move_in_direction(maze, current_dir))
{
random_dir_timer_ = 0.0;
// Try to find a new valid random direction (not backward)
std::vector<direction_t> valid_dirs;
const std::vector<direction_t> all_dirs = {DIR_RIGHT, DIR_LEFT, DIR_DOWN, DIR_UP};
for (direction_t dir : all_dirs)
{
if (dir != opposite_dir && can_move_in_direction(maze, dir))
{
valid_dirs.push_back(dir);
}
}
// If no forward directions available, allow backward
if (valid_dirs.empty())
{
for (direction_t dir : all_dirs)
{
if (can_move_in_direction(maze, dir))
{
valid_dirs.push_back(dir);
}
}
}
// Pick a random direction from valid options
if (!valid_dirs.empty())
{
int random_index = rand() % valid_dirs.size();
random_target_dir_ = valid_dirs[random_index];
}
}
// Set the current random direction
set_desired_direction(random_target_dir_);
}
void Ghost::choose_direction_ambush(const Maze &maze, direction_t pacman_dir)
{
// Calculate position ahead of Pacman based on their direction
double ambush_x = target_x_;
double ambush_y = target_y_;
// Project ahead by AMBUSH_DISTANCE pixels in Pacman's direction
switch (pacman_dir)
{
case DIR_RIGHT:
ambush_x += AMBUSH_DISTANCE;
break;
case DIR_LEFT:
ambush_x -= AMBUSH_DISTANCE;
break;
case DIR_DOWN:
ambush_y += AMBUSH_DISTANCE;
break;
case DIR_UP:
ambush_y -= AMBUSH_DISTANCE;
break;
case DIR_NONE:
// If Pacman isn't moving, just target their current position
break;
}
// Temporarily set target to ambush position
double original_x = target_x_;
double original_y = target_y_;
target_x_ = ambush_x;
target_y_ = ambush_y;
// Use standard pathfinding to reach ambush point
choose_direction_towards_target(maze);
// Restore original target
target_x_ = original_x;
target_y_ = original_y;
}
direction_t Ghost::get_opposite_direction(direction_t dir) const
{
switch (dir)
{
case DIR_LEFT:
return DIR_RIGHT;
case DIR_RIGHT:
return DIR_LEFT;
case DIR_UP:
return DIR_DOWN;
case DIR_DOWN:
return DIR_UP;
default:
return DIR_NONE;
}
}
bool Ghost::can_move_in_direction(const Maze &maze, direction_t dir) const
{
const int current_row = static_cast<int>(get_y() / CELL_SIZE);
const int current_col = static_cast<int>(get_x() / CELL_SIZE);
int next_row = current_row;
int next_col = current_col;
// Get next cell coordinates
switch (dir)
{
case DIR_LEFT:
next_col--;
break;
case DIR_RIGHT:
next_col++;
break;
case DIR_UP:
next_row--;
break;
case DIR_DOWN:
next_row++;
break;
default:
return false;
}
// Use maze's built-in collision detection that handles tunnels
return maze.is_empty_or_tunnel(next_row, next_col);
}
bool Ghost::is_at_intersection(const Maze &maze) const
{
// Check if ghost is approximately centered in a cell
const double cell_center_x = Maze::get_cell_center_x(static_cast<int>(get_x() / CELL_SIZE));
const double cell_center_y = Maze::get_cell_center_y(static_cast<int>(get_y() / CELL_SIZE));
const double dx = std::abs(get_x() - cell_center_x);
const double dy = std::abs(get_y() - cell_center_y);
// Only consider it at intersection if close to cell center
if (dx > 3.0 || dy > 3.0)
{
return false;
}
// Count how many directions are available (excluding the opposite of current direction)
const direction_t current = get_direction();
const direction_t opposite = get_opposite_direction(current);
int available_directions = 0;
const std::vector<direction_t> all_dirs = {DIR_UP, DIR_DOWN, DIR_LEFT, DIR_RIGHT};
for (direction_t dir : all_dirs)
{
if (dir != opposite && can_move_in_direction(maze, dir))
{
available_directions++;
}
}
// It's an intersection if there are 2+ directions available (not counting backward)
return available_directions >= 2;
}
bool Ghost::should_recalculate_direction(const Maze &maze) const
{
const direction_t current = get_direction();
// Always recalculate if not moving or can't continue in current direction
if (current == DIR_NONE || !can_move_in_direction(maze, current))
{
return true;
}
// Otherwise, only recalculate at intersections
return is_at_intersection(maze);
}
void Ghost::update_animation(double delta_time)
{
anim_timer_ += delta_time;
if (anim_timer_ > ANIMATION_DURATION)
{
anim_state_ = (anim_state_ == AnimationState::FRAME_1) ? AnimationState::FRAME_2 : AnimationState::FRAME_1;
anim_timer_ = 0.0;
}
}
void Ghost::handle_tunnel_wrapping(const Maze &maze)
{
const int row = static_cast<int>(floor(get_y() / CELL_SIZE));
const int col = static_cast<int>(floor(get_x() / CELL_SIZE));
// Wrap from left edge to right edge
if (col < 0)
{
const double new_x = (row >= 0 && row < MAZE_ROWS && maze.is_empty(row, MAZE_COLS - 1)) ? Maze::get_cell_center_x(MAZE_COLS - 1) : Maze::get_cell_center_x(0);
set_position(new_x, get_y());
}
// Wrap from right edge to left edge
else if (col >= MAZE_COLS)
{
const double new_x = (row >= 0 && row < MAZE_ROWS && maze.is_empty(row, 0)) ? Maze::get_cell_center_x(0) : Maze::get_cell_center_x(MAZE_COLS - 1);
set_position(new_x, get_y());
}
}
std::tuple<int, int, bool, bool> Ghost::get_sprite_info() const
{
const direction_t current_dir = get_direction();
const bool is_frame_2 = (anim_state_ == AnimationState::FRAME_2);
// If ghost is scared, use scared sprites regardless of direction
if (current_state_ == GhostState::SCARED)
{
return is_frame_2 ? std::make_tuple(GhostSprites::SCARED_2_COL, GhostSprites::SCARED_2_ROW, false, false) : std::make_tuple(GhostSprites::SCARED_1_COL, GhostSprites::SCARED_1_ROW, false, false);
}
// Normal directional sprites for chasing and caught states
switch (current_dir)
{
case DIR_RIGHT:
return is_frame_2 ? std::make_tuple(0, 1, false, false) : std::make_tuple(0, 0, false, false);
case DIR_LEFT:
return is_frame_2 ? std::make_tuple(0, 5, false, false) : std::make_tuple(0, 4, false, false);
case DIR_DOWN:
return is_frame_2 ? std::make_tuple(0, 3, false, false) : std::make_tuple(0, 2, false, false);
case DIR_UP:
return is_frame_2 ? std::make_tuple(0, 7, false, false) : std::make_tuple(0, 6, false, false);
default: // DIR_NONE - default to right direction
return std::make_tuple(0, 0, false, false);
}
}
void Ghost::set_scared_mode()
{
current_state_ = GhostState::SCARED;
scared_timer_ = 0.0;
flash_timer_ = 0.0; // Reset flash timer
// Set actual scared duration inversely to speed multiplier
scared_duration_actual_ = SCARED_DURATION / speed_multiplier_;
}
void Ghost::set_caught_mode()
{
current_state_ = GhostState::CAUGHT;
}
void Ghost::set_chasing_mode()
{
current_state_ = GhostState::CHASING;
scared_timer_ = 0.0; // Reset scared timer
flash_timer_ = 0.0; // Reset flash timer
cooldown_timer_ = 0.0; // Reset cooldown timer
}
bool Ghost::is_scared() const
{
return current_state_ == GhostState::SCARED;
}
bool Ghost::is_caught() const
{
return current_state_ == GhostState::CAUGHT;
}
bool Ghost::can_interact() const
{
// Ghost cannot interact during COOLDOWN (immune to collisions)
return current_state_ != GhostState::COOLDOWN;
}
GhostState Ghost::get_state() const
{
return current_state_;
}
void Ghost::choose_direction_away_from_target(const Maze &maze)
{
// Find the best escape position (furthest from Pac-Man)
find_escape_target(maze);
// Now use the same pathfinding logic as chasing, but towards the escape target
const double dx = escape_target_x_ - get_x();
const double dy = escape_target_y_ - get_y();
const direction_t current_dir = get_direction();
const direction_t opposite_dir = get_opposite_direction(current_dir);
// List of potential directions to try, prioritized by distance to escape target
std::vector<std::pair<direction_t, double>> directions;
// Calculate how much each direction helps us get TO the escape target
if (dx > 0) // Target is to the right
directions.push_back({DIR_RIGHT, std::abs(dx)});
if (dx < 0) // Target is to the left
directions.push_back({DIR_LEFT, std::abs(dx)});
if (dy > 0) // Target is below
directions.push_back({DIR_DOWN, std::abs(dy)});
if (dy < 0) // Target is above
directions.push_back({DIR_UP, std::abs(dy)});
// Sort by priority (larger distance difference = higher priority)
std::sort(directions.begin(), directions.end(),
[](const auto &a, const auto &b)
{ return a.second > b.second; });
// Try directions in order of priority, but avoid going backward unless necessary
for (const auto &[dir, priority] : directions)
{
if (dir != opposite_dir && can_move_in_direction(maze, dir))
{
set_desired_direction(dir);
return;
}
}
// If no forward direction works, try any valid direction (including backward)
for (const auto &[dir, priority] : directions)
{
if (can_move_in_direction(maze, dir))
{
set_desired_direction(dir);
return;
}
}
}
void Ghost::move_towards_home(const Maze &maze)
{
const double dx = home_x_ - get_x();
const double dy = home_y_ - get_y();
const double distance = sqrt(dx * dx + dy * dy);
// If we're very close to home, snap to center and enter cooldown mode
if (distance < 5.0)
{
set_position(home_x_, home_y_);
current_state_ = GhostState::COOLDOWN;
cooldown_timer_ = 0.0;
return;
}
// Move directly towards home (caught ghosts can move through walls)
const double speed = get_current_speed();
const double move_distance = speed / 60.0; // Assuming 60 FPS
// Normalize the direction vector
const double move_x = (dx / distance) * move_distance;
const double move_y = (dy / distance) * move_distance;
// Update position directly (no collision detection - ghosts can pass through walls when caught)
set_position(get_x() + move_x, get_y() + move_y);
// Set sprite direction based on movement direction for visual feedback
direction_t sprite_dir = DIR_NONE;
if (std::abs(dx) > std::abs(dy))
{
sprite_dir = dx > 0 ? DIR_RIGHT : DIR_LEFT;
}
else
{
sprite_dir = dy > 0 ? DIR_DOWN : DIR_UP;
}
set_desired_direction(sprite_dir);
dir_ = sprite_dir; // Set current direction immediately for sprite rendering
}
void Ghost::find_escape_target(const Maze &maze)
{
double max_distance = 0.0;
double best_x = get_x();
double best_y = get_y();
// Sample positions across the maze to find the furthest valid position
const int sample_step = 2; // Check every 2 cells for performance