-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollisionGUI.java
130 lines (116 loc) · 3.05 KB
/
CollisionGUI.java
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
import java.awt.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
/**
* Using a quadtree for collision detection
*
* @author Chris Bailey-Kellogg, Dartmouth CS 10, Spring 2015
* @author CBK, Spring 2016, updated for blobs
* @author CBK, Fall 2016, using generic PointQuadtree
*/
public class CollisionGUI extends DrawingGUI {
private static final int width=800, height=600; // size of the universe
private List<Blob> blobs; // all the blobs
private List<Blob> colliders; // the blobs who collided at this step
private char blobType = 'b'; // what type of blob to create
private char collisionHandler = 'c'; // when there's a collision, 'c'olor them, or 'd'estroy them
private int delay = 100; // timer control
public CollisionGUI() {
super("super-collider", width, height);
blobs = new ArrayList<Blob>();
// Timer drives the animation.
startTimer();
}
/**
* Adds an blob of the current blobType at the location
*/
private void add(int x, int y) {
if (blobType=='b') {
blobs.add(new Bouncer(x,y,width,height));
}
else if (blobType=='w') {
blobs.add(new Wanderer(x,y));
}
else {
System.err.println("Unknown blob type "+blobType);
}
}
/**
* DrawingGUI method, here creating a new blob
*/
public void handleMousePress(int x, int y) {
add(x,y);
repaint();
}
/**
* DrawingGUI method
*/
public void handleKeyPress(char k) {
if (k == 'f') { // faster
if (delay>1) delay /= 2;
setTimerDelay(delay);
System.out.println("delay:"+delay);
}
else if (k == 's') { // slower
delay *= 2;
setTimerDelay(delay);
System.out.println("delay:"+delay);
}
else if (k == 'r') { // add some new blobs at random positions
for (int i=0; i<10; i++) {
add((int)(width*Math.random()), (int)(height*Math.random()));
repaint();
}
}
else if (k == 'c' || k == 'd') { // control how collisions are handled
collisionHandler = k;
System.out.println("collision:"+k);
}
else { // set the type for new blobs
blobType = k;
}
}
/**
* DrawingGUI method, here drawing all the blobs and then re-drawing the colliders in red
*/
public void draw(Graphics g) {
// TODO: YOUR CODE HERE
// Ask all the blobs to draw themselves.
// Ask the colliders to draw themselves in red.
}
/**
* Sets colliders to include all blobs in contact with another blob
*/
private void findColliders() {
// TODO: YOUR CODE HERE
// Create the tree
// For each blob, see if anybody else collided with it
}
/**
* DrawingGUI method, here moving all the blobs and checking for collisions
*/
public void handleTimer() {
// Ask all the blobs to move themselves.
for (Blob blob : blobs) {
blob.step();
}
// Check for collisions
if (blobs.size() > 0) {
findColliders();
if (collisionHandler=='d') {
blobs.removeAll(colliders);
colliders = null;
}
}
// Now update the drawing
repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CollisionGUI();
}
});
}
}