-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.pde
300 lines (264 loc) · 7.02 KB
/
Cell.pde
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
public class Cell
{
PVector pos;
PVector goal;
PVector vel;
PVector acc;
float accSpeed;
float speed;
float startR;
float r;
float rGoal;
float yoff = 0; //For perlin noise
int gen;
color shade;
color famShade;
float sight;
float repro; //How many times original size to reproduce
float mutChance;
boolean fleeing = false;
Cell(PVector _pos, float _r, int _gen, color _shade, color _famShade, float _accSpeed, float _speed, float _sight, float _repro)
{
pos = _pos;
newGoal();
vel = new PVector(0, 0);
acc = new PVector(0, 0);
accSpeed = constrain(_accSpeed, .05, 1);
speed = constrain(_speed, 1, 20);
r = constrain(_r, 1, 200);
startR = r;
rGoal = r;
gen = _gen;
shade = _shade;
famShade = _famShade;
sight = constrain(_sight, 1, 500);
repro = constrain(_repro, 1.2, 10);
mutChance = random(-0.15, 0.5);
}
String[] info()
{
String[] info = new String[9];
info[0] = ("X: " + this.pos.x + " Y: " + this.pos.y + "\n");
info[1] = ("Velocity X: " + vel.x + " Velocity Y: " + vel.y + "\n");
info[2] = ("Original Radius: " + Float.toString(startR) + "\n");
info[3] = ("Radius: " + Float.toString(rGoal) + "\n");
info[4] = ("Generation: " + Integer.toString(gen) + "\n");
info[5] = ("Max Speed: " + Float.toString(speed) + "\n");
info[6] = ("Sight Radius: " + Float.toString(sight) + "\n");
info[7] = ("Reproduction: " + Float.toString(repro) + "\n");
info[8] = ("Mutation: " + Float.toString(mutChance) + "\n");
return info;
}
void update()
{
move();
tele();
grow();
lookFood();
lookCells();
show();
}
void applyForce(PVector force)
{
acc.add(force);
}
void move()
{
float d = pos.dist(goal); //Distance to goal
if (d < r)
{
fleeing = false;
newGoal();
}
PVector dir = PVector.sub(goal, pos); //Figures out direction, adds to acceleration
dir.setMag(accSpeed);
applyForce(dir);
vel.add(acc);
vel.limit(speed);
pos.add(vel);
acc.mult(0);
}
void tele() //Moves cell across screen if it goes off edge
{
if (pos.x < -r)
{
pos.x = width + r;
newGoal();
}
else if (pos.x > width + r)
{
pos.x = -r;
newGoal();
}
if (pos.y < -r)
{
pos.y = height + r;
newGoal();
}
else if (pos.y > height + r)
{
pos.y = -r;
newGoal();
}
}
void grow()
{
if (r < rGoal)
{
r += .1;
}
if (r > startR * repro)
{
reproduce();
}
}
void show()
{
stroke(shade);
strokeWeight(2);
fill(shade, 200);
pushMatrix();
translate(pos.x, pos.y);
beginShape();
float xoff = 0;
for (float a = 0; a < TWO_PI; a += 0.09) //Makes cell wavy
{
float offset = map(noise(xoff, yoff), 0, 1, .01 * -r, r);
float newR = r + offset;
float x = (newR * cos(a)) - vel.x * vel.mag();
float y = (newR * sin(a)) - vel.y * vel.mag();
vertex(x, y);
xoff += 0.1;
}
endShape();
popMatrix();
yoff += .05;
noStroke();
fill(famShade);
ellipse(pos.x, pos.y, r * 1.5, r * 1.5); //Puts family color in center of cell
stroke(shade, 100);
strokeWeight(.5);
noFill();
float vision = (r + sight) * 2;
ellipse(pos.x, pos.y, vision, vision); //Draws vision radius
line(pos.x, pos.y, goal.x, goal.y); //Draws line to goal
color textCol = (brightness(famShade) > 127) ? 0 : 255;
fill(textCol);
textSize(r * 1.5);
text(gen, pos.x, pos.y); //Draws generation number
}
void lookFood()
{
float dist = sight * 2;
for (Food f : foods)
{
float d = pos.dist(f.pos);
boolean canSee = d -r - f.size - sight <= 0;
if (d < r) //If touching food
{
rGoal += f.value;
eaten.add(f);
}
else if (canSee && !fleeing)
{
if (d < dist)
{
goal = f.pos.copy();
dist = d; //Makes sure cell's goal is closest food
}
}
}
}
void lookCells()
{
ArrayList<PVector> toFlee = new ArrayList<PVector>(); //Cells to flee from
for (Cell other : cells)
{
if (other == this)
{
break;
}
float d = pos.dist(other.pos);
boolean touching = d - r - other.r <= 0;
boolean ate = false; //Used to check if cell should bounce
if (other.famShade != famShade) //If not same family
{
boolean canSee = d - r - other.r - sight <= 0;
if (canSee)
{
boolean meBigger = r > 1.5 * other.r;
boolean itBigger = other.r > 1.5 * r;
if (meBigger && touching)
{
rGoal += other.r / 10; //Eat other
toRemove.add(other);
newGoal();
ate = true;
}
else if (meBigger && !fleeing)
{
goal = other.pos.copy();
}
else if (itBigger)
{
toFlee.add(other.pos.copy());
}
}
}
if(touching && !ate)
{
PVector force = awayVector(other.pos);
force.setMag(r / 4);
applyForce(force);
other.applyForce(force.setMag(-1));
}
}
if (toFlee.size() > 0)
{
fleeing = true;
fleeGoal(toFlee);
}
}
void reproduce()
{
for (int i = 0; i < 2; i++)
{
float newX = (random(1) < .5) ? pos.x + r : pos.x - r;
float newY = (random(1) < .5) ? pos.y + r : pos.y - r;
PVector newP = new PVector(newX, newY);
float newRad = ((r / 1.2) + mutChance);
color newRGB = color(red(shade) + random(-mutChance, mutChance) * 80,
green(shade) + random(-mutChance, mutChance) * 80,
blue(shade) + random(-mutChance, mutChance) * 80);
float nAccSp = accSpeed + accSpeed * mutChance;
float nSpeed = speed + mutChance;
float nSight = sight + mutChance;
float nRepro = repro + mutChance;
babies.add(new Cell(newP, newRad, gen + 1, newRGB, famShade, nAccSp, nSpeed, nSight, nRepro));
toRemove.add(this);
}
}
void newGoal()
{
PVector v = PVector.random2D();
v.setMag(sight + r);
goal = PVector.add(pos, v);
}
void fleeGoal(ArrayList<PVector> flee)
{
PVector v = new PVector(0, 0);
for (PVector vec : flee)
{
v.add(vec);
}
v.div(flee.size());
PVector dir = awayVector(v);
dir.setMag(sight + r);
goal = PVector.add(dir, pos);
}
PVector awayVector(PVector other)
{
PVector v = PVector.sub(pos, other);
return v;
}
}