diff --git a/src/break_out/controller/Controller.java b/src/break_out/controller/Controller.java index 6a50979..dcbea7b 100644 --- a/src/break_out/controller/Controller.java +++ b/src/break_out/controller/Controller.java @@ -120,6 +120,18 @@ public class Controller implements ActionListener, KeyListener { if(!game.getLevel().ballWasStarted()) game.getLevel().startBall(); else game.getLevel().stopBall(); break; + // arrow facing left was pressed + case KeyEvent.VK_LEFT: + // paddle shall go left + game.getLevel().getPaddleBottom().setDirection(-1); + game.getLevel().getPaddleTop().setDirection(-1); + break; + // arrow facing right was pressed + case KeyEvent.VK_RIGHT: + // paddle shall go right + game.getLevel().getPaddleBottom().setDirection(+1); + game.getLevel().getPaddleTop().setDirection(+1); + break; } } @@ -131,7 +143,14 @@ public class Controller implements ActionListener, KeyListener { */ @Override public void keyReleased(KeyEvent e) { - + switch(e.getKeyCode()) { + case KeyEvent.VK_LEFT: // arrow facing left was released + case KeyEvent.VK_RIGHT: // arrow facing right was released + // paddle shall stop + game.getLevel().getPaddleBottom().setDirection(0); + game.getLevel().getPaddleTop().setDirection(0); + break; + } } /** diff --git a/src/break_out/model/Level.java b/src/break_out/model/Level.java index 3754d7f..9dce4ba 100644 --- a/src/break_out/model/Level.java +++ b/src/break_out/model/Level.java @@ -111,30 +111,32 @@ public class Level extends Thread implements ILevel { // endless loop while (true) { // if ballWasStarted is true, the ball is moving - if (ballWasStarted) { + if (ballWasStarted()) { // Call here the balls method for updating his position on the playground - this.ball.updatePosition(); + getBall().updatePosition(); // Call here the balls method for reacting on the borders of the playground - this.ball.reactOnBorder(); + getBall().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); - if(this.ball.hitsPaddle(paddleTop) || this.ball.hitsPaddle(paddleBottom)) { - this.ball.setHitState(true); + if(getBall().hitsPaddle(paddleTop)) getBall().reflectOnPaddle(paddleTop); + if(getBall().hitsPaddle(paddleBottom)) getBall().reflectOnPaddle(paddleBottom); + if(getBall().hitsPaddle(paddleTop) || getBall().hitsPaddle(paddleBottom)) { + getBall().setHitState(true); // DEBUG OUTPUT //System.out.println(String.format("hitstate: %s, color: %s", this.ball.getHitState(), this.ball.getColor().getRGB())); } // if ball has hit a paddle set a new random color to it - else if(this.ball.getHitState()) { - this.ball.setHitState(false); - this.ball.newRandomColor(); + else if(getBall().getHitState()) { + getBall().setHitState(false); + getBall().newRandomColor(); } - + // update paddles position + getPaddleTop().updatePosition(getBall()); + getPaddleBottom().updatePosition(getBall()); // Tells the observer to repaint the components on the playground diff --git a/src/break_out/model/Paddle.java b/src/break_out/model/Paddle.java index f40b711..92d1ec1 100644 --- a/src/break_out/model/Paddle.java +++ b/src/break_out/model/Paddle.java @@ -16,6 +16,12 @@ public class Paddle implements IPaddle { */ private Position position; + /** + * The paddles direction on the playground + * only -1,0,+1 are valid values + */ + private int direction; + /** * The paddles sizing */ @@ -121,7 +127,7 @@ public class Paddle implements IPaddle { * @return direction The paddles current direction */ public int getDirection() { - return 0; + return direction; } /** @@ -130,7 +136,11 @@ public class Paddle implements IPaddle { * @param direction The paddles new direction */ public void setDirection(int direction) { + // normalize to valid values + if(direction > 0) direction = 1; + if(direction < 0) direction = -1; + this.direction = direction; } /** @@ -139,6 +149,22 @@ public class Paddle implements IPaddle { * @param ball The ball */ public void updatePosition(Ball ball) { + // move paddle, if it is the nearest paddle + if(ball.getPosition().getY() < Constants.SCREEN_HEIGHT/2.0 && getPosition().getY() < Constants.SCREEN_HEIGHT/2.0 || + ball.getPosition().getY() > Constants.SCREEN_HEIGHT/2.0 && getPosition().getY() > Constants.SCREEN_HEIGHT/2.0) { + // temporary position to modify x value + Position tmp = getPosition(); + + // modify x value; getDirection is normalized to the values {-1, 0, 1} + // therefore we can just multiply it with DX_MOVEMENT + tmp.setX(getPosition().getX() + getDirection()*Constants.DX_MOVEMENT); + + // out of border boundaries + if(tmp.getX() <= 0) tmp.setX(0); + if(tmp.getX() >= Constants.SCREEN_WIDTH-getWidth()) tmp.setX(Constants.SCREEN_WIDTH-getWidth()); + + setPosition(tmp); + } } }