-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticleManager.pde
89 lines (72 loc) · 2.2 KB
/
ParticleManager.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
/**
*A class to manage various particles that fly in space
*
*/
class ParticleManager implements Effect {
private float horizontalParticleSpeed = -20;
private float maxAbsVerticalSpeed = 5;
private float maxHorizontalSpeedDispersion = 3;
private List<PVector> particlePositions =
new LinkedList<PVector>();
private List<PVector> particleSpeeds =
new LinkedList<PVector>();
public ParticleManager() {
}
private void addParticle() {
particlePositions.add(
new PVector(random(sketchWidth()), random(sketchHeight()))
);
float hsp = horizontalParticleSpeed +
random (maxHorizontalSpeedDispersion) -
0.5 * maxHorizontalSpeedDispersion;
float vsp = 0 +
random (maxAbsVerticalSpeed) -
0.5 * maxAbsVerticalSpeed;
particleSpeeds.add(new PVector(hsp, vsp));
}
private void moveParticles() {
ListIterator<PVector> iterpos = particlePositions.listIterator();
ListIterator<PVector> itersp = particleSpeeds.listIterator();
while (iterpos.hasNext ()) {
iterpos.next().add(itersp.next());
}
}
private void removeInvisibleParticles() {
ListIterator<PVector> iterpos = particlePositions.listIterator();
ListIterator<PVector> itersp = particleSpeeds.listIterator();
while (iterpos.hasNext ()) {
PVector pos = iterpos.next();
itersp.next();
if (pos.x < 0 || pos.x > sketchWidth() ||
pos.y < 0 || pos.y > sketchHeight()) {
iterpos.remove();
itersp.remove();
}
}
}
public void manage() {
removeInvisibleParticles();
moveParticles();
if (frameCount % 3 == 0) {
addParticle();
};
}
public void move(final PVector translation) {
ListIterator<PVector> iter = particlePositions.listIterator();
while (iter.hasNext ()) {
iter.next().add(translation);
}
}
public void draw() {
ListIterator<PVector> iter = particlePositions.listIterator();
while (iter.hasNext ()) {
PVector pos = iter.next();
for (int offset = 0; offset < 20; offset += 1) {
int alpha = 255 - 7*offset;
fill(255, 255, 255, alpha);
stroke(255, 255, 255, alpha);
rect(pos.x+offset*5, pos.y, 2, 2);
}
}
}
};