diff --git a/src/break_out/model/Ball.java b/src/break_out/model/Ball.java index 7073ce8..913fc39 100644 --- a/src/break_out/model/Ball.java +++ b/src/break_out/model/Ball.java @@ -52,9 +52,8 @@ public class Ball implements IBall { this.direction = new Vector2D(Constants.BALL_SPEED, Constants.BALL_SPEED); this.direction.rescale(); - // start at bottom-center - this.position.setX((Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER) / 2.0); - this.position.setY(Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER - Constants.PADDLE_HEIGHT); + // reset position to bottom-center + resetPosition(); } /** @@ -388,4 +387,13 @@ public class Ball implements IBall { isLost = lost; } + /** + * resets balls position + */ + public void resetPosition() { + // start at bottom-center + position.setX((Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER) / 2.0); + position.setY(Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER - Constants.PADDLE_HEIGHT); + } + } diff --git a/src/break_out/model/Level.java b/src/break_out/model/Level.java index f0c81af..5df6467 100644 --- a/src/break_out/model/Level.java +++ b/src/break_out/model/Level.java @@ -73,13 +73,7 @@ public class Level extends Thread implements ILevel { this.score = score; this.ball = new Ball(); - // calc paddle positions - Position posPaddleTop = new Position((Constants.SCREEN_WIDTH - Constants.PADDLE_WIDTH) / 2.0, 0); - Position posPaddleBottom = new Position((Constants.SCREEN_WIDTH - Constants.PADDLE_WIDTH) / 2.0, Constants.SCREEN_HEIGHT - Constants.PADDLE_HEIGHT); - - // set paddles - this.paddleTop = new Paddle(posPaddleTop); - this.paddleBottom = new Paddle(posPaddleBottom); + resetPaddles(); // set paddles color this.paddleTop.setColor(Constants.COLOR_PADDLE_TOP); @@ -137,6 +131,11 @@ public class Level extends Thread implements ILevel { // Call here the balls method for reacting on the borders of the playground getBall().reactOnBorder(); + // Call here the balls method for reacting on lost ball state + if(getBall().isLost()) { + decreaseLives(); + } + // if ball hits paddle (top|bottom), reflect ball if(getBall().hitsPaddle(paddleTop)) getBall().reflectOnPaddle(paddleTop); if(getBall().hitsPaddle(paddleBottom)) getBall().reflectOnPaddle(paddleBottom); @@ -282,6 +281,57 @@ public class Level extends Thread implements ILevel { private boolean allStonesBroken() { return stones.isEmpty(); } + + /** + * decreases the lives and interact to the new count + */ + private void decreaseLives() { + lifeCounter--; + // reset paddles to center pos + if(lifeCounter > 0) { + resetPaddles(); + getBall().resetPosition(); + getBall().setLost(false); + stopBall(); + // level failed + } else { + setFinished(true); + game.getController().toStartScreen(); + } + } + + /** + * resets paddles position and or init them + */ + private void resetPaddles() { + // calc paddle positions + Position posPaddleTop = new Position((Constants.SCREEN_WIDTH - Constants.PADDLE_WIDTH) / 2.0, 0); + Position posPaddleBottom = new Position((Constants.SCREEN_WIDTH - Constants.PADDLE_WIDTH) / 2.0, Constants.SCREEN_HEIGHT - Constants.PADDLE_HEIGHT); + + // resets top paddle + if(paddleTop == null) paddleTop = new Paddle(posPaddleTop); + else paddleTop.setPosition(posPaddleTop); + + // resets bottom paddle + if(paddleBottom == null) paddleBottom = new Paddle(posPaddleBottom); + else paddleBottom.setPosition(posPaddleBottom); + } + + /** + * The getter for levels score + * @return the score + */ + public int getScore() { + return score; + } + + /** + * The getter for levels lives + * @return the life counter + */ + public int getLives() { + return lifeCounter; + } }