forked from itsfuad/Computer-Graphics-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuad.h
More file actions
3490 lines (2725 loc) · 121 KB
/
Copy pathfuad.h
File metadata and controls
3490 lines (2725 loc) · 121 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
#pragma once
#include <GL/glut.h>
#include<GL/gl.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <memory>
#include <chrono>
#include <functional>
#include <random>
#include <string>
#include <unordered_map>
#include "audio.h"
#define M_PI 3.14159265358979323846
const int WINDOW_WIDTH = 1000;
const int WINDOW_HEIGHT = 600;
namespace Fuad {
const float ANIMATION_SPEED = 1.0f;
const float USER_CAR_SPEED_BASE = 3.0f * ANIMATION_SPEED;
const float USER_HUMAN_SPEED = 2.80f * ANIMATION_SPEED;
const int MIN_TIME_BETWEEN_SPAWNS_CAR = 30;
const int MAX_ACTIVE_HUMANS = 10;
const int MAX_ACTIVE_VEHICLES = 8;
const int MIN_TIME_BETWEEN_SPAWNS_HUMAN = 15;
const float USER_HUMAN_SIDEWALK_SPEED_FACTOR = 1.2f * ANIMATION_SPEED;
const int HUMAN_SPAWN_RATE_SIDEWALK = 120;
const int CAR_SPAWN_RATE = 100;
const float ROAD_Y_BOTTOM = 150.0f;
const float ROAD_Y_TOP = 250.0f;
const float SIDEWALK_TOP_Y_START = 250.0f;
const float SIDEWALK_TOP_Y_END = 280.0f;
const float SIDEWALK_BOTTOM_Y_START = 120.0f;
const float SIDEWALK_BOTTOM_Y_END = 150.0f;
const float HUMAN_CROSSING_X_START = 600.0f;
const float CAR_STOP_LINE_X = HUMAN_CROSSING_X_START - 20.0f;
const float HUMAN_CROSSING_WIDTH = 70.0f;
const float HUMAN_CROSSING_CENTER_X = HUMAN_CROSSING_X_START + HUMAN_CROSSING_WIDTH / 2.0f;
const float MIN_CAR_SPACING_AHEAD = 60.0f;
const float MIN_CAR_SPAWN_DISTANCE = 300.0f;
const float CAR_SAME_LANE_Y_THRESHOLD = 5.0f;
const float HUMAN_SAFETY_BUFFER = 3.0f;
const float DISTANCE_TO_STOP_FROM_SIGNAL = 20.0f;
const float CAR_SPAWN_OFFSCREEN_DISTANCE = 100.0f;
const float USER_DAY_NIGHT_CYCLE_SPEED = 0.0008f * ANIMATION_SPEED;
float currentTimeOfDay = 0.3f;
bool isNight = false;
unsigned char frameCount = 0;
bool IS_PAUSED = false;
bool DEBUG_ON = false;
bool MUSIC_ON = true;
bool showWarningMessage = false;
bool warningMessageActive = false; // Add flag to track if warning timer is active
int lastCarSpawnTime = 0;
int lastHumanSpawnTime = 0;
int warningMessageTimer = 0; // Add timer variable
const float CAR_PRIORITY_THRESHOLD = 350.0f;
const float VEHICLE_WHEEL_OFFSET = 5.0f;
struct Color {
float r, g, b;
};
enum class HairStyle {
Spiky,
FlatTop,
Bald
};
struct Rect {
float x, y, w, h;
};
struct Point2D {
float x, y;
};
struct SmokeParticle {
float x, y;
float size;
float alpha;
float x_drift;
};
std::vector<std::function<void()>> debugCalls;
AudioManager audioManager;
class Vehicle;
class Human;
class Building;
class Tree;
class StreetLamp;
class BlurrySkyline;
class Star;
class ShootingStar;
class Cloud;
class Drawable;
unsigned short HumansWaitingToCross();
unsigned short HumansCrossing();
int countCarsNearCrossing();
void showTransitionDelay(std::function<void()>, int);
std::vector<std::shared_ptr<Drawable>> drawableObjects;
std::vector<std::shared_ptr<Drawable>> backgroundObjects;
std::vector<std::shared_ptr<Vehicle>> vehicles;
std::vector<std::shared_ptr<Human>> activeHumans;
std::vector<std::shared_ptr<Star>> stars;
std::vector<std::shared_ptr<ShootingStar>> shootingStars;
void drawDebugOverlay() {
for (const auto& debugCall : debugCalls) {
debugCall();
}
debugCalls.clear();
}
void setObjectColor(float r, float g, float b, bool isLightSource = false) {
if (isLightSource) {
glColor3f(r, g, b);
return;
}
const float dayLightLevel = 1.0f;
const float nightLightLevel = 0.5f;
const float dawnStartTime = 0.18f;
const float dawnEndTime = 0.28f;
const float duskStartTime = 0.62f;
const float duskEndTime = 0.72f;
float currentLightLevel;
if (currentTimeOfDay >= duskStartTime && currentTimeOfDay < duskEndTime) {
float transitionProgress = (currentTimeOfDay - duskStartTime) / (duskEndTime - duskStartTime);
currentLightLevel = dayLightLevel - (dayLightLevel - nightLightLevel) * transitionProgress;
} else if (currentTimeOfDay >= duskEndTime || currentTimeOfDay < dawnStartTime) {
currentLightLevel = nightLightLevel;
} else if (currentTimeOfDay >= dawnStartTime && currentTimeOfDay < dawnEndTime) {
float transitionProgress = (currentTimeOfDay - dawnStartTime) / (dawnEndTime - dawnStartTime);
currentLightLevel = nightLightLevel + (dayLightLevel - nightLightLevel) * transitionProgress;
} else {
currentLightLevel = dayLightLevel;
}
glColor3f(
std::max(0.0f, r * currentLightLevel),
std::max(0.0f, g * currentLightLevel),
std::max(0.0f, b * currentLightLevel)
);
}
void setObjectColor(const Color& c, bool isLightSource = false) {
setObjectColor(c.r, c.g, c.b, isLightSource);
}
void drawText(float x, float y, const char* text, float scale = 0.7f) {
glColor3f(1.0f, 1.0f, 1.0f); // Set text color to white
glPushMatrix();
glTranslatef(x, y, 0.0f);
glScalef(scale, scale, 1.0f);
glRasterPos2f(0, 0);
for (const char* p = text; *p; p++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *p);
}
glPopMatrix();
}
void drawRect(float x, float y, float width, float height, float translateX = 0.0f, float translateY = 0.0f, int type = GL_QUADS) {
glBegin(type);
glVertex2f(x + translateX, y + translateY);
glVertex2f(x + width + translateX, y + translateY);
glVertex2f(x + width + translateX, y + height + translateY);
glVertex2f(x + translateX, y + height + translateY);
glEnd();
}
void drawBound(float x, float y, float width, float height, float translateX = 0.0f, float translateY = 0.0f) {
glLineWidth(2.0f);
drawRect(x, y, width, height, translateX, translateY, GL_LINE_LOOP);
glLineWidth(1.0f);
}
void drawLine(float x1, float y1, float x2, float y2, float thickness = 1.0f) {
glLineWidth(thickness);
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
}
void drawCircle(float cx, float cy, float r, int num_segments = 100) {
glBegin(GL_POLYGON);
for (int ii = 0; ii < num_segments; ii++) {
float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);
glVertex2f(cx + r * cosf(theta), cy + r * sinf(theta));
}
glEnd();
}
void drawTriangle(float x1, float y1, float x2, float y2, float x3, float y3) {
glBegin(GL_TRIANGLES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glEnd();
}
bool checkAABBCollision(Rect r1, Rect r2) {
return (r1.x < r2.x + r2.w && r1.x + r1.w > r2.x && r1.y < r2.y + r2.h && r1.y + r1.h > r2.y);
}
int timeNow() {
using namespace std::chrono;
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}
void drawRadialGradientCircle(float cx, float cy, float radius, float r, float g, float b) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_TRIANGLE_FAN);
// Center: full color and full alpha
glColor4f(r, g, b, 0.6f);
glVertex2f(cx, cy);
// Outer ring: same color, 0 alpha
glColor4f(r, g, b, 0.0f);
for (int i = 0; i <= 64; ++i) {
float angle = 2.0f * M_PI * i / 64;
float x = cx + cos(angle) * radius;
float y = cy + sin(angle) * radius;
glVertex2f(x, y);
}
glEnd();
glDisable(GL_BLEND);
}
void drawLinearGradientBeam(float cx, float topY, float bottomY, float topWidth, float bottomWidth, float r, float g, float b) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
// Top edge (bright)
glColor4f(r, g, b, 0.3f);
glVertex2f(cx - topWidth / 2, topY);
glVertex2f(cx + topWidth / 2, topY);
// Bottom edge (faded)
glColor4f(r, g, b, 0.0f);
glVertex2f(cx + bottomWidth / 2, bottomY);
glVertex2f(cx - bottomWidth / 2, bottomY);
glEnd();
glDisable(GL_BLEND);
}
class Drawable {
public:
float x, y, width, height;
Drawable(float _x, float _y, float _w = 0, float _h = 0) : x(_x), y(_y), width(_w), height(_h) {}
Rect getBounds() {
return {x, y, width, height};
}
virtual void draw() = 0;
virtual void update() = 0;
virtual ~Drawable() = default;
};
class Star : public Drawable {
public:
float size;
float baseBrightness;
float blinkPhase;
Star(float _x, float _y, float _size) :
Drawable(_x, _y),
size(_size),
baseBrightness(0.7f + (rand() % 30) / 100.0f),
blinkPhase(rand() % 628)
{}
void update() override {
}
void draw() override {
float starAlpha = 0.0f;
if (currentTimeOfDay >= 0.85f) {
starAlpha = 1.0f;
} else if (currentTimeOfDay >= 0.80f) {
starAlpha = (currentTimeOfDay - 0.80f) / 0.05f;
} else if (currentTimeOfDay <= 0.15f) {
starAlpha = 1.0f;
} else if (currentTimeOfDay <= 0.20f) {
starAlpha = (0.20f - currentTimeOfDay) / 0.05f;
}
if (starAlpha > 0.0f) {
float blinkFactor = 0.5f + 0.5f * sin(blinkPhase);
float finalBrightness = baseBrightness * blinkFactor;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1.0f, 1.0f, 0.9f, starAlpha * finalBrightness);
glPointSize(size);
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
glDisable(GL_BLEND);
}
}
};
class Cloud : public Drawable {
public:
float speed;
float scale;
Cloud(float _x, float _y, float _scale): Drawable(_x, _y), scale(_scale) {
speed = 0.2f + (rand() % 30) / 100.0f;
}
void draw() override {
glColor3f(0.9f, 0.9f, 0.95f);
float r = 15.0f * scale;
drawCircle(x, y, r);
drawCircle(x + r * 0.8f, y, r * 0.9f);
drawCircle(x - r * 0.8f, y, r * 0.9f);
drawCircle(x + r * 0.4f, y + r * 0.3f, r * 0.8f);
drawCircle(x - r * 0.4f, y + r * 0.3f, r * 0.8f);
}
void update() override {
if (!IS_PAUSED) {
x += speed;
if (x > WINDOW_WIDTH + 100.0f) {
x = -100.0f;
y = WINDOW_HEIGHT * (0.8f + (rand() % 20) / 100.0f);
}
}
}
};
class ShootingStar : public Drawable {
public:
float speed;
float angle;
float trailLength;
float alpha;
bool active;
ShootingStar() : Drawable(0, 0), speed(0), angle(0), trailLength(0), alpha(0), active(false) {}
void spawn() {
// Spawn from top of screen
x = rand() % WINDOW_WIDTH;
y = WINDOW_HEIGHT + 10.0f; // Reduced from +50 to +10
// Random angle (downward, slightly diagonal)
angle = (3 * M_PI / 2) + ((rand() % 60) - 30) * M_PI / 180.0f; // Changed from M_PI/2 to 3*M_PI/2
speed = 2.0f + (rand() % 3); // Reduced speed from 3-6 to 2-4
trailLength = 30.0f + (rand() % 40); // Trail length between 30-70
alpha = 1.0f;
active = true;
}
void update() override {
if (IS_PAUSED) return;
if (!active || !isNight) return;
// Move shooting star
x += cos(angle) * speed;
y += sin(angle) * speed;
// Fade out as it moves
alpha -= 0.02f;
// Deactivate if off screen or faded out
if (x < -100 || x > WINDOW_WIDTH + 100 || y < -100 || alpha <= 0) {
active = false;
}
}
void draw() override {
if (!active || !isNight) return;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Draw trail
glColor4f(1.0f, 1.0f, 1.0f, alpha * 0.3f);
glLineWidth(2.0f);
glBegin(GL_LINES);
float trailX = x - cos(angle) * trailLength;
float trailY = y - sin(angle) * trailLength;
glVertex2f(trailX, trailY);
glVertex2f(x, y);
glEnd();
// Draw bright head
glColor4f(1.0f, 1.0f, 1.0f, alpha);
glPointSize(3.0f);
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
glDisable(GL_BLEND);
}
};
class TrafficSignal : public Drawable {
public:
enum class TrafficLightState { RED, GREEN };
static TrafficLightState lightState;
static bool yellowLightOn;
const int YELLOW_BLINK_INTERVAL = 20;
TrafficSignal(float x, float y) : Drawable(x, y, 20, 60) {
static bool initialized = false;
if (!initialized) {
lightState = TrafficLightState::GREEN;
yellowLightOn = false;
initialized = true;
}
}
static void showGreenLight() {
lightState = TrafficLightState::GREEN;
}
static void showRedLight() {
lightState = TrafficLightState::RED;
}
void update() override {
if (IS_PAUSED) return;
if (yellowLightOn) return;
int numOfHumanWaiting = HumansWaitingToCross();
int numOfHumansCurrentlyCrossing = HumansCrossing();
int carsNearCrossing = countCarsNearCrossing();
bool isWaiting = numOfHumanWaiting > 0;
bool isCrossing = numOfHumansCurrentlyCrossing > 0;
bool prioritizeHumans = numOfHumanWaiting > carsNearCrossing;
if (numOfHumanWaiting == carsNearCrossing) prioritizeHumans = false;
if (lightState == TrafficLightState::GREEN && isWaiting && prioritizeHumans) {
yellowLightOn = true;
showTransitionDelay([&]() { showRedLight(); }, 2000);
}
else if (lightState == TrafficLightState::RED && !isCrossing && !prioritizeHumans) {
yellowLightOn = true;
showTransitionDelay([&]() { showGreenLight(); }, 2000);
}
}
void draw() override {
const float height = 60.0f;
const float width = 20.0f;
const float lightRadius = 8.0f;
float spacing = height / 3.0f;
int translateY = 60;
int translateX = -(width / 4);
const float gap = 18;
for (int i = 0; i < 3; i++) {
float centerY = y + height - (i + 0.5f) * spacing;
bool drawGlow = true;
Color lightColor;
if(i == 0 && lightState == TrafficLightState::RED && !yellowLightOn)
lightColor = {1.0f, 0.0f, 0.0f};
else if(i == 1 && yellowLightOn)
lightColor = {1.0f, 1.0f, 0.0f};
else if(i == 2 && lightState == TrafficLightState::GREEN && !yellowLightOn)
lightColor = {0.0f, 1.0f, 0.0f};
else {
lightColor = {0.3f, 0.3f, 0.3f};
drawGlow = false;
}
setObjectColor(lightColor, true);
drawCircle(x + translateX + gap, centerY + translateY - lightRadius, lightRadius);
drawCircle(x + translateX + width - gap, centerY + translateY - lightRadius, lightRadius);
if (drawGlow) {
// drawCircle(x + translateX + gap, centerY + translateY - lightRadius, lightRadius * 1.8f);
// drawCircle(x + translateX + width - gap, centerY + translateY - lightRadius, lightRadius * 1.8f);
// use gradient circle for glow effect
drawRadialGradientCircle(x + translateX + gap, centerY + translateY - lightRadius, lightRadius * 1.8f, lightColor.r, lightColor.g, lightColor.b);
drawRadialGradientCircle(x + translateX + width - gap, centerY + translateY - lightRadius, lightRadius * 1.8f, lightColor.r, lightColor.g, lightColor.b);
}
setObjectColor(0.0f, 0.0f, 0.0f);
drawTriangle(x + translateX, centerY + translateY, x + translateX - 10, centerY + translateY, x + translateX, centerY - 10 + translateY);
drawTriangle(x + translateX + width, centerY + translateY, x + translateX + width + 10, centerY + translateY, x + translateX + width, centerY - 10 + translateY);
}
setObjectColor(0.0f, 0.0f, 0.0f);
drawRect(x + translateX, y + translateY - 8, width, height);
drawRect(x, y, 10, 100);
drawHumanSign();
}
private:
void drawHumanShape(float x, float y, float scale, int walkState) {
glPushMatrix();
glTranslatef(x, y, 0.0f);
glScalef(scale, scale, 1.0f);
if (walkState == 1) {
glColor3f(0.0f, 0.9f, 0.0f);
drawLine(-7, -4, -3, 4, 4.0f);
drawLine(6, 0, 3, 4, 4.0f);
drawLine(6, -4, 6, 0, 4.0f);
drawLine(-5, -10, -1, -2, 4.0f);
drawLine(5, -10, 1, -2, 4.0f);
} else {
glColor3f(0.9f, 0.0f, 0.0f);
drawLine(-3, -4, -3, 4, 4.0f);
drawLine(3.2, -4, 3.2, 4, 4.0f);
drawLine(-1, -10, -1, -2, 4.0f);
drawLine(1.2, -10, 1.2, -2, 4.0f);
}
drawCircle(0, 7.5, 3);
drawRect(0.0f, 0.0f, 4.0f, 8.0f, -4.0f / 2.0f, -8.0f / 2.0f);
glPopMatrix();
}
void drawHumanSign() {
if (yellowLightOn) {
if ((frameCount / YELLOW_BLINK_INTERVAL) % 2 == 0) {
audioManager.playSound("beep", true);
glColor3f(0.9f, 0.0f, 0.0f);
drawHumanShape(x + 5, y + 85, 0.8, 0);
}
} else {
if (audioManager.isPlaying("beep")) {
audioManager.stopSound("beep");
}
if (lightState == TrafficLightState::RED) {
glColor3f(0.0f, 0.9f, 0.0f);
} else {
glColor3f(0.9f, 0.0f, 0.0f);
}
drawHumanShape(x + 5, y + 85, 0.8, lightState == TrafficLightState::RED ? 1 : 0);
}
}
};
TrafficSignal::TrafficLightState TrafficSignal::lightState = TrafficSignal::TrafficLightState::GREEN;
bool TrafficSignal::yellowLightOn = false;
class Human : public Drawable {
public:
enum class HumanState {
WALKING_ON_SIDEWALK,
WAITING_AT_CROSSING_EDGE,
CROSSING_ROAD,
REACHED_OTHER_SIDEWALK,
WALKING_AWAY_ON_SIDEWALK,
DESPAWNED
};
HumanState state;
float targetX;
float currentSidewalkY;
bool onBottomSidewalkInitially;
bool willCrossRoad;
bool cameFromLeft; // Track original spawn direction
float speedFactor;
float speed;
Color shirtColor;
Color pantsColor;
Color hairColor;
Color skinColor;
HairStyle hairStyle;
float scale;
float walkCycle;
bool isWalking;
int direction;
float SYNC_CONST;
Human() : Drawable(-1000.0f, -1000.0f),
state(HumanState::DESPAWNED),
targetX(0.0f), currentSidewalkY(0.0f),
onBottomSidewalkInitially(false), willCrossRoad(false),
speedFactor(1.0f), speed(0.0f),
shirtColor({0,0,0}), pantsColor({0,0,0}), hairColor({0,0,0}), skinColor({0,0,0}),
hairStyle(HairStyle::Bald),
scale(1.0f), walkCycle(0.0f), isWalking(false), direction(0) {
SYNC_CONST = 0.15f;
}
Human(float startX, float startY, float visualScale, const Color& shirt, const Color& pants, const Color& skin, const Color& hair, HairStyle style)
: Drawable(startX, startY, 0.2f, 0.4f),
state(HumanState::WALKING_ON_SIDEWALK),
targetX(0.0f),
currentSidewalkY(0.0f),
onBottomSidewalkInitially(false),
willCrossRoad(false),
speedFactor(1.0f + (rand() % 41) / 100.0f),
speed(0.7f * speedFactor),
shirtColor(shirt),
pantsColor(pants),
hairColor(hair),
skinColor(skin),
hairStyle(style),
scale(visualScale),
walkCycle(0.0f),
isWalking(false),
direction(0) {
SYNC_CONST = 0.15f;
}
void reset() {
state = HumanState::DESPAWNED;
x = -1000.0f;
stopWalking();
}
void initialize(float startX, float startY, float visualScale, const Color& shirt, const Color& pants, const Color& skin, const Color& hair, HairStyle style, bool bottomSidewalk, bool crossRoad) {
x = startX;
y = startY;
scale = visualScale;
shirtColor = shirt;
pantsColor = pants;
skinColor = skin;
hairColor = hair;
hairStyle = style;
onBottomSidewalkInitially = bottomSidewalk;
willCrossRoad = crossRoad;
cameFromLeft = (startX < WINDOW_WIDTH / 2); // Set original spawn direction
speedFactor = 1.0f + (rand() % 41) / 100.0f;
speed = 0.7f * speedFactor;
walkCycle = 0.0f;
isWalking = false;
direction = 0;
state = HumanState::WALKING_ON_SIDEWALK;
currentSidewalkY = startY;
// Set target based on crossing state
if (willCrossRoad) {
targetX = HUMAN_CROSSING_CENTER_X + (rand() % (int)(HUMAN_CROSSING_WIDTH / 2) - (int)(HUMAN_CROSSING_WIDTH / 4));
} else {
targetX = cameFromLeft ? WINDOW_WIDTH + 50.0f : -50.0f;
}
}
void startWalking(int dir) {
isWalking = true;
direction = dir;
}
void stopWalking() {
isWalking = false;
walkCycle = 0.0f;
}
void update() {
if (IS_PAUSED) return;
bool isCurrentlyMoving = false;
float effectiveSpeed = speed * USER_HUMAN_SIDEWALK_SPEED_FACTOR;
float walkCycleSpeed = 0.2f;
switch (state) {
case HumanState::WALKING_ON_SIDEWALK:
isCurrentlyMoving = true;
y = currentSidewalkY;
walkCycleSpeed = effectiveSpeed * SYNC_CONST;
// Removed dynamic targetX update here
if (fabs(x - targetX) < effectiveSpeed * 1.5f) {
x = targetX;
if (willCrossRoad) {
state = HumanState::WAITING_AT_CROSSING_EDGE;
} else {
state = HumanState::DESPAWNED;
reset();
}
} else if (x < targetX) {
x += effectiveSpeed;
startWalking(0);
} else {
x -= effectiveSpeed;
startWalking(1);
}
break;
case HumanState::WAITING_AT_CROSSING_EDGE:
stopWalking();
if (!willCrossRoad) {
// Changed mind, go back to walking away
// Determine which edge to go to based on current position
if (x > WINDOW_WIDTH / 2) {
targetX = WINDOW_WIDTH + 50.0f; // Go to right edge
} else {
targetX = -50.0f; // Go to left edge
}
state = HumanState::WALKING_ON_SIDEWALK;
} else if (TrafficSignal::lightState == TrafficSignal::TrafficLightState::RED && !TrafficSignal::yellowLightOn) {
state = HumanState::CROSSING_ROAD;
}
break;
case HumanState::CROSSING_ROAD:
isCurrentlyMoving = true;
effectiveSpeed *= 0.8f;
walkCycleSpeed = effectiveSpeed * SYNC_CONST;
if (onBottomSidewalkInitially) {
y += effectiveSpeed;
startWalking(2);
if (y >= (SIDEWALK_TOP_Y_START + SIDEWALK_TOP_Y_END) / 2.0f) {
y = (SIDEWALK_TOP_Y_START + SIDEWALK_TOP_Y_END) / 2.0f;
currentSidewalkY = y;
state = HumanState::REACHED_OTHER_SIDEWALK;
}
} else {
y -= effectiveSpeed;
startWalking(3);
if (y <= (SIDEWALK_BOTTOM_Y_START + SIDEWALK_BOTTOM_Y_END) / 2.0f) {
y = (SIDEWALK_BOTTOM_Y_START + SIDEWALK_BOTTOM_Y_END) / 2.0f;
currentSidewalkY = y;
state = HumanState::REACHED_OTHER_SIDEWALK;
}
}
break;
case HumanState::REACHED_OTHER_SIDEWALK:
stopWalking();
targetX = (x < WINDOW_WIDTH / 2) ? -50.0f : WINDOW_WIDTH + 50.0f;
state = HumanState::WALKING_AWAY_ON_SIDEWALK;
break;
case HumanState::WALKING_AWAY_ON_SIDEWALK:
isCurrentlyMoving = true;
y = currentSidewalkY;
effectiveSpeed *= USER_HUMAN_SIDEWALK_SPEED_FACTOR;
walkCycleSpeed = effectiveSpeed * SYNC_CONST;
if ((targetX < 0 && x < 0) || (targetX > WINDOW_WIDTH && x > WINDOW_WIDTH)) {
state = HumanState::DESPAWNED;
reset();
} else if (x < targetX) {
x += effectiveSpeed;
startWalking(0);
} else {
x -= effectiveSpeed;
startWalking(1);
}
break;
case HumanState::DESPAWNED:
stopWalking();
break;
}
if (isCurrentlyMoving) {
walkCycle += walkCycleSpeed;
if (walkCycle >= 2.0f * M_PI) {
walkCycle = 0.0f;
}
} else {
stopWalking();
}
}
void draw() {
if (state == HumanState::DESPAWNED) return;
glPushMatrix();
const float model_feet_to_center_offset = 0.19f;
glTranslatef(x, y + model_feet_to_center_offset * scale, 0.0f);
glScalef(scale, scale, 1.0f);
if (direction == 0) {
glScalef(-1.0f, 1.0f, 1.0f);
}
float armAngle = 0.0f, legAngle = 0.0f, legLift = 0.0f;
if (isWalking) {
if (direction == 0 || direction == 1) {
legAngle = sin(walkCycle) * 25.0f;
armAngle = -sin(walkCycle) * 20.0f;
} else {
armAngle = sin(walkCycle) * 15.0f;
legLift = std::max(0.0f, (float)sin(walkCycle * 2.0f)) * 0.03f;
}
}
const float torsoTop = 0.1f, torsoBottom = -0.05f, torsoWidth = 0.08f;
const float headRadius = 0.04f, neckY = torsoTop, headY = neckY + headRadius;
const float shoulderY = torsoTop - 0.02f, shoulderX = torsoWidth / 2;
const float hipY = torsoBottom, hipX = torsoWidth / 4;
setObjectColor(skinColor.r, skinColor.g, skinColor.b);
glPushMatrix();
glTranslatef(shoulderX, shoulderY, 0.0f);
glRotatef(-armAngle, 0, 0, 1);
drawLine(0, 0, 0, -0.08f, 5.0f);
glTranslatef(0, -0.08f, 0);
glRotatef(-30.0f, 0, 0, 1);
drawLine(0, 0, 0, -0.07f, 5.0f);
drawCircle(0, -0.075f, 0.015f);
glPopMatrix();
glPushMatrix();
setObjectColor(pantsColor.r, pantsColor.g, pantsColor.b);
glPushMatrix();
glTranslatef(hipX + 0.01f, hipY + 0.02f, 0.0f);
if(direction == 0 || direction == 1) glRotatef(-legAngle, 0, 0, 1);
else glTranslatef(0.0f, -legLift, 0.0f);
drawLine(0, 0, 0, -0.13f, 5.0f);
glTranslatef(0, -0.13f, 0);
glRotatef(std::max(0.0f, -legAngle * 0.5f), 0, 0, 1);
drawLine(0, 0, 0, -0.06f, 5.0f);
setObjectColor(0.1f, 0.1f, 0.1f);
drawRect(-0.01f, -0.06f, 0.04f, 0.02f, -0.04f / 2.0f, -0.02f / 2.0f);
glPopMatrix();
glPushMatrix();
setObjectColor(pantsColor.r, pantsColor.g, pantsColor.b);
glTranslatef(-hipX - 0.01f, hipY + 0.02f, 0.0f);
if(direction == 0 || direction == 1) glRotatef(legAngle, 0, 0, 1);
else glTranslatef(0.0f, legLift, 0.0f);
drawLine(0, 0, 0, -0.13f, 5.0f);
glTranslatef(0, -0.13f, 0);
glRotatef(std::max(0.0f, legAngle * 0.5f), 0, 0, 1);
drawLine(0, 0, 0, -0.06f, 5.0f);
setObjectColor(0.1f, 0.1f, 0.1f);
drawRect(-0.01f, -0.06f, 0.04f, 0.02f, -0.04f / 2.0f, -0.02f / 2.0f);
glPopMatrix();
glPopMatrix();
setObjectColor(shirtColor.r, shirtColor.g, shirtColor.b);
drawRect(0.0f, (torsoTop + torsoBottom) / 2, torsoWidth, torsoTop - torsoBottom, -torsoWidth / 2.0f, -(torsoTop - torsoBottom) / 2.0f);
setObjectColor(pantsColor.r, pantsColor.g, pantsColor.b);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0.0f, hipY + 0.01f);
for(int i = 0; i <= 180; i += 10) {
float angle = i * M_PI / 180.0f;
float x = ((torsoWidth + 0.005)/2 - 0.002) * cos(angle);
float y = hipY + 0.01f - ((torsoWidth + 0.005)/2 - 0.002) * sin(angle);
glVertex2f(x, y);
}
glEnd();
setObjectColor(skinColor.r, skinColor.g, skinColor.b);
drawCircle(0.0f, headY, headRadius, 20);
setObjectColor(hairColor.r, hairColor.g, hairColor.b);
switch(hairStyle) {
case HairStyle::Spiky:
if (direction == 2) {
glBegin(GL_POLYGON);
glVertex2f(-headRadius * 0.9f, headY + headRadius * 0.3f); glVertex2f( headRadius * 0.9f, headY + headRadius * 0.3f);
glVertex2f( headRadius * 0.8f, headY + headRadius * 0.8f); glVertex2f(-headRadius * 0.8f, headY + headRadius * 0.8f);
glEnd();
for (int i = -1; i <= 1; ++i) {
float bx = i * 0.025f;
glBegin(GL_TRIANGLES);
glVertex2f(bx - 0.01f, headY + headRadius * 0.8f); glVertex2f(bx + 0.01f, headY + headRadius * 0.8f); glVertex2f(bx, headY + headRadius * 1.3f);
glEnd();
}
glBegin(GL_POLYGON);
glVertex2f(-headRadius * 0.9f, headY + headRadius * 0.4f); glVertex2f( headRadius * 0.9f, headY + headRadius * 0.4f);
glVertex2f( headRadius * 0.7f, headY + headRadius * -0.8f); glVertex2f(-headRadius * 0.7f, headY + headRadius * -0.8f);
glEnd();
} else if (direction == 0 || direction == 1) {
glBegin(GL_POLYGON);
glVertex2f(headRadius * 0.3f, headY + headRadius * 0.8f); glVertex2f(headRadius * 0.85f, headY + headRadius * 0.8f);
glVertex2f(headRadius * 1.0f, headY - headRadius * 0.3f); glVertex2f(headRadius * 0.8f, headY - headRadius * 0.3f);
glEnd();
for (int i = -1; i <= 1; ++i) {
float bx = i * 0.025f;
glBegin(GL_TRIANGLES);
glVertex2f(bx - 0.01f, headY + headRadius * 0.8f); glVertex2f(bx + 0.01f, headY + headRadius * 0.8f); glVertex2f(bx, headY + headRadius * 1.3f);
glEnd();
}
} else {
for (int i = -1; i <= 1; ++i) {
float bx = i * 0.025f;
glBegin(GL_TRIANGLES);
glVertex2f(bx - 0.01f, headY + headRadius * 0.8f); glVertex2f(bx + 0.01f, headY + headRadius * 0.8f); glVertex2f(bx, headY + headRadius * 1.3f);
glEnd();
glBegin(GL_QUADS);
glVertex2f(bx - 0.01f, headY + headRadius * 0.8f); glVertex2f(bx + 0.01f, headY + headRadius * 0.8f);
glVertex2f(bx + 0.01f, headY + headRadius * 0.6f); glVertex2f(bx - 0.01f, headY + headRadius * 0.6f);
glEnd();
}
}
break;
case HairStyle::FlatTop:
if(direction == 2){
drawRect(0.0f, headY + headRadius * 0.5f, headRadius * 2, headRadius, -(headRadius * 2) / 2.0f, -headRadius / 2.0f);
drawCircle(0.0f, headY, headRadius*0.95f, 20);
} else {
drawRect(0.0f, headY + headRadius * 0.8f, headRadius * 1.8f, headRadius * 0.5f, -(headRadius * 1.8f) / 2.0f, -(headRadius * 0.5f) / 2.0f);
glBegin(GL_QUADS);
glVertex2f(-headRadius * 0.9f, headY + headRadius * 0.55f); glVertex2f( headRadius * 0.9f, headY + headRadius * 0.55f);
glVertex2f( headRadius * 1.0f, headY + headRadius * 0.5f); glVertex2f(-headRadius * 1.0f, headY + headRadius * 0.5f);
glEnd();
if (direction == 0 || direction == 1) {
glBegin(GL_POLYGON);
glVertex2f(headRadius * 0.3f, headY + headRadius * 0.8f); glVertex2f(headRadius * 0.85f, headY + headRadius * 0.8f);
glVertex2f(headRadius * 1.0f, headY - headRadius * 0.3f); glVertex2f(headRadius * 0.8f, headY - headRadius * 0.3f);
glEnd();
}
}
break;
case HairStyle::Bald: break;
}
if (direction != 2) {