-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTema2.cpp
More file actions
970 lines (807 loc) · 34.2 KB
/
Copy pathTema2.cpp
File metadata and controls
970 lines (807 loc) · 34.2 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
#include "lab_m1/Tema2/Tema2.h"
#include <vector>
#include <string>
#include <iostream>
#include <cmath>
#include <ctime>
#include <glm/gtc/type_ptr.hpp>
using namespace std;
using namespace m1;
Tema2::Tema2()
{
camera = nullptr;
gameOver = false;
textRenderer = nullptr;
}
Tema2::~Tema2()
{
if (camera != nullptr) {
delete camera;
camera = nullptr;
}
if (textRenderer != nullptr) {
delete textRenderer;
textRenderer = nullptr;
}
for (Rail* rail : allRails) {
delete rail;
}
allRails.clear();
}
void Tema2::Init()
{
camera = new implemented::Camera();
camera->Set(glm::vec3(0, 20, 25), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
projectionMatrix = glm::perspective(RADIANS(60.0f), window->props.aspectRatio, 0.01f, 200.0f);
// Initializam text renderer pentru UI (timer si mesaje)
glm::ivec2 resolution = window->GetResolution();
if (textRenderer == nullptr) {
textRenderer = new gfxc::TextRenderer(window->props.selfDir, resolution.x, resolution.y);
textRenderer->Load((window->props.selfDir + std::string("\\assets\\fonts\\Hack-Bold.ttf")).c_str(), 36);
}
CreateMeshes();
CreateRailNetwork();
// Cream cele 3 statii de resurse (pozitii fizice pentru colectare)
float y = 0.2f;
stations.push_back(StationResource(ResourceType::CYAN_BOX, glm::vec3(-16, y, 0)));
stations.push_back(StationResource(ResourceType::YELLOW_SPHERE, glm::vec3(-16, y, -16)));
stations.push_back(StationResource(ResourceType::RED_PYRAMID, glm::vec3(16, y, -8)));
// Generam comanda random de 5 resurse
srand(static_cast<unsigned int>(time(nullptr)));
currentOrder.GenerateRandom();
// Plasam trenul la pozitia de start: gara centrala (0, 8)
glm::vec3 startPos = glm::vec3(0, 0.2f, 8);
glm::vec3 targetPos = glm::vec3(0, 0.2f, 0);
for (Rail* rail : allRails) {
if (glm::length(rail->startPosition - startPos) < 0.01f &&
glm::length(rail->endPosition - targetPos) < 0.01f) {
train.currentRail = rail;
train.progress = 0.0f;
train.speed = 0.5f;
train.waitingAtJunction = true;
train.movingForward = true;
train.UpdatePosition();
break;
}
}
}
// Cream toate mesh-urile necesare pentru joc (sine, teren, statii, tren)
void Tema2::CreateMeshes()
{
// Mesh-uri pentru sine (negru, albastru, alb, galben)
AddMeshToList(CreateBox("box_black", glm::vec3(0.1f, 0.1f, 0.1f)));
AddMeshToList(CreateBox("box_blue", glm::vec3(0.2f, 0.4f, 0.8f)));
AddMeshToList(CreateBox("box_white", glm::vec3(0.9f, 0.9f, 0.9f)));
AddMeshToList(CreateBox("box_yellow", glm::vec3(1.0f, 0.9f, 0.0f)));
// Mesh-uri pentru teren (campie, apa, munti)
AddMeshToList(CreateBox("box_green", glm::vec3(0.3f, 0.8f, 0.3f)));
AddMeshToList(CreateBox("box_water", glm::vec3(0.2f, 0.5f, 0.9f)));
AddMeshToList(CreateBox("box_mountain", glm::vec3(0.5f, 0.4f, 0.3f)));
// Mesh-uri pentru statii (cub cyan, sfera galbena, piramida rosie)
AddMeshToList(CreateBox("station_box", glm::vec3(0.2f, 0.8f, 0.9f)));
AddMeshToList(CreateSphere("station_sphere", glm::vec3(0.9f, 0.9f, 0.2f), 20));
AddMeshToList(CreatePyramid("station_pyramid", glm::vec3(0.9f, 0.3f, 0.3f)));
// Mesh-uri pentru tren (motor, cos, roti, incarcatura)
AddMeshToList(CreateCylinder("cyl_blue", glm::vec3(0.2f, 0.4f, 0.8f), 32));
AddMeshToList(CreateCylinder("cyl_purple", glm::vec3(0.7f, 0.2f, 0.8f), 32));
AddMeshToList(CreateCylinder("wheel_red", glm::vec3(0.9f, 0.2f, 0.2f), 24));
AddMeshToList(CreateBox("cabin_orange", glm::vec3(1.0f, 0.5f, 0.0f)));
}
// Cream un cub 3D din 8 varfuri si 12 triunghiuri (6 fete x 2 triunghiuri)
Mesh* Tema2::CreateBox(const std::string& name, glm::vec3 color)
{
// Definim cele 8 varfuri (centrat la origine, dimensiune 1x1x1)
std::vector<VertexFormat> vertices = {
VertexFormat(glm::vec3(-0.5f, -0.5f, 0.5f), color),
VertexFormat(glm::vec3(0.5f, -0.5f, 0.5f), color),
VertexFormat(glm::vec3(0.5f, 0.5f, 0.5f), color),
VertexFormat(glm::vec3(-0.5f, 0.5f, 0.5f), color),
VertexFormat(glm::vec3(-0.5f, -0.5f, -0.5f), color),
VertexFormat(glm::vec3(0.5f, -0.5f, -0.5f), color),
VertexFormat(glm::vec3(0.5f, 0.5f, -0.5f), color),
VertexFormat(glm::vec3(-0.5f, 0.5f, -0.5f), color),
};
// Definim indicii pentru triunghiuri (36 indici = 6 fete x 6 indici/fata)
std::vector<unsigned int> indices = {
0, 1, 2, 2, 3, 0,
5, 4, 7, 7, 6, 5,
4, 0, 3, 3, 7, 4,
1, 5, 6, 6, 2, 1,
3, 2, 6, 6, 7, 3,
4, 5, 1, 1, 0, 4,
};
Mesh* mesh = new Mesh(name);
mesh->InitFromData(vertices, indices);
return mesh;
}
// Cream o sfera folosind coordonate sferice (phi = latitudine, theta = longitudine)
Mesh* Tema2::CreateSphere(const std::string& name, glm::vec3 color, int segments)
{
std::vector<VertexFormat> vertices;
std::vector<unsigned int> indices;
float radius = 0.5f;
int stacks = segments;
int slices = segments;
// Generam vertices: x = r*cos(theta)*sin(phi), y = r*cos(phi), z = r*sin(theta)*sin(phi)
for (int i = 0; i <= stacks; ++i) {
float V = (float)i / (float)stacks;
float phi = V * glm::pi<float>();
for (int j = 0; j <= slices; ++j) {
float U = (float)j / (float)slices;
float theta = U * 2.0f * glm::pi<float>();
float x = radius * cos(theta) * sin(phi);
float y = radius * cos(phi);
float z = radius * sin(theta) * sin(phi);
glm::vec3 position(x, y, z);
glm::vec3 normal = glm::normalize(position);
vertices.push_back(VertexFormat(position, color, normal));
}
}
// Conectam vertices-urile in triunghiuri
for (int i = 0; i < slices * stacks + slices; ++i) {
indices.push_back(i);
indices.push_back(i + slices + 1);
indices.push_back(i + slices);
indices.push_back(i + slices + 1);
indices.push_back(i);
indices.push_back(i + 1);
}
Mesh* mesh = new Mesh(name);
mesh->InitFromData(vertices, indices);
return mesh;
}
// Cream o piramida cu baza patrata (4 varfuri) si varf (1 varf) = 5 varfuri total
Mesh* Tema2::CreatePyramid(const std::string& name, glm::vec3 color)
{
std::vector<VertexFormat> vertices;
std::vector<unsigned int> indices;
float base = 1.0f;
float height = 1.5f;
// Cele 4 varfuri ale bazei + varful piramidei
vertices.push_back(VertexFormat(glm::vec3(-base / 2, 0, -base / 2), color, glm::vec3(0, -1, 0)));
vertices.push_back(VertexFormat(glm::vec3(base / 2, 0, -base / 2), color, glm::vec3(0, -1, 0)));
vertices.push_back(VertexFormat(glm::vec3(base / 2, 0, base / 2), color, glm::vec3(0, -1, 0)));
vertices.push_back(VertexFormat(glm::vec3(-base / 2, 0, base / 2), color, glm::vec3(0, -1, 0)));
vertices.push_back(VertexFormat(glm::vec3(0, height, 0), color, glm::vec3(0, 1, 0)));
// Baza (2 triunghiuri) + 4 fete laterale (4 triunghiuri)
indices.push_back(0); indices.push_back(2); indices.push_back(1);
indices.push_back(0); indices.push_back(3); indices.push_back(2);
indices.push_back(0); indices.push_back(1); indices.push_back(4);
indices.push_back(1); indices.push_back(2); indices.push_back(4);
indices.push_back(2); indices.push_back(3); indices.push_back(4);
indices.push_back(3); indices.push_back(0); indices.push_back(4);
Mesh* mesh = new Mesh(name);
mesh->InitFromData(vertices, indices);
return mesh;
}
// Cream un cilindru orizontal cu capace (pentru roti si motor tren)
Mesh* Tema2::CreateCylinder(const std::string& name, glm::vec3 color, int segments)
{
std::vector<VertexFormat> vertices;
std::vector<unsigned int> indices;
const float r = 0.5f;
const float z0 = -0.5f;
const float z1 = 0.5f;
// Generam perechi de vertices de-a lungul cercului
for (int i = 0; i <= segments; i++) {
float a = (float)i / (float)segments * 2.0f * (float)M_PI;
float x = r * cos(a);
float y = r * sin(a);
vertices.push_back(VertexFormat(glm::vec3(x, y, z0), color));
vertices.push_back(VertexFormat(glm::vec3(x, y, z1), color));
}
// Conectam perechile pentru suprafata laterala
for (int i = 0; i < segments; i++) {
int i0 = 2 * i;
int i1 = 2 * i + 1;
int i2 = 2 * (i + 1);
int i3 = 2 * (i + 1) + 1;
indices.push_back(i0); indices.push_back(i2); indices.push_back(i1);
indices.push_back(i1); indices.push_back(i2); indices.push_back(i3);
}
// Adaugam centrele pentru cele 2 capace
int centerBack = (int)vertices.size();
vertices.push_back(VertexFormat(glm::vec3(0, 0, z0), color));
int centerFront = (int)vertices.size();
vertices.push_back(VertexFormat(glm::vec3(0, 0, z1), color));
// Generam triunghiurile pentru capace
for (int i = 0; i < segments; i++) {
int ringBack0 = 2 * i;
int ringBack1 = 2 * (i + 1);
indices.push_back(centerBack);
indices.push_back(ringBack1);
indices.push_back(ringBack0);
int ringFront0 = 2 * i + 1;
int ringFront1 = 2 * (i + 1) + 1;
indices.push_back(centerFront);
indices.push_back(ringFront0);
indices.push_back(ringFront1);
}
Mesh* mesh = new Mesh(name);
mesh->InitFromData(vertices, indices);
return mesh;
}
// Cream o sina si o adaugam in lista globala de sine
Rail* Tema2::CreateRail(glm::vec3 start, glm::vec3 end, RailType type)
{
Rail* rail = new Rail(start, end, type);
allRails.push_back(rail);
return rail;
}
// Cream reteaua de sine: 15 sine care conecteaza 5 junctiuni si 3 statii
void Tema2::CreateRailNetwork()
{
float y = 0.2f;
// Definim toate punctele cheie: junctiuni si statii
glm::vec3 centralJunction = glm::vec3(0, y, 0);
glm::vec3 junction1 = glm::vec3(-8, y, 0);
glm::vec3 junction2 = glm::vec3(-8, y, -8);
glm::vec3 junction3 = glm::vec3(0, y, -8);
glm::vec3 junction4 = glm::vec3(8, y, -8);
glm::vec3 resourceStation1 = glm::vec3(-16, y, 0);
glm::vec3 resourceStation2 = glm::vec3(-8, y, -16);
glm::vec3 resourceStation3 = glm::vec3(16, y, -8);
glm::vec3 auxPoint1 = glm::vec3(8, y, 0);
glm::vec3 auxPoint2 = glm::vec3(0, y, -16);
glm::vec3 greenCubeJunction = glm::vec3(0, y, 8);
glm::vec3 sphereStation = glm::vec3(-16, y, -16);
// Cream toate cele 15 sine (NORMAL = campie, BRIDGE = pod, TUNNEL = tunel)
CreateRail(centralJunction, auxPoint1, RailType::NORMAL);
CreateRail(auxPoint1, junction4, RailType::NORMAL);
CreateRail(junction4, resourceStation3, RailType::BRIDGE);
CreateRail(junction4, junction3, RailType::NORMAL);
CreateRail(junction3, auxPoint2, RailType::TUNNEL);
CreateRail(auxPoint2, resourceStation2, RailType::TUNNEL);
CreateRail(resourceStation2, sphereStation, RailType::NORMAL);
CreateRail(resourceStation2, junction2, RailType::TUNNEL);
CreateRail(junction3, centralJunction, RailType::NORMAL);
CreateRail(centralJunction, greenCubeJunction, RailType::NORMAL);
CreateRail(junction2, junction1, RailType::NORMAL);
CreateRail(junction1, resourceStation1, RailType::BRIDGE);
CreateRail(junction1, centralJunction, RailType::NORMAL);
CreateRail(junction2, junction3, RailType::NORMAL);
CreateRail(greenCubeJunction, centralJunction, RailType::NORMAL);
}
// Desenam o sina: calculam pozitia, rotatia si scalarea apoi randam mesh-ul corespunzator
void Tema2::RenderSingleRail(Rail* rail)
{
if (rail == nullptr) return;
glm::vec3 start = rail->startPosition;
glm::vec3 end = rail->endPosition;
glm::vec3 dir = end - start;
float length = glm::length(dir);
if (length < 0.001f) return;
// Plasam sina la mijlocul dintre start si end, rotita in directia corecta
glm::vec3 center = (start + end) * 0.5f;
float angle = atan2(dir.x, dir.z);
glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), center);
modelMatrix = glm::rotate(modelMatrix, angle, glm::vec3(0, 1, 0));
float railWidth = 0.5f;
float railHeight = 0.15f;
// NORMAL: sina neagra simpla
if (rail->type == RailType::NORMAL) {
glm::mat4 M = glm::scale(modelMatrix, glm::vec3(railWidth, railHeight, length));
RenderMesh(meshes["box_black"], shaders["VertexColor"], M);
}
// BRIDGE: 4 segmente alternante (albastru-alb-albastru-alb) pe lungime
else if (rail->type == RailType::BRIDGE) {
float segmentLength = length / 4.0f;
for (int i = 0; i < 4; i++) {
float offset = (i - 1.5f) * segmentLength;
glm::mat4 S = glm::translate(modelMatrix, glm::vec3(0, 0, offset));
S = glm::scale(S, glm::vec3(railWidth, railHeight, segmentLength * 0.9f));
const char* meshName = (i % 2 == 0) ? "box_blue" : "box_white";
RenderMesh(meshes[meshName], shaders["VertexColor"], S);
}
}
// TUNNEL: 4 segmente alternante (negru-galben-negru-galben) pe latime
else if (rail->type == RailType::TUNNEL) {
float segmentWidth = railWidth / 4.0f;
for (int i = 0; i < 4; i++) {
float offset = (i - 1.5f) * segmentWidth;
glm::mat4 S = glm::translate(modelMatrix, glm::vec3(offset, 0, 0));
S = glm::scale(S, glm::vec3(segmentWidth * 0.9f, railHeight, length));
const char* meshName = (i % 2 == 0) ? "box_black" : "box_yellow";
RenderMesh(meshes[meshName], shaders["VertexColor"], S);
}
}
}
// Randam toate sinele din retea
void Tema2::RenderRails()
{
for (Rail* rail : allRails) {
RenderSingleRail(rail);
}
}
// Randam cele 4 statii: 1 gara centrala (verde->rosu) si 3 statii de resurse
void Tema2::RenderStations()
{
// Gara centrala cu culoare dinamica (verde -> rosu pe masura ce timpul expira)
float normalizedTime = 1.0f - (currentOrder.timeRemaining / 60.0f);
glm::vec3 baseColor = glm::vec3(0.1f, 0.9f, 0.4f);
glm::vec3 urgentColor = glm::vec3(1.0f, 0.0f, 0.0f);
glm::vec3 stationColor = glm::mix(baseColor, urgentColor, normalizedTime);
AddMeshToList(CreateBox("central_station_dynamic", stationColor));
glm::mat4 centralMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0, 1.5f, 10));
centralMatrix = glm::scale(centralMatrix, glm::vec3(3.0f, 3.0f, 3.0f));
RenderMesh(meshes["central_station_dynamic"], shaders["VertexColor"], centralMatrix);
// Statia 1: Cub cyan la (-18, 1.5, 0)
glm::mat4 rs1Matrix = glm::translate(glm::mat4(1.0f), glm::vec3(-18, 1.5f, 0));
rs1Matrix = glm::scale(rs1Matrix, glm::vec3(2.5f, 3.0f, 2.5f));
RenderMesh(meshes["station_box"], shaders["VertexColor"], rs1Matrix);
// Statia 2: Sfera galbena la (-18, 1.8, -16)
glm::mat4 rs2Matrix = glm::translate(glm::mat4(1.0f), glm::vec3(-18, 1.8f, -16));
rs2Matrix = glm::scale(rs2Matrix, glm::vec3(3.5f, 3.5f, 3.5f));
RenderMesh(meshes["station_sphere"], shaders["VertexColor"], rs2Matrix);
// Statia 3: Piramida rosie la (18, 0.5, -8)
glm::mat4 rs3Matrix = glm::translate(glm::mat4(1.0f), glm::vec3(18, 0.5f, -8));
rs3Matrix = glm::scale(rs3Matrix, glm::vec3(2.5f, 2.5f, 2.5f));
RenderMesh(meshes["station_pyramid"], shaders["VertexColor"], rs3Matrix);
}
// Afisam comanda curenta (5 obiecte) deasupra garii centrale
void Tema2::RenderOrder()
{
float startX = -3.0f;
float spacing = 1.5f;
float yPos = 7.5f;
float zPos = 10.0f;
float scale = 0.6f;
for (size_t i = 0; i < currentOrder.resources.size(); i++) {
float xPos = startX + i * spacing;
glm::mat4 M = glm::translate(glm::mat4(1.0f), glm::vec3(xPos, yPos, zPos));
M = glm::scale(M, glm::vec3(scale, scale, scale));
switch (currentOrder.resources[i]) {
case ResourceType::RED_PYRAMID:
RenderMesh(meshes["station_pyramid"], shaders["VertexColor"], M);
break;
case ResourceType::CYAN_BOX:
RenderMesh(meshes["station_box"], shaders["VertexColor"], M);
break;
case ResourceType::YELLOW_SPHERE:
RenderMesh(meshes["station_sphere"], shaders["VertexColor"], M);
break;
}
}
}
// Afisam indicatori mici deasupra statiilor pentru resursele disponibile
void Tema2::RenderStationIndicators()
{
float indicatorScale = 1.2f;
float yOffset = 4.0f;
for (const StationResource& station : stations) {
if (!station.available) {
continue;
}
glm::vec3 visualPosition;
switch (station.type) {
case ResourceType::CYAN_BOX:
visualPosition = glm::vec3(-18, 1.5f + yOffset, 0);
break;
case ResourceType::YELLOW_SPHERE:
visualPosition = glm::vec3(-18, 1.8f + yOffset, -16);
break;
case ResourceType::RED_PYRAMID:
visualPosition = glm::vec3(18, 0.5f + yOffset, -8);
break;
}
glm::mat4 indicator = glm::translate(glm::mat4(1.0f), visualPosition);
indicator = glm::scale(indicator, glm::vec3(indicatorScale, indicatorScale, indicatorScale));
switch (station.type) {
case ResourceType::CYAN_BOX:
RenderMesh(meshes["station_box"], shaders["VertexColor"], indicator);
break;
case ResourceType::YELLOW_SPHERE:
RenderMesh(meshes["station_sphere"], shaders["VertexColor"], indicator);
break;
case ResourceType::RED_PYRAMID:
RenderMesh(meshes["station_pyramid"], shaders["VertexColor"], indicator);
break;
}
}
}
// Randam trenul: locomotiva (sasiu + cabina + motor + cos + 12 roti) si vagonul (sasiu + incarcatura + 4 roti)
void Tema2::RenderTrain()
{
// Matricea de baza pentru tot trenul (pozitie + rotatie)
glm::mat4 base = glm::translate(glm::mat4(1.0f), train.position + glm::vec3(0, 0.55f, 0));
base = glm::rotate(base, train.yaw, glm::vec3(0, 1, 0));
float bodyW = 1.1f;
float chassisH = 0.15f;
float chassisL = 2.6f;
float chassisCenter = 0.0f;
// ===== LOCOMOTIVA =====
// Sasiu galben
{
glm::mat4 M = base;
M = glm::translate(M, glm::vec3(0, -0.25f, chassisCenter));
M = glm::scale(M, glm::vec3(bodyW, chassisH, chassisL));
RenderMesh(meshes["box_yellow"], shaders["VertexColor"], M);
}
// Cos cilindric mov (la marginea din fata a sasiului)
float chimneyLength = 0.35f;
{
glm::mat4 M = base;
float chimneyZPos = chassisL / 2.0f - chimneyLength / 2.0f;
M = glm::translate(M, glm::vec3(0, 0.00f, chimneyZPos));
M = glm::scale(M, glm::vec3(0.18f, 0.18f, chimneyLength));
RenderMesh(meshes["cyl_purple"], shaders["VertexColor"], M);
}
// Motor cilindric albastru (in mijloc)
float engineLength = 1.35f;
{
glm::mat4 M = base;
M = glm::translate(M, glm::vec3(0, 0.10f, 0.3f));
M = glm::scale(M, glm::vec3(0.55f, 0.55f, engineLength));
RenderMesh(meshes["cyl_blue"], shaders["VertexColor"], M);
}
// Cabina alba (vatman) - lipita de cilindrul albastru din spate
float cabinLength = 0.85f;
{
glm::mat4 M = base;
float cabinZPos = 0.3f - (engineLength / 2.0f) - (cabinLength / 2.0f);
M = glm::translate(M, glm::vec3(0, 0.20f, cabinZPos));
M = glm::scale(M, glm::vec3(1.0f, 1.0f, cabinLength));
RenderMesh(meshes["box_white"], shaders["VertexColor"], M);
}
// Functie helper pentru desenare roti
auto drawWheel = [&](float xSide, float zPos)
{
glm::mat4 W = base;
W = glm::translate(W, glm::vec3(xSide, -0.325f, zPos));
W = glm::rotate(W, RADIANS(90.0f), glm::vec3(0, 1, 0));
W = glm::scale(W, glm::vec3(0.25f, 0.25f, 0.18f));
RenderMesh(meshes["wheel_red"], shaders["VertexColor"], W);
};
float xL = -0.55f;
float xR = 0.55f;
// Calculam pozitiile pentru 6 roti pe fiecare parte
float wheelSpacing = 0.4f;
float zWheels[6];
for (int i = 0; i < 6; i++) {
zWheels[i] = chassisCenter + (i - 2.5f) * wheelSpacing;
}
// Desenam rotile locomotivei (12 roti total)
for (int i = 0; i < 6; i++) {
drawWheel(xL, zWheels[i]);
drawWheel(xR, zWheels[i]);
}
// ===== VAGON =====
// Calculam distanta pentru a lipi vagonul de locomotiva
float locomotiveBackEdge = -(chassisL / 2.0f);
float wagonChassisL = 2.0f;
float wagonDistance = locomotiveBackEdge - (wagonChassisL / 2.0f) - 0.1f;
float wagonChassisH = 0.15f;
float wagonCargoH = 0.8f;
float wagonCargoL = 1.8f;
// Sasiu galben vagon
{
glm::mat4 M = base;
M = glm::translate(M, glm::vec3(0, -0.25f, wagonDistance));
M = glm::scale(M, glm::vec3(bodyW, wagonChassisH, wagonChassisL));
RenderMesh(meshes["box_yellow"], shaders["VertexColor"], M);
}
// Incarcatura portocalie
{
glm::mat4 M = base;
M = glm::translate(M, glm::vec3(0, 0.15f, wagonDistance));
M = glm::scale(M, glm::vec3(0.95f, wagonCargoH, wagonCargoL));
RenderMesh(meshes["cabin_orange"], shaders["VertexColor"], M);
}
// Roti vagon (4 roti total: 2 pe fiecare parte)
float wagonWheelPositions[2] = {wagonDistance - 0.5f, wagonDistance + 0.5f};
for (int i = 0; i < 2; i++) {
drawWheel(xL, wagonWheelPositions[i]);
drawWheel(xR, wagonWheelPositions[i]);
}
}
// Randam terenul: campie verde + apa sub poduri + munti peste tuneluri
void Tema2::RenderTerrain()
{
// Campie verde (40x40)
glm::mat4 groundMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0, -0.1f, -4));
groundMatrix = glm::scale(groundMatrix, glm::vec3(40, 0.1f, 40));
RenderMesh(meshes["box_green"], shaders["VertexColor"], groundMatrix);
// Apa sub poduri
for (Rail* rail : allRails) {
if (rail->type == RailType::BRIDGE) {
glm::vec3 start = rail->startPosition;
glm::vec3 end = rail->endPosition;
glm::vec3 center = (start + end) * 0.5f;
glm::vec3 dir = end - start;
float length = glm::length(dir);
float angle = atan2(dir.x, dir.z);
glm::mat4 waterMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(center.x, -0.05f, center.z));
waterMatrix = glm::rotate(waterMatrix, angle, glm::vec3(0, 1, 0));
waterMatrix = glm::scale(waterMatrix, glm::vec3(1.2f, 0.05f, length + 0.5f));
RenderMesh(meshes["box_water"], shaders["VertexColor"], waterMatrix);
}
}
// Munti peste tuneluri
for (Rail* rail : allRails) {
if (rail->type == RailType::TUNNEL) {
glm::vec3 start = rail->startPosition;
glm::vec3 end = rail->endPosition;
glm::vec3 center = (start + end) * 0.5f;
glm::vec3 dir = end - start;
float length = glm::length(dir);
float angle = atan2(dir.x, dir.z);
glm::mat4 mountainMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(center.x, -0.3f, center.z));
mountainMatrix = glm::rotate(mountainMatrix, angle, glm::vec3(0, 1, 0));
mountainMatrix = glm::scale(mountainMatrix, glm::vec3(2.0f, 0.8f, length + 0.5f));
RenderMesh(meshes["box_mountain"], shaders["VertexColor"], mountainMatrix);
}
}
}
// Initializam frame-ul: curatam bufferele si configuram viewport-ul
void Tema2::FrameStart()
{
glClearColor(0.53f, 0.81f, 0.92f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::ivec2 resolution = window->GetResolution();
glViewport(0, 0, resolution.x, resolution.y);
}
// Bucla principala: randam scena si actualizam logica jocului (timer, tren, resurse)
void Tema2::Update(float deltaTimeSeconds)
{
// Daca jocul s-a terminat, afisam doar ecranul de Game Over
if (gameOver) {
RenderUI();
return;
}
// Randam scena completa
RenderTerrain();
RenderRails();
RenderStations();
RenderStationIndicators();
RenderOrder();
RenderTrain();
RenderUI();
// Actualizam timer-ul comenzii
currentOrder.timeRemaining -= deltaTimeSeconds;
if (currentOrder.timeRemaining <= 0.0f) {
gameOver = true;
return;
}
// Gestionam respawn-ul resurselor (dupa 5 secunde)
for (StationResource& station : stations) {
if (!station.available) {
station.respawnTimer -= deltaTimeSeconds;
if (station.respawnTimer <= 0.0f) {
station.available = true;
}
}
}
// Miscam trenul pe sina curenta (daca nu asteapta la junctiune)
if (train.currentRail != nullptr && !train.waitingAtJunction) {
train.progress += deltaTimeSeconds * train.speed;
if (train.progress >= 1.0f) {
train.progress = 1.0f;
train.UpdatePosition();
train.waitingAtJunction = true;
CheckResourceCollection();
CheckCentralStationReturn();
if (train.movingForward) {
glm::vec3 pos = train.currentRail->endPosition;
} else {
glm::vec3 pos = train.currentRail->startPosition;
}
} else {
train.UpdatePosition();
}
}
}
void Tema2::FrameEnd()
{
}
// Randam un mesh cu shader si matricea de transformare specificata
void Tema2::RenderMesh(Mesh* mesh, Shader* shader, const glm::mat4& modelMatrix)
{
if (!mesh || !shader || !shader->program)
return;
shader->Use();
glUniformMatrix4fv(shader->loc_view_matrix, 1, GL_FALSE, glm::value_ptr(camera->GetViewMatrix()));
glUniformMatrix4fv(shader->loc_projection_matrix, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniformMatrix4fv(shader->loc_model_matrix, 1, GL_FALSE, glm::value_ptr(modelMatrix));
mesh->Render();
}
// Gestionam input-ul continuu pentru camera (WASD = deplasare, QE = sus/jos)
void Tema2::OnInputUpdate(float deltaTime, int mods)
{
float cameraSpeed = 10.0f;
if (window->KeyHold(GLFW_KEY_W)) camera->MoveForward(cameraSpeed * deltaTime);
if (window->KeyHold(GLFW_KEY_S)) camera->MoveForward(-cameraSpeed * deltaTime);
if (window->KeyHold(GLFW_KEY_A)) camera->TranslateRight(-cameraSpeed * deltaTime);
if (window->KeyHold(GLFW_KEY_D)) camera->TranslateRight(cameraSpeed * deltaTime);
if (window->KeyHold(GLFW_KEY_Q)) camera->TranslateUpward(-cameraSpeed * deltaTime);
if (window->KeyHold(GLFW_KEY_E)) camera->TranslateUpward(cameraSpeed * deltaTime);
}
// Cautam o sina care conecteaza pozitia curenta cu tinta (currentPos + delta)
// Algoritmul verifica toate sinele si returneaza prima care satisface:
// 1. FORWARD: startPosition aproape de currentPos SI endPosition aproape de target
// 2. BACKWARD: endPosition aproape de currentPos SI startPosition aproape de target
Rail* Tema2::FindRailInDirection(const glm::vec3& currentPos, int deltaX, int deltaZ)
{
glm::vec3 targetPos = currentPos + glm::vec3(deltaX, 0, deltaZ);
for (Rail* rail : allRails) {
float distStart = glm::length(rail->startPosition - currentPos);
float distEnd = glm::length(rail->endPosition - targetPos);
// Varianta FORWARD: sina merge de la currentPos spre target
if (distStart < 0.5f && distEnd < 0.5f) {
return rail;
}
// Varianta BACKWARD: sina merge de la target spre currentPos (parcurgem invers)
float distStartRev = glm::length(rail->endPosition - currentPos);
float distEndRev = glm::length(rail->startPosition - targetPos);
if (distStartRev < 0.5f && distEndRev < 0.5f) {
return rail;
}
}
return nullptr;
}
// Incercam sa mutam trenul in directia specificata (deltaX, deltaZ)
void Tema2::TryMoveInDirection(int deltaX, int deltaZ)
{
if (!train.waitingAtJunction || train.currentRail == nullptr) {
return;
}
// Determinam pozitia exacta a trenului (la capatul sau inceputul sinei)
glm::vec3 currentPos;
if (train.progress >= 0.99f) {
currentPos = train.movingForward ?
train.currentRail->endPosition :
train.currentRail->startPosition;
} else {
currentPos = train.movingForward ?
train.currentRail->startPosition :
train.currentRail->endPosition;
}
Rail* nextRail = FindRailInDirection(currentPos, deltaX, deltaZ);
if (nextRail != nullptr) {
glm::vec3 targetPos = currentPos + glm::vec3(deltaX, 0, deltaZ);
// Determinam directia de mers pe noua sina (forward sau backward)
float distToEnd = glm::length(nextRail->endPosition - targetPos);
float distToStart = glm::length(nextRail->startPosition - targetPos);
train.currentRail = nextRail;
train.progress = 0.0f;
train.waitingAtJunction = false;
if (distToEnd < distToStart) {
train.movingForward = true;
} else {
train.movingForward = false;
}
train.UpdatePosition();
}
}
// Gestionam apasarea tastelor: R pentru restart, sageti pentru directia trenului
void Tema2::OnKeyPress(int key, int mods)
{
// Restart dupa Game Over
if (gameOver && key == GLFW_KEY_R) {
gameOver = false;
currentOrder.GenerateRandom();
for (StationResource& station : stations) {
station.available = true;
station.respawnTimer = 0.0f;
}
glm::vec3 startPos = glm::vec3(0, 0.2f, 8);
glm::vec3 targetPos = glm::vec3(0, 0.2f, 0);
for (Rail* rail : allRails) {
if (glm::length(rail->startPosition - startPos) < 0.01f &&
glm::length(rail->endPosition - targetPos) < 0.01f) {
train.currentRail = rail;
train.progress = 0.0f;
train.speed = 0.5f;
train.waitingAtJunction = true;
train.movingForward = true;
train.UpdatePosition();
break;
}
}
return;
}
// Control tren: calculam directia relativa la orientarea trenului
if (train.waitingAtJunction) {
float cosYaw = cos(train.yaw);
float sinYaw = sin(train.yaw);
int deltaX = 0;
int deltaZ = 0;
switch (key) {
case GLFW_KEY_UP:
deltaX = static_cast<int>(round(sinYaw * 8.0f));
deltaZ = static_cast<int>(round(cosYaw * 8.0f));
break;
case GLFW_KEY_DOWN:
deltaX = static_cast<int>(round(-sinYaw * 8.0f));
deltaZ = static_cast<int>(round(-cosYaw * 8.0f));
break;
case GLFW_KEY_LEFT:
deltaX = static_cast<int>(round(cosYaw * 8.0f));
deltaZ = static_cast<int>(round(-sinYaw * 8.0f));
break;
case GLFW_KEY_RIGHT:
deltaX = static_cast<int>(round(-cosYaw * 8.0f));
deltaZ = static_cast<int>(round(sinYaw * 8.0f));
break;
default:
return;
}
if (deltaX != 0 || deltaZ != 0) {
TryMoveInDirection(deltaX, deltaZ);
}
}
}
// Verificam daca trenul a ajuns la o statie si colectam resursa daca e necesara
void Tema2::CheckResourceCollection()
{
if (train.currentRail == nullptr || !train.waitingAtJunction) {
return;
}
glm::vec3 trainPos = train.position;
for (StationResource& station : stations) {
float distance = glm::length(glm::vec3(trainPos.x - station.position.x, 0, trainPos.z - station.position.z));
if (distance < 2.0f && station.available) {
if (currentOrder.RemoveResource(station.type)) {
station.available = false;
station.respawnTimer = 5.0f;
if (currentOrder.IsComplete()) {
currentOrder.allCollected = true;
}
}
break;
}
}
}
// Verificam daca trenul s-a intors la gara centrala cu toate resursele colectate
void Tema2::CheckCentralStationReturn()
{
if (!currentOrder.allCollected) {
return;
}
glm::vec3 trainPos = train.position;
glm::vec3 centralStation = glm::vec3(0, 0.2f, 8);
float distance = glm::length(glm::vec3(trainPos.x - centralStation.x, 0, trainPos.z - centralStation.z));
if (distance < 2.0f) {
currentOrder.GenerateRandom();
}
}
// Afisam UI-ul: ecran Game Over sau timer-ul in timpul jocului
void Tema2::RenderUI()
{
if (textRenderer == nullptr) return;
glm::ivec2 resolution = window->GetResolution();
if (gameOver) {
textRenderer->RenderText("GAME OVER", (float)(resolution.x / 2 - 150), (float)(resolution.y / 2), 1.5f, glm::vec3(1, 0, 0));
textRenderer->RenderText("Press R to restart",
(float)(resolution.x / 2 - 150), (float)(resolution.y / 2 - 80), 1.0f, glm::vec3(1, 1, 1));
} else {
int minutes = (int)currentOrder.timeRemaining / 60;
int seconds = (int)currentOrder.timeRemaining % 60;
std::string timerText = "Timp: " + std::to_string(minutes) + ":" +
(seconds < 10 ? "0" : "") + std::to_string(seconds);
textRenderer->RenderText(timerText, 20.0f, (float)(resolution.y - 50), 1.0f, glm::vec3(1, 1, 1));
}
}
void Tema2::OnKeyRelease(int key, int mods)
{
}
// Gestionam miscarea mouse-ului: click dreapta pentru rotirea camerei
void Tema2::OnMouseMove(int mouseX, int mouseY, int deltaX, int deltaY)
{
if (window->MouseHold(GLFW_MOUSE_BUTTON_RIGHT)) {
float sensitivity = 0.002f;
camera->RotateFirstPerson_OY(-deltaX * sensitivity);
camera->RotateFirstPerson_OX(-deltaY * sensitivity);
}
}
void Tema2::OnMouseBtnPress(int mouseX, int mouseY, int button, int mods)
{
}
void Tema2::OnMouseBtnRelease(int mouseX, int mouseY, int button, int mods)
{
}
void Tema2::OnMouseScroll(int mouseX, int mouseY, int offsetX, int offsetY)
{
}
void Tema2::OnWindowResize(int width, int height)
{
}