-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperson.js
104 lines (102 loc) · 3.12 KB
/
person.js
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
class Person {
constructor(isSick = false) {
this.pos = createVector(random(width), random(height));
this.vel = p5.Vector.random2D();
this.isDead = false;
this.isSick = isSick;
this.immune = false;
this.sickTime = 60;
}
update() {
if(!this.isDead) {
this.pos.add(this.vel);
if(this.sickTime <= 0) {
this.isSick = false;
this.sickTime = 0;
this.getWell();
}
if(this.isSick) {
this.sickTime -= 0.02; // TODO: Improve
if(!this.isDead && floor(random(10000)) == 1)
this.die(); // Imagine havind die() function inside you xD
}
}
}
emitInfection(people) {
if((this.isSick || this.immune) && !this.isDead) {
for(let i = 0; i < people.length; i++) {
let other = people[i];
let d = dist(this.pos.x, this.pos.y, other.pos.x, other.pos.y);
if(d < infectionRadius) {
if(this.isSick) {
let r = floor(random(100));
if(r == 1)
other.getSick();
}
else if(this.immune) {
if(other.isSick)
other.sickTime -= 0.1;
else {
let r = floor(random(100));
if(r == 1)
other.getWell();
}
}
}
}
}
}
edge() {
if(this.pos.x > width) this.pos.x = 0;
if(this.pos.x < 0) this.pos.x = width;
if(this.pos.y > height) this.pos.y = 0;
if(this.pos.y < 0) this.pos.y = height;
}
die() {
this.immune = false;
this.isSick = false;
this.isDead = true;
}
show() {
if(!this.isDead) {
stroke(255);
noFill();
strokeWeight(4);
point(this.pos.x, this.pos.y);
if(this.isSick) {
stroke(255, 0, 0);
strokeWeight(1);
circle(this.pos.x, this.pos.y, infectionRadius * 2);
}
if(this.immune) {
stroke(0, 255, 0);
strokeWeight(1);
circle(this.pos.x, this.pos.y, infectionRadius * 2);
}
} else {
stroke(100);
noFill();
strokeWeight(4);
point(this.pos.x, this.pos.y);
}
}
getSick() {
if(!this.immune && !this.isDead) {
if(!this.isSick) {
this.isSick = true;
this.sickTime = 60;
} else {
this.sickTime += 0.1;
}
}
}
getWell() {
if(!this.isDead) {
this.isSick = false;
this.immune = true;
}
}
copy() {
return new Person(this.isSick);
}
}