-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit.pde
182 lines (134 loc) · 3.6 KB
/
Unit.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
class MovingObject{
PVector pos, v, home;
boolean locked;
float pathlen;
//float angle;
}
float repulsion_k=0.2;
class Unit extends MovingObject{
float size=1.5;
float immune_power; //gaussain 0..1 and 1 is more probable; green channel
//float infected_cells=0;
boolean infected=false;
boolean recovered=false;
int infected_time=0;
int time=0;
void setInfected(boolean i){
this.infected=i;
}
Unit( ){
immune_power = abs(randomGaussian());
pos = new PVector();
v = new PVector();
time=1+(int)random(130);
}
void updateImmune(Pandemia model){
if (infected){
infected_time++;
}
if (infected_time > model.recovery_time){
infected = false;
recovered = true;
}
}
boolean isInfected(){
return this.infected;
}
void updateVelocity(Unit[] all, Pandemia model){
updateImmune(model);
for(Unit other : all){
if(other != this ){
PVector d = PVector.sub(other.pos, pos);
float distanceSq = d.magSq();
if (other.isInfected() && !this.recovered){
float infection_probabilty = 0.7/(0.1 + distanceSq);
if(random(model.infection_distance) < infection_probabilty)
this.setInfected(true);
//if
//if(distance < model.infection_distance ){
// this.setInfected(!recovered);
//}
}
//mutual repulsion
if(distanceSq < size*size*18){
d.mult(0.09*repulsion_k / (0.001+distanceSq));
v.sub(d);
other.v.add(d);
}
}
}
//EACH OTHER mean stream; Frame dragging
for(Unit l:all){
if(l!=this ){
float d = pos.dist(l.pos);
if (d<model.range.x/8){
float k = 2.9 * 1.0/(1.0+d*d*d);
v.add (l.v.copy().mult(k) );
}
}
}
// to GRID
PVector d = PVector.sub(home, pos);
//if(d.mag()<1){
// this.locked=true;
//}else{
// this.locked=false;
//}
d.mult(0.0007);
//if(infected())
// d.mult(1.5);
v.add(d);
if(v.mag()>10){
v.setMag(10);
}
}
void move(float al){
float max_speed=0.6*size;
//if(!isAlive() )
// return;
float dempfer=1;
//if(this.locked)
// dempfer=0.1;
PVector addon = v.copy();//.setMag(1);
addon.mult(0.2*(immune_power+0.1) * al * dempfer);
//float step_len = addon.mag();
//if(step_len>max_speed){
// step_len=max_speed;
// addon.setMag(step_len);
//}
pos.add( addon);
pathlen += addon.mag();
//if(infected){
// v.mult(0.85); //innertia
//}else
}
boolean isAlive(){
return immune_power>0.1;
}
void decay(Pandemia model){
if(isInfected())
v.mult(0.7);
v.mult(0.970883); //innertia
//if( this.time % 100 == 1){
// model.putToken(pos);
//}
//this.time++;
//if(((int)pathlen) % 106 == 1)
if(random(1)<0.001)
model.putToken(pos.copy());
}
void draw(float al, Pandemia model){
float g = isInfected()? 12: 180 + v.y*10; //immune_power*255;
float r = 20 + 240*((float)infected_time/(float)model.recovery_time);// isInfected() ;
r*=0.01+(sin(pathlen/100.)+1)*0.4;
float b =15 * v.mag() + v.x*20;
pushStyle();
fill(r, g, b, al*170);
pushMatrix();
translate( pos.x , pos.y);
noStroke();
ellipse( 0,0, size, size);
popMatrix();
popStyle();
}
}