-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBug.pde
97 lines (73 loc) · 1.88 KB
/
Bug.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
// This is a class of "bugs" that are sensitive to light
class Bug {
// location, velocity, and acceleration
PVector loc;
PVector vel;
PVector acc;
float repel = 1.9;
// Vector from bug to source
PVector srcVector;
// Vector describing position of head relative to loc
PVector headLoc;
boolean hiding = false;
Bug(float x, float y) {
loc = new PVector();
loc.x = x;
loc.y = y;
vel = new PVector(0,0);
acc = new PVector(0,0);
headLoc = new PVector();
}
void update(boolean inLight, int X_MIN, int X_MAX, int Y_MIN, int Y_MAX, PVector source) {
hiding = inLight;
if (inLight) {
// Hide!
acc.x = random(-1, 1);
acc.y = random(-1, 1);
// Avoid Light Source
srcVector = new PVector(source.x - loc.x, source.y - loc.y);
if (srcVector.mag() < 100) {
srcVector.setMag(50);
acc.sub(srcVector);
}
acc.setMag(0.1);
vel.add(acc);
loc.add(vel);
}
int tolerance = 15;
// Can't hide against outer walls
if (loc.x < X_MIN + tolerance) {
vel.x += repel;
loc.add(vel);
}
if (loc.x >= X_MAX - tolerance) {
vel.x -= repel;
loc.add(vel);
}
if (loc.y <= Y_MIN + tolerance) {
vel.y += repel;
loc.add(vel);
}
if (loc.y >= Y_MAX - tolerance) {
vel.y -= repel;
loc.add(vel);
}
}
void display() {
int bodyWidth = 7;
headLoc.x = vel.x;
headLoc.y = vel.y;
headLoc.setMag(0.75*bodyWidth);
headLoc.add(loc);
//fill(#D4C3E3);
fill(#333333);
noStroke();
ellipse(loc.x, loc.y, bodyWidth, bodyWidth);
ellipse(headLoc.x, headLoc.y, 0.75*bodyWidth, 0.75*bodyWidth);
if (hiding) {
fill(#CCCCCC);
textSize(12);
text("eek!", loc.x + bodyWidth, loc.y);
}
}
}