-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1255 lines (1025 loc) · 35.4 KB
/
Copy pathmain.cpp
File metadata and controls
1255 lines (1025 loc) · 35.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
#include <SFML/Graphics.hpp>
#include <iostream>
#include <future>
#include <ctime>
#include <cmath>
#include <vector>
#include "PerlinNoise.hpp"
using namespace std;
using namespace sf;
// Char identifiers for entities on screen
// (These chars are stored in the blueprints array, so that we can keep track of everyone's position)
char waterCharIdentifier = 'w';
char landCharIdentifier = 'l';
char rabbitCharIdentifier = 'r';
char plantCharIdentifier = 'p';
char wolfCharIdentifier = 'W';
// Setting global variables
const int height = 1080 / 2;
const int width = 1920 / 2;
// -------- RABBIT VARIABLES ----------
int intialNumRabbits = 30;
float rabbitSize = 1.5;
int rabbitVision = 30;
// This is the maximum of each thing an entity can live with, more than
// that will end in death (does not apply to reproductive urge)
float rabbitMaxHunger = 100;
float rabbitMaxThirst = 120;
float rabbitMaxReproductiveUrge = 100;
// These delta values are the amount that is incremented per frame to the
// respective thing(hunger, thirst or reproductive urge)
float rabbitHungerDelta = 0.05;
float rabbitThirstDelta = 0.05;
float rabbitReproductiveUrgeDelta = 0.05;
// Speed is randomly assigned to each object, so here are the min and max
// values for it.
float rabbitSpeedMin = 0.3;
float rabbitSpeedMax = 0.5;
// -------- WOLF VARIABLES ----------
int initialNumWolves = 20;
float wolfSize = 1.5;
int wolfVision = 40;
float wolfMaxHunger = 100;
float wolfMaxThirst = 80;
float wolfMaxReproductiveUrge = 50;
// These delta values are the amount that is incremented per frame to the
// respective thing(hunger, thirst or reproductive urge)
float wolfHungerDelta = 0.05;
float wolfThirstDelta = 0.05;
float wolfReproductiveUrgeDelta = 0.05;
float wolfSpeedMin = 0.5;
float wolfSpeedMax = 1;
// -------- PLANT VARIABLES ----------
float plantDensity = 4;
float plantSize = 5;
int numPlants = floor((width * height) * plantDensity);
// -------- COLOR VARIABLES ----------
// These store rgba values for colors used
int landColorRGBA[4] = {(int)(255 * 3.1 / 100), (int)(255 * 64.7 / 100), (int)(255 * 9.0 / 100), 255};
int waterColorRGBA[4] = {(int)(255 * 11.0 / 100), (int)(255 * 29.0 / 100), (int)(255 * 85.5 / 100), 255};
int treeColorRGBA[4] = {(int)(255 * 0.0 / 100), (int)(255 * 42.0 / 100), (int)(255 * 15.7 / 100), 255};
// -------- OTHER VARIABLES ----------
int frameRate = 30;
const float pi = 3.142;
class Rabbit;
class Plant;
class Wolf;
// Vector arrays to store objects
vector<Rabbit *> rabbits;
vector<Plant *> plants;
vector<Wolf *> wolves;
// Objects for terrain generation and display
Image terrainTextureImage;
Texture terrainTexture;
Sprite backgroundSprite;
// The POSITION BLUEPRINT
// Array that holds the position data for entities
vector<char> positionBlueprint[width][height];
// Boolean that is false until terrain is generated
bool terrainGenerated = false;
// Some Function headers
bool isLand(int x, int y);
bool isWithinBounds(int x, int y);
void addToPositionBlueprint(char charIdentifier, int x, int y);
bool checkPositionInBlueprint(char charIdentifier, int x, int y);
void removePositionFromBlueprint(char charIdentifier, int x, int y);
void removePlant(Vector2f position);
void addRabbit(Vector2f position);
void removeRabbit(Vector2f position);
void addWolf(Vector2f position);
void removeWolf(Vector2f position);
// ----------------- CLASSES ------------------
class Animal
{
protected:
Sprite shape; // SFML Shape object for the animal
Texture animalTexture;
float speed; // Speed of the animal
Vector2f direction; // Direction the animal is headed
Vector2f position; // Current position of the animal
Vector2f headedTo; // Direction the animal is headed when roaming randomly
Vector2f closestFoodSource; // Stores the location of closest food source for the animal
Vector2f closestWaterSource; // Stores the location of closest water source for the animal
Vector2f closestMate; // Stored the location of the closest mate for the animal
float maxHunger; // Max hunger that the animal can live with
float maxThirst; // Max thirst that the animal can live with
float maxReproductiveUrge; // Max reproductive urge
float hungerLevel; // Current hunger level of the animal
float thirstLevel; // Current thirst level of the animal
float reproductiveUrge; // Current reproductive urge of the animal
friend void initializeRabbits(RenderWindow *window);
public:
Animal(
float speed,
Vector2f direction,
Vector2f position,
float maxHunger,
float maxThirst,
float maxReproductiveUrge)
: speed(speed),
direction(direction),
position(position),
maxHunger(maxHunger),
maxThirst(maxThirst),
maxReproductiveUrge(maxReproductiveUrge),
closestFoodSource(Vector2f(-1, -1)),
closestWaterSource(Vector2f(-1, -1)),
closestMate(Vector2f(-1, -1))
{
// Setting the shape origin to center
shape.setOrigin(shape.getGlobalBounds().width / 2, shape.getGlobalBounds().height / 2);
shape.setPosition(floor(position.x), floor(position.y));
// Setting the initial headed to to a valid value so that it doesnt break
headedTo = position;
}
virtual void draw(RenderWindow *window) = 0; // virtual function
Vector2f getPosition()
{
return position;
}
// A function that choses random coordinates that are in range of
// the animal's sight as the next headed to value
void roam()
{
if (terrainGenerated)
{
Vector2f vectorToNextPoint = headedTo - position;
float distanceToNextPoint = pow(pow(vectorToNextPoint.x, 2) + pow(vectorToNextPoint.y, 2), 0.5);
// Generate new value if animal is close to the current headedTo position
if (distanceToNextPoint < 5)
{
// Select new point to roam to
int x, y;
do
{
float theta = (((float)(rand() % 1000) / 1000)) * (float)(2 * pi);
float r = (((float)(rand() % 1000) / 1000)) * rabbitVision;
x = (int)round(position.x + (float)(r * cos(theta)));
y = (int)round(position.y + (float)(r * sin(theta)));
} while (!isLand(x, y));
headedTo = Vector2f(x, y);
}
}
}
// Common part of the move function for all children
virtual void move()
{
// Basic move funcion that just sets the animal's direction to its next goal
Vector2f vectorToNextPoint = headedTo - position;
float distanceToNextPoint = pow(pow(vectorToNextPoint.x, 2) + pow(vectorToNextPoint.y, 2), 0.5);
direction = Vector2f(vectorToNextPoint.x / distanceToNextPoint, vectorToNextPoint.y / distanceToNextPoint);
}
void update()
{
roam();
move();
}
};
class Rabbit : public Animal
{
protected:
Vector2f threatsAverageLocation;
public:
Rabbit(
float speed,
Vector2f direction,
Vector2f position,
float maxHunger,
float maxThirst,
float maxReproductiveUrge)
: Animal(
speed,
direction,
position,
maxHunger,
maxThirst,
maxReproductiveUrge),
threatsAverageLocation(Vector2f(-1, -1))
{
// Setting the rabbit sprite with the rabbit image and giving it size
Image rabbitImage;
rabbitImage.loadFromFile("assets/images/RabbitFace.png");
animalTexture.loadFromImage(rabbitImage);
shape.setTexture(animalTexture);
shape.setScale(Vector2f(shape.getScale().x / 15 * rabbitSize, shape.getScale().y / 15 * rabbitSize));
// shape.setFillColor(Color::White);
shape.setOrigin(shape.getGlobalBounds().width / 2, shape.getGlobalBounds().height / 2);
// Setting random values for thirst hunger and mating urge
hungerLevel = (float)(rand() % (int)(rabbitMaxHunger));
thirstLevel = (float)(rand() % (int)(rabbitMaxThirst));
reproductiveUrge = (float)(rand() % (int)(rabbitMaxReproductiveUrge));
}
void move() override
{
// Calling the parent's move because it is common for both children
Animal::move();
// Calculate velocity
Vector2f velocity;
velocity.x = direction.x * speed;
velocity.y = direction.y * speed;
// Remove old position blueprint
removePositionFromBlueprint('r', floor(position.x), floor(position.y));
position.x = position.x + velocity.x;
position.y = position.y + velocity.y;
// Add new position to blueprint
addToPositionBlueprint('r', floor(position.x), floor(position.y));
shape.setPosition(position);
}
void draw(RenderWindow *window)
{
window->draw(shape);
}
// Returns true if rabbit is near a plant
bool atPlant()
{
bool found = false;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
found = checkPositionInBlueprint(plantCharIdentifier, position.x + i - 2, position.y + j - 2);
if (found)
return found;
}
}
return found;
}
// Returns true if rabbit is near water
bool atWater()
{
bool found = false;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
found = checkPositionInBlueprint(waterCharIdentifier, position.x + i - 2, position.y + j - 2);
if (found)
return found;
}
}
return found;
}
// Returns true if rabbit is near another rabbit
bool atMate()
{
bool found = false;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (i != 0 && j != 0)
{
found = checkPositionInBlueprint(rabbitCharIdentifier, position.x + i - 1, position.y + j - 1);
if (found)
return found;
}
}
}
return found;
}
// A function that scans the surroundings and takes note of important things
void scanSurroundings()
{
closestFoodSource = Vector2f(-1, -1);
closestWaterSource = Vector2f(-1, -1);
closestMate = Vector2f(-1, -1);
threatsAverageLocation = Vector2f(-1, -1);
// Scan suroundings and look for plant
for (int r = 0; r < rabbitVision + 1; r++)
{
// First checking the pixel the rabbit is currently on
if (r == 0)
{
int search_x = round(position.x);
int search_y = round(position.y);
// If plant found and plant not already found (is closest)
if (checkPositionInBlueprint(plantCharIdentifier, search_x, search_y) && closestFoodSource == Vector2f(-1, -1))
{
closestFoodSource = Vector2f(search_x, search_y);
}
// If water found and is closest
if (checkPositionInBlueprint(waterCharIdentifier, search_x, search_y) && closestWaterSource == Vector2f(-1, -1))
{
closestWaterSource = Vector2f(search_x, search_y);
}
}
// Checking all other pixels in the rabbit's field of vision other than its own position
else
{
float dtheta = (float)(1 / (float)(2 * r));
for (float theta = 0; theta < (2 * pi); theta += dtheta)
{
int search_x = round(position.x + r * cos(theta));
int search_y = round(position.y + r * sin(theta));
// If plant found and plant not already found
if (checkPositionInBlueprint(plantCharIdentifier, search_x, search_y) && closestFoodSource == Vector2f(-1, -1))
{
closestFoodSource = Vector2f(search_x, search_y);
}
// If water found
if (checkPositionInBlueprint(waterCharIdentifier, search_x, search_y) && closestWaterSource == Vector2f(-1, -1))
{
closestWaterSource = Vector2f(search_x, search_y);
}
// If mate found
if (checkPositionInBlueprint(rabbitCharIdentifier, search_x, search_y) && closestMate == Vector2f(-1, -1))
{
if (floor(position.x) != search_x || floor(position.y) != search_y)
{
closestMate = Vector2f(search_x, search_y);
}
}
}
}
}
}
void update()
{
// Increasing hunger, thirst and reproductive urge with time
hungerLevel += rabbitHungerDelta;
thirstLevel += rabbitThirstDelta;
reproductiveUrge += rabbitReproductiveUrgeDelta;
// Scanning surroundings to take note of everything
scanSurroundings();
// Now checking where to go to next
// If hunger level is high then set next destination as food (if available)
if (hungerLevel > (float)(maxHunger / 2) && closestFoodSource != Vector2f(-1, -1))
{
headedTo = closestFoodSource;
if (atPlant())
{
hungerLevel = 0;
}
}
// Else if thirst level is high then set next destination as water (if available)
else if (thirstLevel > (float)(maxThirst / 2) && closestWaterSource != Vector2f(-1, -1))
{
headedTo = closestWaterSource;
if (atWater())
{
thirstLevel = 0;
}
}
// Else if reprodcutive urge is high then set next destination as mate (if available)
else if (reproductiveUrge > (float)(maxReproductiveUrge / 2) && closestMate != Vector2f(-1, -1))
{
headedTo = closestMate;
if (atMate())
{
reproductiveUrge = 0;
// CREATE BABY
addRabbit(position);
}
}
// If all urges satisfied, then just roam randomly
else
{
roam();
}
// Kill if too much hunger or thirst
if (hungerLevel > maxHunger || thirstLevel > maxThirst)
{
removeRabbit(position);
}
// Move towards the next point (headed to)
move();
}
};
// Same as above except food is rabbit instead of plant and mate is fox instead of rabbit
class Wolf : public Animal
{
protected:
public:
Wolf(
float speed,
Vector2f direction,
Vector2f position,
float maxHunger,
float maxThirst,
float maxReproductiveUrge)
: Animal(
speed,
direction,
position,
maxHunger,
maxThirst,
maxReproductiveUrge)
{
Image wolfImage;
wolfImage.loadFromFile("assets/images/WolfFace.png");
animalTexture.loadFromImage(wolfImage);
shape.setTexture(animalTexture);
shape.setScale(Vector2f(shape.getScale().x / 12 * wolfSize, shape.getScale().y / 12 * wolfSize));
shape.setOrigin(shape.getScale().x / 2, shape.getScale().y / 2);
// Setting random values for thirst hunger and mating urge
hungerLevel = (float)(rand() % (int)(wolfMaxHunger));
thirstLevel = (float)(rand() % (int)(wolfMaxThirst));
reproductiveUrge = (float)(rand() % (int)(wolfMaxReproductiveUrge));
}
void move() override
{
Animal::move();
Vector2f velocity;
velocity.x = direction.x * speed;
velocity.y = direction.y * speed;
// Remove old position blueprint
removePositionFromBlueprint(wolfCharIdentifier, floor(position.x), floor(position.y));
position.x = position.x + velocity.x;
position.y = position.y + velocity.y;
addToPositionBlueprint(wolfCharIdentifier, floor(position.x), floor(position.y));
shape.setPosition(position);
}
void draw(RenderWindow *window)
{
window->draw(shape);
}
bool atRabbit()
{
return checkPositionInBlueprint(rabbitCharIdentifier, position.x, position.y);
}
bool atWater()
{
bool found = false;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
found = checkPositionInBlueprint(waterCharIdentifier, position.x + i - 2, position.y + j - 2);
if (found)
return found;
}
}
return found;
}
bool atMate()
{
bool found = false;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (i != 0 && j != 0)
{
found = checkPositionInBlueprint(wolfCharIdentifier, position.x + i - 1, position.y + j - 1);
if (found)
return found;
}
}
}
return found;
}
void scanSurroundings()
{
closestFoodSource = Vector2f(-1, -1);
closestWaterSource = Vector2f(-1, -1);
closestMate = Vector2f(-1, -1);
// Scan suroundings and look for plant
for (int r = 0; r < wolfVision + 1; r++)
{
// First checking the pixel the wolf is currently on
if (r == 0)
{
int search_x = round(position.x);
int search_y = round(position.y);
// If rabbit found and plant not already found
if (checkPositionInBlueprint(rabbitCharIdentifier, search_x, search_y) && closestFoodSource == Vector2f(-1, -1))
{
closestFoodSource = Vector2f(search_x, search_y);
}
// If water found
if (checkPositionInBlueprint(waterCharIdentifier, search_x, search_y) && closestWaterSource == Vector2f(-1, -1))
{
closestWaterSource = Vector2f(search_x, search_y);
}
}
// Checking all other pixels in the wolf's field of vision other than its own position
else
{
float dtheta = (float)(1 / (float)(2 * r));
for (float theta = 0; theta < (2 * pi); theta += dtheta)
{
int search_x = round(position.x + r * cos(theta));
int search_y = round(position.y + r * sin(theta));
// If rabbit found and plant not already found
if (checkPositionInBlueprint(rabbitCharIdentifier, search_x, search_y) && closestFoodSource == Vector2f(-1, -1))
{
closestFoodSource = Vector2f(search_x, search_y);
}
// If water found
if (checkPositionInBlueprint(waterCharIdentifier, search_x, search_y) && closestWaterSource == Vector2f(-1, -1))
{
closestWaterSource = Vector2f(search_x, search_y);
}
// If mate found
if (checkPositionInBlueprint(wolfCharIdentifier, search_x, search_y) && closestMate == Vector2f(-1, -1))
{
if (floor(position.x) != search_x || floor(position.y) != search_y)
{
closestMate = Vector2f(search_x, search_y);
}
}
}
}
}
}
void update()
{
hungerLevel += wolfHungerDelta;
thirstLevel += wolfThirstDelta;
reproductiveUrge += wolfReproductiveUrgeDelta;
scanSurroundings();
// If hunger level is down then check for plant before roaming
if (hungerLevel > (float)(maxHunger / 2) && closestFoodSource != Vector2f(-1, -1))
{
headedTo = closestFoodSource;
if (atRabbit())
{
hungerLevel = 0;
removeRabbit(Vector2f(floor(position.x), floor(position.y)));
}
}
else if (thirstLevel > (float)(maxThirst / 2) && closestWaterSource != Vector2f(-1, -1))
{
headedTo = closestWaterSource;
if (atWater())
{
thirstLevel = 0;
}
}
else if (reproductiveUrge > (float)(maxReproductiveUrge / 2) && closestMate != Vector2f(-1, -1))
{
headedTo = closestMate;
if (atMate())
{
reproductiveUrge = 0;
// CREATE BABY
addWolf(position);
}
}
else
{
roam();
}
// Kill if too much hunger
if (hungerLevel > maxHunger || thirstLevel > maxThirst)
{
removeWolf(position);
}
move();
}
};
class Plant
{
CircleShape shape;
Vector2f position;
public:
Plant(Vector2f position) : position(position)
{
// Set graphical stuff
shape.setRadius(plantSize);
shape.setOutlineColor(Color(0, 50, 0, 255));
shape.setOutlineThickness(0.5);
// Centering the shape's origin
shape.setOrigin(shape.getGlobalBounds().width / 2, shape.getGlobalBounds().height / 2);
shape.setPosition(floor(position.x), floor(position.y));
shape.setFillColor(Color(treeColorRGBA[0], treeColorRGBA[1], treeColorRGBA[2], treeColorRGBA[3]));
}
Vector2f getPosition()
{
return this->position;
}
void draw(RenderWindow *window)
{
window->draw(shape);
}
};
void displayLoadingScreen(RenderWindow *window)
{
// Creating and setting text
Text headingText;
Font font;
font.loadFromFile("assets/fonts/8-bit-hud.ttf");
headingText.setFont(font);
headingText.setString("Generating\nTerrain...");
headingText.setCharacterSize(25);
sf::FloatRect bounds = headingText.getLocalBounds();
headingText.setOrigin(-bounds.left + bounds.width / 2.f, -bounds.top + bounds.height / 2.f);
headingText.setPosition(Vector2f(window->getSize().x / 2, window->getSize().y / 2));
headingText.setFillColor(Color::White);
// Drawing and displaying loading screen text
window->clear();
window->draw(headingText);
window->display();
}
void drawPopulationStats(RenderWindow *window)
{
// Drawing a translucent rectangle and the number of population
// as text on top of it.
Text text;
Font font;
font.loadFromFile("assets/fonts/Jersey15-Regular.ttf");
text.setFont(font);
String textString = "Rabbits Alive: " + to_string(rabbits.size()) + " Wolves Alive: " + to_string(wolves.size());
RectangleShape shape(Vector2f(width, 30));
shape.setFillColor(Color(0, 0, 0, 255 * 0.9));
text.setFillColor(Color::White);
text.setString(textString);
text.setCharacterSize(20);
shape.setPosition(Vector2f(0, 0));
text.setPosition(Vector2f((width / 2) - (text.getLocalBounds().width / 2), (text.getLocalBounds().height - 10)));
window->draw(shape);
window->draw(text);
}
bool onIntro = true;
void drawIntroScreen(RenderWindow *window)
{
// Draw the intro menu image
Image introImage;
introImage.loadFromFile("assets/images/IntroScreen.png");
Texture introTexture;
introTexture.loadFromImage(introImage);
Sprite introSprite;
introSprite.setTexture(introTexture);
introSprite.setScale(0.5, 0.5);
window->draw(introSprite);
}
// ------------ TERRAIN FUNCTIONS ----------------
// To generate a terrain using perlin noise
void generateTerrain()
{
terrainTextureImage.create(width, height, sf::Color(0, 0, 0, 0));
const siv::PerlinNoise::seed_type seed = rand();
const siv::PerlinNoise perlin{seed};
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
const double noise = perlin.octave2D_01((i * 0.01), (j * 0.01), 1, 0.2);
// Values above 0.4 are land, rest are water
if (noise > 0.4)
{
addToPositionBlueprint('l', i, j);
terrainTextureImage.setPixel(i, j, Color(landColorRGBA[0], landColorRGBA[1], landColorRGBA[2], landColorRGBA[3]));
}
else
{
addToPositionBlueprint('w', i, j);
terrainTextureImage.setPixel(i, j, Color(waterColorRGBA[0], waterColorRGBA[1], waterColorRGBA[2], waterColorRGBA[3]));
}
}
}
terrainTexture.loadFromImage(terrainTextureImage);
backgroundSprite.setTexture(terrainTexture);
terrainGenerated = true;
// Printing seed for the terrain generated
cout << "\nSeed: " << seed << endl;
}
// ------------ POSITION BLUEPRINT FUNCTIONS ----------------
// Add the character to the blueprint at specific pixel
void addToPositionBlueprint(char charIdentifier, int x, int y)
{
// Adding the char to the blueprint
if (isWithinBounds(x, y))
{
positionBlueprint[x][y].push_back(charIdentifier);
}
}
// Checks if the character exists in the blueprint at the specific pixel
bool checkPositionInBlueprint(char charIdentifier, int x, int y)
{
if (isWithinBounds(x, y))
{
for (int i = 0; i < positionBlueprint[x][y].size(); i++)
{
if (positionBlueprint[x][y][i] == charIdentifier)
{
return true;
}
}
}
return false;
}
// Remove the character to the blueprint at specific pixel
void removePositionFromBlueprint(char charIdentifier, int x, int y)
{
if (isWithinBounds(x, y))
{
int existsAt = -1;
// Checking if charIdentifier exists on the coordinates
for (int i = 0; i < positionBlueprint[x][y].size(); i++)
{
if (positionBlueprint[x][y][i] == charIdentifier)
{
existsAt = i;
break;
}
}
// Removing position from blueprint if it exists
if (existsAt != -1)
{
vector<char>::iterator it = positionBlueprint[x][y].begin();
advance(it, existsAt);
positionBlueprint[x][y].erase(it);
}
}
}
// ------------ UTILITY FUNCTIONS ----------------
// Returns true if the given coordinates are on land
bool isLand(int x, int y)
{
if (isWithinBounds(x, y))
{
Color thisPixel = terrainTextureImage.getPixel(x, y);
return thisPixel.r == landColorRGBA[0] && thisPixel.g == landColorRGBA[1] && thisPixel.b == landColorRGBA[2] && thisPixel.a == landColorRGBA[3];
}
else
{
return false;
}
}
// Returns true if the given coordinates are within the bounds of the screen
bool isWithinBounds(int x, int y)
{
return (x < width && x > 0 && y < height && y > 0);
}
// ------------ FUNCTIONS FOR RABBITS ------------------
// add a rabbit to the simulation at given position
void addRabbit(Vector2f position)
{
int rabbit_x = floor(position.x);
int rabbit_y = floor(position.y);
// Creating a new rabbit with a pointer
Rabbit *rabbit = new Rabbit((rabbitSpeedMin + ((float)(rand() % 1000) / 1000) * (rabbitSpeedMax - rabbitSpeedMin)),
Vector2f(1, 1),
Vector2f(rabbit_x, rabbit_y),
rabbitMaxHunger,
rabbitMaxThirst,
rabbitMaxReproductiveUrge);
// Adding the rabbit's position to the blueprint
addToPositionBlueprint('r', floor(position.x), floor(position.y));
rabbits.push_back(rabbit);
}
// Remove a rabbit from the simulation fromt the specific point
void removeRabbit(Vector2f position)
{
// Variable to hold index of the element to remove
int targetAt = -1;
// Getting the index
for (int i = 0; i < rabbits.size(); i++)
{
if (Vector2f((float)floor(rabbits[i]->getPosition().x), (float)floor(rabbits[i]->getPosition().y)) == Vector2f((float)floor(position.x), (float)floor(position.y)))
{
targetAt = i;
break;
}
}
// If index is valid then remove the rabbit
if (targetAt >= 0 && targetAt <= rabbits.size())
{
vector<Rabbit *>::iterator it = rabbits.begin();
removePositionFromBlueprint(rabbitCharIdentifier, rabbits[targetAt]->getPosition().x, rabbits[targetAt]->getPosition().y);
advance(it, targetAt);
rabbits.erase(it);
}
}
void initializeRabbits()
{
// Create rabbits and set them on land
for (int i = 0; i < intialNumRabbits; i++)
{
int rabbit_x;
int rabbit_y;
// Generate coordinates until they are on land
do
{
rabbit_x = rand() % width;
rabbit_y = rand() % height;
} while (!isLand(rabbit_x, rabbit_y));
addRabbit(Vector2f((float)rabbit_x, (float)rabbit_y));
}
}
// Calls update function of all existing rabbits
void updateAllRabbits()
{
for (int i = 0; i < rabbits.size(); i++)
{
rabbits[i]->update();
}
}
// Calls draw function of all existing rabbits
void drawAllRabbits(RenderWindow *window)
{
for (int i = 0; i < rabbits.size(); i++)
{
rabbits[i]->draw(window);
}
}
// ------------ FUNCTIONS FOR WOLVES ------------------
// Same as rabbits
void addWolf(Vector2f position)
{
int wolf_x = floor(position.x);
int wolf_y = floor(position.y);
// int wolf_x = floor(width / 2);
// int wolf_y = floor(height / 2);
Wolf *wolf = new Wolf((wolfSpeedMin + ((float)(rand() % 1000) / 1000) * (wolfSpeedMax - wolfSpeedMin)),