-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionalityTest
113 lines (88 loc) · 3.18 KB
/
FunctionalityTest
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
package Breakpoint;
import java.awt.Color;
import acm.graphics.*;
import acm.program.GraphicsProgram;
public class FunctionalityTest extends GraphicsProgram{
// private static final double SIZE = 5;
// AnimatedBall redBall = new AnimatedBall(SIZE);
public void run() {
setSize(Model.getWidth(), Model.getHeight());
AnimatedBall curBall = Model.getRedBall();
curBall.setFilled(true);
curBall.setColor(Color.RED);
add(curBall, Model.getBallStart());
GLabel startMessage = new GLabel("Click to start Breakout!");
startMessage.setLocation(Model.getPaddleStart().getX() - 50, Model.getPaddleStart().getY() + 25);
add(startMessage);
Thread curBallThread = new Thread(curBall);
waitForClick();
remove(startMessage);
curBallThread.start();
while(curBallThread.isAlive() && !Model.isLost() && !Model.isWon()){
println(curBall.getLocation().toString());
if(wouldCollideWith(curBall) != null){
curBall.setDirection(calculateDirection(curBall,getElementAt(curBall.getX(),curBall.getY())));
}
}
}
private Object wouldCollideWith(AnimatedBall curBall) {
double deltaX = Math.sin(curBall.getDirection()) * AnimatedBall.getDelta();
double deltaY = Math.cos(curBall.getDirection()) * AnimatedBall.getDelta();
double redX = curBall.getX();
double redY = curBall.getY();
double size = AnimatedBall.getBallSize();
Object collisionObject = null;
collisionObject = getElementAt(redX + deltaX, redY + deltaY);
if(collisionObject instanceof GCanvas){
collisionObject = getElementAt(redX + deltaX + size , redY + deltaY);
if(collisionObject instanceof GCanvas){
collisionObject = getElementAt(redX + deltaX , redY + deltaY + size);
if(collisionObject instanceof GCanvas){
collisionObject = getElementAt(redX + deltaX + size, redY + deltaY + size);
}else{
/*When all cllisionObjects were GCanvas*/
collisionObject = null;
}
}
}
return collisionObject;
}
private double calculateDirection(AnimatedBall redBall, Object elementAt) {
double newDirection = redBall.getDirection();
if(elementAt != null){
if(elementAt instanceof Brick /*|| elementAt instanceof Wall*/){
newDirection = reflect(redBall, elementAt);
}else if(elementAt instanceof Paddle){
newDirection = paddleReflect(redBall, elementAt);
// }else if(elementAt instanceof Bottom){
// Model.setLost(true);
}
}else{
assert false : "No collision object handed to calculate new direction.";
}
assert newDirection == redBall.getDirection() : "No new direction set.";
return newDirection;
}
private double paddleReflect(AnimatedBall redBall, Object elementAt) {
double newDirection;
// TODO Implement correct paddleReflection
double direction = redBall.getDirection();
newDirection = direction + 180;
return newDirection;
}
private double reflect(AnimatedBall redBall, Object elementAt) {
double newDirection;
double direction = redBall.getDirection();
int side = (int) (direction / 180);
switch (side) {
case 0:
newDirection = 180 - direction;
case 1:
newDirection = 360 - direction;
default:
assert (false): "Reflection can not be callucalted correctly";
newDirection = direction;
}
return newDirection;
}
}