-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
169 lines (135 loc) · 4.22 KB
/
game.cpp
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
#include "headers/utils.h"
#include "headers/boids.h"
#include "headers/colors.h"
typedef struct Nest{
Vector2 pos;
double radius;
bool active;
} Nest;
std::vector<boid> boids_group;
std::vector<boid> preds;
std::vector<Nest> nests;
int number_of_boids = 1;
int number_of_nests = 1000;
int number_of_pred = 10;
Vector2 center_of_mass;
void new_boid(int W, int H, Camera2D camera){
Vector2 pos = {(float)GetRandomValue(0, W), (float)GetRandomValue(0, H)};
Vector2 vel = {(float)GetRandomValue(0, 5), (float)GetRandomValue(0, 5)};
pos = GetScreenToWorld2D(pos, camera);
setMag(vel, MAX_SPEED);
boids_group.push_back(boid(pos, vel, {0,0}, 8));
}
void new_pred(int W, int H, Camera2D camera){
Vector2 pos = {(float)GetRandomValue(-10*W, 10*W), (float)GetRandomValue(-10*H, 10*H)};
Vector2 vel = {(float)GetRandomValue(0, 2), (float)GetRandomValue(0, 2)};
pos = GetScreenToWorld2D(pos, camera);
setMag(vel, MAX_SPEED);
preds.push_back(boid(pos, vel, {0,0}, 8));
}
void new_boid2(Vector2 pos, Camera2D camera){
Vector2 vel = {(float)GetRandomValue(0, 5), (float)GetRandomValue(0, 5)};
setMag(vel, MAX_SPEED);
boids_group.push_back(boid(pos, vel, {0,0}, 8));
number_of_boids++;
}
void game_init(int W, int H, Camera2D camera){
// Init boids
for(int i = 0; i < number_of_boids; i++){
new_boid(W, H, camera);
}
for(int i = 0; i < number_of_pred; i++){
new_pred(W, H, camera);
}
int g = 5;
for(int i = 0; i < number_of_nests; i++){
Nest n;
n.pos = {(float)GetRandomValue(-W*g, W*g), (float)GetRandomValue(-H*g, H*g)};
n.pos = GetScreenToWorld2D(n.pos, camera);
n.radius = GetRandomValue(6, 30);
n.active = true;
nests.push_back(n);
}
}
double call_radius = 0;
bool calling = false;
double init_calling_time = 0;
double time_of_calling = 1;
bool loopa = false;
int update_loop(Rectangle box, Vector2 target, int W, int H, Camera2D camera){
bool state = (IsKeyPressed(KEY_SPACE) || IsMouseButtonDown(0) || IsMouseButtonDown(1));
for(auto &p : preds){
p.update(preds, target, state);
}
for(auto &boid : boids_group){
for(auto &p : preds){
if(dist(p.pos, boid.pos) < 15 && boid.visible == true){
boid.visible = false;
number_of_boids--;
}
}
boid.update(boids_group, target);
}
if(IsKeyPressed(KEY_SPACE) && calling == false){
calling = true;
loopa = true;
init_calling_time = GetTime();
}
if(calling == true){
if(GetTime() - init_calling_time > time_of_calling){
calling = false;
call_radius = 0;
}
else{
call_radius += 5;
}
}
if(loopa == true){
for(auto &nest : nests){
if(nest.active == true){
float nest_dist = dist(nest.pos, target);
int is_there = GetRandomValue(0, 100);
if( (nest_dist < call_radius) && (is_there > 20) ){
new_boid2(nest.pos, camera);
nest.active = false;
}
}
}
}
return number_of_boids;
}
void draw_loop(Vector2 target){
Vector2 dis = {6, 6};
for(auto &boid : boids_group){
Vector2 head = boid.pos - target;
if(IsMouseButtonDown(1)) head = head*(-1);
setMag(head, boid.radius);
float theta = 60;
Vector2 tail1 = Vector2Rotate(head, theta);
Vector2 tail2 = Vector2Rotate(head, 127);
if(boid.visible){
DrawTriangle(head + boid.pos + dis, tail1 + boid.pos + dis, tail2 + boid.pos + dis, d4);
DrawTriangle(head + boid.pos, tail1 + boid.pos, tail2 + boid.pos, l1);
}
}
for(auto &p : preds){
Vector2 head = p.pos - target;
if(IsMouseButtonDown(1)) head = head*(-1);
setMag(head, 2*p.radius);
float theta = 60;
Vector2 tail1 = Vector2Rotate(head, theta);
Vector2 tail2 = Vector2Rotate(head, 127);
DrawTriangle(head + p.pos + dis, tail1 + p.pos + dis, tail2 + p.pos + dis, d4);
DrawTriangle(head + p.pos, tail1 + p.pos, tail2 + p.pos, l3);
}
for(auto &nest : nests){
DrawCircleV(nest.pos + dis, nest.radius, d4);
if(nest.active == true) DrawCircleV(nest.pos, nest.radius, l2);
else{
DrawCircleV(nest.pos, nest.radius, d2);
}
}
if(calling){
DrawCircleLines(target.x, target.y, call_radius, l1);
}
}