Repository navigation
Expand file tree
/
Copy pathgame.cpp
More file actions
1503 lines (1266 loc) · 53.4 KB
/
Copy pathgame.cpp
File metadata and controls
1503 lines (1266 loc) · 53.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
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
//============================================================================
// Name : RushHour.cpp
// Author : Dr. Sibt Ul Hussain (Refactored)
// Version :
// Copyright : (c) Reserved
// Description : Basic 2D game with improved OOP design
//============================================================================
#ifndef RushHour_CPP_
#define RushHour_CPP_
#include "util.h"
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
int MENU_STATE = 0;
const int TAXI_MODE = 1;
const int DELIVERY_MODE = 2;
const int GAME_OVER_STATE = 3;
const int LEADERBOARD_STATE = 4;
class Menu {
private:
int selectedOption;
int windowWidth;
int windowHeight;
float animTimer;
public:
Menu() : selectedOption(0), windowWidth(1280), windowHeight(960), animTimer(0) {}
void draw(int width, int height) {
windowWidth = width;
windowHeight = height;
animTimer += 0.1; // Animation timer
// Draw background
DrawRectangle(0, 0, width, height, colors[SLATE_GRAY]);
// Create a city skyline effect
for (int i = 0; i < 10; i++) {
int buildingWidth = width / 10;
int buildingHeight = 80 + (i % 4) * 30 + sin(animTimer + i) * 10;
DrawRectangle(i * buildingWidth, 100, buildingWidth - 10, buildingHeight, colors[DARK_GRAY]);
// Windows on buildings
for (int w = 0; w < 3; w++) {
for (int h = 0; h < 5; h++) {
DrawSquare(i * buildingWidth + 10 + w * 20, 120 + h * 30, 10,
((w + h + i) % 3 == 0) ? colors[YELLOW] : colors[BLACK]);
}
}
}
// Road
DrawRectangle(0, 20, width, 60, colors[BLACK]);
// Road lines
for (int i = 0; i < width; i += 80) {
DrawRectangle(i, 45, 40, 10, colors[YELLOW]);
}
// Moving car
int carPosX = 580;
float carSize = 40;
DrawRoundRect(carPosX, 30, carSize * 1.4, carSize * 0.6, colors[RED]);
DrawRectangle(carPosX + carSize * 0.1, 30 + carSize * 0.6, carSize * 0.8, carSize * 0.5, colors[DARK_RED]);
DrawCircle(carPosX + carSize * 0.3, 30 - carSize * 0.2, carSize * 0.2, colors[DARK_GRAY]);
DrawCircle(carPosX + carSize * 1.0, 30 - carSize * 0.2, carSize * 0.2, colors[DARK_GRAY]);
DrawString(width / 2 - 110, height - 200, "RUSH HOUR", colors[RED]);
// Option box with gradient effect
DrawRoundRect(width / 2 - 200, height - 505, 330, 250, colors[DARK_BLUE]);
DrawRoundRect(width / 2 - 195, height - 500, 320, 240, colors[BLUE]);
// Draw options with hover effect
const int optionCount = 3; // Total number of options
const char* options[] = {"Taxi Rush", "Delivery Rush", "Leaderboard"};
const int yPositions[] = {height - 320, height - 370, height - 420};
for (int i = 0; i < optionCount; i++) {
if (selectedOption == i) {
DrawRoundRect(width / 2 - 180, yPositions[i] - 10, 285, 40, colors[YELLOW]);
}
DrawString(width / 2 - 160, yPositions[i], options[i], selectedOption == i ? colors[RED] : colors[WHITE]);
}
}
void moveSelection(bool up) {
if (up && selectedOption > 0) {
selectedOption--;
} else if (!up && selectedOption < 2) {
selectedOption++;
}
}
int getSelection() {
return selectedOption;
}
void handleMouseClick(int x, int y, int height) {
// Calculate positions relative to menu
const int yPositions[] = {windowHeight - 320, windowHeight - 370, windowHeight - 420};
for (int i = 0; i < 3; i++) {
if (y >= yPositions[i] - 20 && y <= yPositions[i] + 20 &&
x >= windowWidth / 2 - 180 && x <= windowWidth / 2 + 180) {
selectedOption = i;
MENU_STATE = i + 1; // Set to corresponding mode
if (i == 2) MENU_STATE = LEADERBOARD_STATE; // Override for leaderboard
break;
}
}
}
};
// Buildings class (Base class for Map)
class Buildings {
protected:
// 32 x 24 grid
int buildings[32][24];
public:
Buildings() {
// Initialize buildings
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 24; j++) {
buildings[i][j] = 0;
}
}
}
bool isBuilding(int X, int Y) {
if (X >= 0 && X < 32 && Y >= 0 && Y < 24) {
return buildings[X][Y] == 1;
}
return false;
}
void BuildingGen() {
const int maxB = 300;
int buildingCount = 0;
// Generate initial buildings
for (int i = 2; i < 30 && buildingCount < maxB; ++i) {
for (int j = 2; j < 21 && buildingCount < maxB; ++j) {
int neighbors = 0;
if (i > 0 && buildings[i - 1][j]) neighbors++;
if (j > 0 && buildings[i][j - 1]) neighbors++;
if (i > 0 && j > 0 && buildings[i - 1][j - 1]) neighbors++;
int baseChance = rand() % 84;
if (baseChance < 10 + 15 * neighbors) {
buildings[i][j] = 1;
buildingCount++;
}
}
}
// Clear paths where needed
for (int i = 1; i < 31; ++i) {
for (int j = 1; j < 22; ++j) {
if (buildings[i][j] == 0) {
bool right = buildings[i + 1][j] == 1;
bool up = buildings[i][j + 1] == 1;
bool down = buildings[i][j - 1] == 1;
bool left = buildings[i - 1][j] == 1;
if (right && up && down) {
buildings[i][j - 1] = 0;
} else if (right && up && left) {
buildings[i][j + 1] = 0;
}
}
}
}
}
void drawBuildings(int cSize) {
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 24; j++) {
if (buildings[i][j] == 1) {
DrawSquare(i * cSize, j * cSize, cSize, colors[DARK_SLATE_GRAY]);
}
}
}
}
};
class FuelStation {
private:
int gridX, gridY;
int cellSize;
public:
FuelStation() : cellSize(0), gridX(-1), gridY(-1) {}
explicit FuelStation(int cSize) : cellSize(cSize), gridX(-1), gridY(-1) {}
void setPosition(int x, int y) {
gridX = x;
gridY = y;
}
void draw() {
if (gridX >= 0 && gridY >= 0) {
DrawSquare(gridX * cellSize, gridY * cellSize, cellSize, colors[RED]);
}
}
bool isAtStation(int carGridX, int carGridY) {
return (carGridX == gridX && carGridY == gridY);
}
int getX() const { return gridX; }
int getY() const { return gridY; }
};
class Garage {
private:
int gridX, gridY;
int cellSize;
public:
explicit Garage(int cSize) : cellSize(cSize), gridX(30), gridY(1) {}
void draw() {
int x = gridX * cellSize;
int y = gridY * cellSize;
// Draw garage base
DrawSquare(x, y, cellSize, colors[DARK_BLUE]);
// Draw garage door
DrawRectangle(x + cellSize * 0.15, y + cellSize * 0.1, cellSize * 0.7, cellSize * 0.6, colors[GRAY]);
// Draw roof (triangle)
DrawTriangle(x, y + cellSize,
x + cellSize, y + cellSize,
x + cellSize/2, y + cellSize * 1.5,
colors[RED]);
// Draw 'P' letter above
DrawString(x + cellSize * 0.4, y + cellSize * 0.8, "P", colors[WHITE]);
}
bool isAtGarage(int carGridX, int carGridY) {
return (carGridX == gridX && carGridY == gridY);
}
int getX() const { return gridX; }
int getY() const { return gridY; }
};
class Map : public Buildings {
private:
int width, height;
int cell;
FuelStation fuelStations[2];
public:
Map(int w, int h, int cSize) : width(w), height(h), cell(cSize) {
BuildingGen();
// Initialize fuel stations
fuelStations[0] = FuelStation(cell);
fuelStations[1] = FuelStation(cell);
// Generate fuel station positions
generateFuelStations();
}
void generateFuelStations() {
for (int i = 0; i < 2; i++) {
int maxAttempts = 50;
int attempt = 0;
bool validPosition = false;
while (!validPosition && attempt < maxAttempts) {
int gridX = rand() % 30 + 1;
int gridY = rand() % 21 + 1;
if (!isBuilding(gridX, gridY) &&
(i == 0 || (gridX != fuelStations[0].getX() || gridY != fuelStations[0].getY()))) {
fuelStations[i].setPosition(gridX, gridY);
validPosition = true;
}
attempt++;
}
// Fallback if no valid position found
if (!validPosition) {
for (int x = 1; x < 30; x++) {
for (int y = 1; y < 22; y++) {
if (!isBuilding(x, y) &&
(i == 0 || (x != fuelStations[0].getX() || y != fuelStations[0].getY()))) {
fuelStations[i].setPosition(x, y);
return;
}
}
}
}
}
}
void draw() {
// Draw background grid
for (int i = 0; i < width * cell; i += cell) {
for (int j = 0; j < (height - 2) * cell; j += cell) {
DrawSquare(i, j, cell, colors[WHITE]);
}
}
// Draw borders
DrawRectangle(0, (height - 2) * cell, width * cell, 2 * cell, colors[WHITE]);
DrawRectangle(0, 0, width * cell, cell, colors[WHITE]);
DrawRectangle(0, 0, cell, height * cell, colors[WHITE]);
DrawSquare((width - 1) * cell, 0, height * cell, colors[WHITE]);
drawBuildings(cell);
// Draw fuel stations
fuelStations[0].draw();
fuelStations[1].draw();
}
bool canMoveTo(int pixelX, int pixelY) {
int X = pixelX / cell;
int Y = pixelY / cell;
return !isBuilding(X, Y);
}
bool isAtFuelStation(int carGridX, int carGridY) {
return fuelStations[0].isAtStation(carGridX, carGridY) ||
fuelStations[1].isAtStation(carGridX, carGridY);
}
int getCellSize() const { return cell; }
int getWidth() const { return width; }
int getHeight() const { return height; }
};
class Car {
private:
int xPos, yPos;
int size;
int maxWidth, maxHeight;
int cellSize;
Map* gameMapPtr;
int fuel;
const int maxFuel = 100;
bool hasPassenger;
ColorNames carColor;
public:
Car(int x, int y, int s, int maxW, int maxH, int cSize, Map* mapPtr)
: xPos(x), yPos(y), size(s), maxWidth(maxW), maxHeight(maxH),
cellSize(cSize), gameMapPtr(mapPtr), fuel(maxFuel),
hasPassenger(false), carColor(BLUE) {}
void draw() {
float bodyHeight = size * 0.6;
float bodyLength = size * 1.4;
float bodyX = xPos - size * 0.2;
float bodyY = yPos;
DrawRoundRect(bodyX, bodyY, bodyLength, bodyHeight, colors[carColor]);
float cabinWidth = size * 0.8;
float cabinHeight = size * 0.5;
float cabinX = xPos + size * 0.1;
float cabinY = yPos + bodyHeight;
DrawRectangle(cabinX, cabinY, cabinWidth, cabinHeight, colors[RED]);
DrawCircle(bodyX + size * 0.3, bodyY - size * 0.2, size * 0.2, colors[DARK_GRAY]);
DrawCircle(bodyX + size * 1.0, bodyY - size * 0.2, size * 0.2, colors[DARK_GRAY]);
if (hasPassenger) {
DrawSquare(cabinX + cabinWidth / 2 - size * 0.15, cabinY + cabinHeight, size * 0.3, colors[YELLOW]);
}
}
// Combined movement function to reduce code duplication
void move(int dirX, int dirY) {
if (fuel <= 0) return; // Can't move if out of fuel
int moveDistance = size + 20;
int newX = xPos + dirX * moveDistance;
int newY = yPos + dirY * moveDistance;
// Check boundaries based on movement direction
bool validMove = false;
if (dirX < 0) { // Moving left
validMove = newX >= cellSize && gameMapPtr->canMoveTo(newX, yPos);
} else if (dirX > 0) { // Moving right
validMove = newX + size < maxWidth - cellSize && gameMapPtr->canMoveTo(newX, yPos);
} else if (dirY > 0) { // Moving up
validMove = newY + size < maxHeight - 2 * cellSize && gameMapPtr->canMoveTo(xPos, newY);
} else if (dirY < 0) { // Moving down
validMove = newY >= cellSize && gameMapPtr->canMoveTo(xPos, newY);
}
if (validMove) {
xPos = newX;
yPos = newY;
fuel = max(fuel - 1, 0);
}
}
void moveLeft() {
move(-1, 0);
}
void moveRight() {
move(1, 0);
}
void moveUp() {
move(0, 1);
}
void moveDown() {
move(0, -1);
}
void pickupPassenger() {
hasPassenger = true;
carColor = YELLOW;
}
void dropoffPassenger() {
hasPassenger = false;
carColor = BLUE;
}
bool tryRefuel(int& money) {
int gridX = xPos / cellSize;
int gridY = yPos / cellSize;
// Check if at fuel station and has enough money
if (gameMapPtr->isAtFuelStation(gridX, gridY) && money >= 50) {
money -= 50;
fuel = maxFuel;
return true;
}
return false;
}
int getX() const { return xPos; }
int getY() const { return yPos; }
int getFuel() const { return fuel; }
bool getHasPassenger() const { return hasPassenger; }
};
class Box {
private:
int xPos, yPos;
int destX, destY;
bool isVisible;
bool hasDestination;
int size;
Map* gameMapPtr;
int cellSize;
public:
Box(Map* mapPtr, int cSize)
: gameMapPtr(mapPtr), cellSize(cSize), size(cSize / 3),
isVisible(false), hasDestination(false),
xPos(0), yPos(0), destX(0), destY(0) {}
// Helper function to find valid position on map
bool findValidPosition(int& x, int& y, int origX = -1, int origY = -1) {
int maxAttempts = 50;
// First try random positions
for (int attempt = 0; attempt < maxAttempts; attempt++) {
int gridX = rand() % 30 + 1;
int gridY = rand() % 21 + 1;
int testX = gridX * cellSize;
int testY = gridY * cellSize;
if (gameMapPtr->canMoveTo(testX, testY) &&
(origX == -1 || testX != origX || testY != origY)) {
x = testX;
y = testY;
return true;
}
}
// Fallback: search systematically
for (int i = 1; i < 30; i++) {
for (int j = 1; j < 22; j++) {
int testX = i * cellSize;
int testY = j * cellSize;
if (gameMapPtr->canMoveTo(testX, testY) &&
(origX == -1 || testX != origX || testY != origY)) {
x = testX;
y = testY;
return true;
}
}
}
return false;
}
void spawn() {
if (findValidPosition(xPos, yPos)) {
isVisible = true;
}
}
void draw() {
if (hasDestination) {
DrawSquare(destX, destY, cellSize, colors[BROWN]);
}
if (isVisible) {
// Draw a package/box
float centerX = xPos + cellSize / 2;
float centerY = yPos + cellSize / 2;
float boxSize = size * 0.8;
DrawSquare(centerX - boxSize/2, centerY - boxSize/2, boxSize, colors[BROWN]);
// Add "tape" lines on the box
DrawLine(centerX - boxSize/2, centerY, centerX + boxSize/2, centerY, 2, colors[YELLOW]);
DrawLine(centerX, centerY - boxSize/2, centerX, centerY + boxSize/2, 2, colors[YELLOW]);
}
}
bool isPickedUp(int carX, int carY) {
if (!isVisible) return false;
int boxGridX = xPos / cellSize;
int boxGridY = yPos / cellSize;
int carGridX = carX / cellSize;
int carGridY = carY / cellSize;
return (boxGridX == carGridX && boxGridY == carGridY);
}
void pickup() {
isVisible = false;
generateDestination();
}
void generateDestination() {
if (findValidPosition(destX, destY, xPos, yPos)) {
hasDestination = true;
}
}
bool isAtDestination(int carX, int carY) {
if (!hasDestination) return false;
int destGridX = destX / cellSize;
int destGridY = destY / cellSize;
int carGridX = carX / cellSize;
int carGridY = carY / cellSize;
return (destGridX == carGridX && destGridY == carGridY);
}
void resetDestination() {
hasDestination = false;
}
bool getHasDestination() const { return hasDestination; }
bool getIsVisible() const { return isVisible; }
};
class NPCCar {
private:
int xPos, yPos;
int size;
int cellSize;
Map* gameMapPtr;
ColorNames carColor;
int moveDirection; // 0-3: Left, Right, Up, Down
int maxWidth, maxHeight; // Track grid boundaries
public:
NPCCar(Map* mapPtr, int cSize)
: size(15), cellSize(cSize), gameMapPtr(mapPtr), carColor(RED),
moveDirection(rand() % 4) {
// Set grid boundaries based on map size
maxWidth = mapPtr->getWidth() * cSize;
maxHeight = mapPtr->getHeight() * cSize;
// Initialize position randomly at a valid location
spawn();
}
void spawn() {
int maxAttempts = 50;
int attempt = 0;
bool validPosition = false;
while (!validPosition && attempt < maxAttempts) {
int gridX = rand() % 30 + 1;
int gridY = rand() % 21 + 1;
// Center in cell
xPos = gridX * cellSize + (cellSize - size) / 2;
yPos = gridY * cellSize + (cellSize - size) / 2;
if (gameMapPtr->canMoveTo(xPos, yPos)) {
validPosition = true;
}
attempt++;
}
if (!validPosition) {
// Default position centered in cell (3,3)
xPos = 3 * cellSize + cellSize / 2 - size / 2;
yPos = 3 * cellSize + cellSize / 2 - size / 2;
if (!gameMapPtr->canMoveTo(xPos, yPos)) {
for (int i = 1; i < 30; i++) {
for (int j = 1; j < 22; j++) {
if (gameMapPtr->canMoveTo(i * cellSize, j * cellSize)) {
// Center in this found valid cell
xPos = i * cellSize + cellSize / 2 - size / 2;
yPos = j * cellSize + cellSize / 2 - size / 2;
return;
}
}
}
}
}
}
void draw() {
// Center of the cell
float centerX = (xPos / cellSize) * cellSize + cellSize / 2;
float centerY = (yPos / cellSize) * cellSize + cellSize / 3;
// Car body
float bodyLength = size;
float bodyHeight = size * 0.6;
DrawRoundRect(centerX - bodyLength / 2, centerY - bodyHeight / 2, bodyLength, bodyHeight, colors[RED]);
// Cabin
float cabinWidth = size * 0.8;
float cabinHeight = size * 0.5;
DrawRectangle(centerX - cabinWidth / 2, centerY + bodyHeight / 2, cabinWidth, cabinHeight, colors[BLACK]);
// Wheels
float wheelRadius = size * 0.2;
DrawCircle(centerX - size * 0.3, centerY - bodyHeight / 2 - wheelRadius, wheelRadius, colors[DARK_GRAY]);
DrawCircle(centerX + size * 0.3, centerY - bodyHeight / 2 - wheelRadius, wheelRadius, colors[DARK_GRAY]);
}
void moveOnTick() {
// Sometimes change direction
if (rand() % 10 == 0) {
moveDirection = rand() % 4;
}
// Try to move in current direction
bool moved = false;
int originalDirection = moveDirection;
// Directions: 0=Left, 1=Right, 2=Up, 3=Down
const int dirX[] = {-1, 1, 0, 0};
const int dirY[] = {0, 0, 1, -1};
do {
int newX = xPos + dirX[moveDirection] * cellSize;
int newY = yPos + dirY[moveDirection] * cellSize;
// Check if the new position is within grid bounds and not on a building
if (newX >= cellSize && newX + size < maxWidth - cellSize &&
newY >= cellSize && newY + size < maxHeight - 2 * cellSize &&
gameMapPtr->canMoveTo(newX, newY)) {
xPos = newX;
yPos = newY;
moved = true;
} else {
// Try another direction
moveDirection = (moveDirection + 1) % 4;
}
} while (!moved && moveDirection != originalDirection);
}
bool collidesWith(int carX, int carY, int carSize) {
// Collision detection
int thisLeft = xPos - size * 0.2;
int thisRight = xPos + size * 1.2;
int thisBottom = yPos - size * 0.2;
int thisTop = yPos + size * 1.1;
int otherLeft = carX - carSize * 0.2;
int otherRight = carX + carSize * 1.2;
int otherBottom = carY - carSize * 0.2;
int otherTop = carY + carSize * 1.1;
return (thisRight > otherLeft && thisLeft < otherRight &&
thisTop > otherBottom && thisBottom < otherTop);
}
int getX() const { return xPos; }
int getY() const { return yPos; }
};
// Passenger class
class Passenger {
private:
int xPos, yPos;
int destX, destY;
bool isVisible;
bool hasDestination;
int size;
Map* gameMapPtr;
int cellSize;
public:
Passenger(Map* mapPtr, int cSize) {
cellSize = cSize;
gameMapPtr = mapPtr;
size = cellSize / 3;
isVisible = false;
hasDestination = false;
}
void spawn() {
int maxAttempts = 50;
int attempt = 0;
bool validPosition = false;
while (!validPosition && attempt < maxAttempts) {
// Generate random grid position
int gridX = rand() % 30 + 1;
int gridY = rand() % 21 + 1;
// Convert to pixel coordinates
int x = gridX * cellSize;
int y = gridY * cellSize;
// Check if position is valid
if (gameMapPtr->canMoveTo(x, y)) {
xPos = x;
yPos = y;
validPosition = true;
isVisible = true;
return;
}
attempt++;
}
// Fallback position logic
for (int i = 1; i < 30; i++) {
for (int j = 1; j < 22; j++) {
if (gameMapPtr->canMoveTo(i * cellSize, j * cellSize)) {
xPos = i * cellSize;
yPos = j * cellSize;
isVisible = true;
return;
}
}
}
}
void draw() {
if (hasDestination) {
DrawSquare(destX, destY, cellSize, colors[PINK]);
}
if (isVisible) {
float scale = 0.5;
float s = size * scale;
float centerX = xPos + cellSize / 2;
float centerY = yPos + cellSize / 2;
DrawCircle(centerX, centerY, s / 2, colors[BLACK]);
DrawLine(centerX, centerY - s / 2, centerX, centerY - s / 2 - s, 2, colors[BLACK]);
DrawLine(centerX, centerY - s / 2 - s * 0.5, centerX - s, centerY - s / 2 - s * 0.2, 2, colors[BLACK]);
DrawLine(centerX, centerY - s / 2 - s * 0.5, centerX + s, centerY - s / 2 - s * 0.2, 2, colors[BLACK]);
DrawLine(centerX, centerY - s / 2 - s, centerX - s, centerY - s / 2 - s * 2, 2, colors[BLACK]);
DrawLine(centerX, centerY - s / 2 - s, centerX + s, centerY - s / 2 - s * 2, 2, colors[BLACK]);
}
}
bool isPickedUp(int carX, int carY) {
if (!isVisible)
return false;
int passengerGridX = xPos / cellSize;
int passengerGridY = yPos / cellSize;
int carGridX = carX / cellSize;
int carGridY = carY / cellSize;
return (passengerGridX == carGridX && passengerGridY == carGridY);
}
void pickup() {
isVisible = false;
generateDestination();
}
void generateDestination() {
int maxAttempts = 50;
int attempt = 0;
bool validPosition = false;
while (!validPosition && attempt < maxAttempts) {
int gridX = rand() % 30 + 1;
int gridY = rand() % 21 + 1;
int x = gridX * cellSize;
int y = gridY * cellSize;
if (gameMapPtr->canMoveTo(x, y) && (x != xPos || y != yPos)) {
destX = x;
destY = y;
validPosition = true;
hasDestination = true;
return;
}
attempt++;
}
// Fallback logic
for (int i = 1; i < 30; i++) {
for (int j = 1; j < 22; j++) {
int x = i * cellSize;
int y = j * cellSize;
if (gameMapPtr->canMoveTo(x, y) && (x != xPos || y != yPos)) {
destX = x;
destY = y;
hasDestination = true;
return;
}
}
}
}
bool isAtDestination(int carX, int carY) {
if (!hasDestination)
return false;
int destGridX = destX / cellSize;
int destGridY = destY / cellSize;
int carGridX = carX / cellSize;
int carGridY = carY / cellSize;
return (destGridX == carGridX && destGridY == carGridY);
}
void resetDestination() {
hasDestination = false;
}
bool getHasDestination() const { return hasDestination; }
bool getIsVisible() const { return isVisible; }
};
class HighScore {
public:
char playerName[20]; // Fixed-size array for player name
int score;
int gameMode; // 1 for TAXI_MODE, 2 for DELIVERY_MODE
HighScore() : score(0), gameMode(0) {
playerName[0] = '\0'; // Initialize to empty string
}
HighScore(const string& name, int s, int mode) : score(s), gameMode(mode) {
strncpy(playerName, name.c_str(), sizeof(playerName) - 1);
playerName[sizeof(playerName) - 1] = '\0'; // Ensure null termination
}
};
class Leaderboard {
private:
static const int MAX_SCORES = 10;
HighScore scores[MAX_SCORES];
int numScores;
const string filename = "highscores.txt";
public:
Leaderboard() : numScores(0) {
loadFromFile();
}
void loadFromFile() {
ifstream file(filename, ios::binary);
if (!file) {
cout << "No existing highscores file found. Creating new leaderboard." << endl;
numScores = 0;
return;
}
// Get file size
file.seekg(0, ios::end);
streampos fileSize = file.tellg();
file.seekg(0, ios::beg);
// Calculate number of records
numScores = fileSize / sizeof(HighScore);
if (numScores > MAX_SCORES) {
numScores = MAX_SCORES;
}
// Read records
file.read(reinterpret_cast<char*>(scores), numScores * sizeof(HighScore));
file.close();
}
void saveToFile() {
ofstream file(filename, ios::binary | ios::trunc);
if (!file) {
cout << "Error: Could not open highscores file for writing." << endl;
return;
}
file.write(reinterpret_cast<const char*>(scores), numScores * sizeof(HighScore));
file.close();
}
bool isHighScore(int score) const {
// If we have fewer than MAX_SCORES, any score can be added
if (numScores < MAX_SCORES) {
return true;
}
// Otherwise, check if this score is higher than any existing score
for (int i = 0; i < numScores; i++) {
if (score > scores[i].score) {
return true;
}
}
return false;
}
void addScore(const string& playerName, int score, int gameMode) {
if (!isHighScore(score)) {
return;
}
// Create new high score
HighScore newScore(playerName, score, gameMode);
// If we're not at capacity yet, just add the new score
if (numScores < MAX_SCORES) {
scores[numScores++] = newScore;
} else {
// Find the lowest score and replace it
int lowestIdx = 0;
for (int i = 1; i < numScores; i++) {
if (scores[i].score < scores[lowestIdx].score) {
lowestIdx = i;
}
}
scores[lowestIdx] = newScore;
}
// Sort scores in descending order
sortScores();
// Save to file
saveToFile();
}
void sortScores() {
// Sort scores in descending order
for (int i = 0; i < numScores - 1; i++) {
for (int j = 0; j < numScores - i - 1; j++) {
if (scores[j].score < scores[j + 1].score) {
// Swap
HighScore temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
}
void draw(int width, int height) {
// Draw leaderboard background
DrawRectangle(0, 0, width, height, colors[SLATE_GRAY]);
// Draw title
DrawString(width / 2 - 150, height - 100, "RUSH HOUR LEADERBOARD", colors[YELLOW]);
// Draw column headers
DrawString(width / 4 - 150, height - 200, "RANK", colors[WHITE]);
DrawString(width / 4 - 50, height - 200, "PLAYER", colors[WHITE]);