-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsteroidSystem.pde
45 lines (44 loc) · 1.02 KB
/
AsteroidSystem.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
import java.util.Iterator;
class AsteroidSystem {
ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();
int count;
AsteroidSystem(int count_) {
count = count_;
for (int i=0; i<count; i++) {
asteroids.add(new Asteroid());
}
checkStart();
}
void run() {
for (Iterator<Asteroid> it = asteroids.iterator(); it.hasNext(); ) {
Asteroid a = it.next();
a.run();
}
checkCollisions();
}
void checkCollisions() {
Asteroid b1, b2;
for (int i=0; i<count; i++) {
for (int j=i+1; j<count; j++) {
b1 = asteroids.get(i);
b2 = asteroids.get(j);
if (b1.intersects(b2)) {
b1.collide(b2);
}
}
}
}
void checkStart() {
for (Iterator<Asteroid> it = asteroids.iterator(); it.hasNext(); ) {
Asteroid a1 = it.next();
for(int i=0;i<count;i++) {
Asteroid a2 = asteroids.get(i);
if(a1!=a2 && a1.intersects(a2)) {
it.remove();
count--;
break;
}
}
}
}
}