Aufgabe 2.2 - hitsPaddle(Paddle p) - tests whether the ball touches the paddle's hit box.
This commit is contained in:
parent
dcc68e837a
commit
78159521c7
@ -93,17 +93,51 @@ public class Ball implements IBall {
|
||||
}
|
||||
|
||||
/**
|
||||
* test if Ball hits paddle
|
||||
* tests whether the ball touches the paddle's hit box.
|
||||
* @param paddle paddle which will be tested
|
||||
* @return true when ball hits paddle
|
||||
* @return true when ball hits the paddle
|
||||
*/
|
||||
public boolean hitsPaddle(Paddle paddle) {
|
||||
// paddles position
|
||||
Position posPaddle = paddle.getPosition();
|
||||
|
||||
// balls position
|
||||
Position posBall = this.getPosition();
|
||||
|
||||
// test balls y position against paddles y values
|
||||
boolean testPaddleY = (
|
||||
(posBall.getY() <= posPaddle.getY()+paddle.getHeight() && posBall.getY() >= posPaddle.getY())
|
||||
|| (posBall.getY()+Constants.BALL_DIAMETER >= posPaddle.getY() && posBall.getY() <= posPaddle.getY()+paddle.getHeight())
|
||||
);
|
||||
|
||||
// test balls x position against paddles x values
|
||||
boolean testPaddleX = (
|
||||
posBall.getX() >= posPaddle.getX() && posBall.getX() <= posPaddle.getX()+paddle.getWidth()
|
||||
);
|
||||
|
||||
// if balls y position is in paddles y values, verify x position
|
||||
if(testPaddleY) {
|
||||
// DEBUG OUTPUT
|
||||
//System.out.println("ball is in y area of paddle: "+String.format("x, y, w, h: %s, %s, %s, %s", posPaddle.getX(), posPaddle.getY(), paddle.getWidth(), paddle.getHeight()));
|
||||
|
||||
// if ball is in paddles hit box
|
||||
if(testPaddleX) {
|
||||
// DEBUG OUTPUT
|
||||
//System.out.println("ball hits paddle: "+String.format("x, y, w, h: %s, %s, %s, %s", posPaddle.getX(), posPaddle.getY(), paddle.getWidth(), paddle.getHeight()));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// default output, ball doesn't hit paddle
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ball got hit by Paddle paddle
|
||||
* @param paddle hitbox mechanism of paddle
|
||||
* @todo implementation
|
||||
*/
|
||||
public void reflectOnPaddle(Paddle paddle) {
|
||||
return;
|
||||
|
@ -116,6 +116,10 @@ public class Level extends Thread implements ILevel {
|
||||
// Call here the balls method for reacting on the borders of the playground
|
||||
this.ball.reactOnBorder();
|
||||
|
||||
// if ball hits paddle (top|bottom), reflect ball
|
||||
if(this.ball.hitsPaddle(paddleTop)) this.ball.reflectOnPaddle(paddleTop);
|
||||
if(this.ball.hitsPaddle(paddleBottom)) this.ball.reflectOnPaddle(paddleBottom);
|
||||
|
||||
|
||||
// Tells the observer to repaint the components on the playground
|
||||
game.notifyObservers();
|
||||
|
Loading…
x
Reference in New Issue
Block a user