diff --git a/src/break_out/model/Ball.java b/src/break_out/model/Ball.java index 6700b3d..f674bbd 100644 --- a/src/break_out/model/Ball.java +++ b/src/break_out/model/Ball.java @@ -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; diff --git a/src/break_out/model/Level.java b/src/break_out/model/Level.java index 6a11552..9666235 100644 --- a/src/break_out/model/Level.java +++ b/src/break_out/model/Level.java @@ -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();