2019-11-19 00:55:13 +00:00
|
|
|
package break_out.model;
|
|
|
|
|
|
|
|
|
2019-12-01 17:31:50 +00:00
|
|
|
import break_out.Constants;
|
|
|
|
|
2019-11-19 00:55:13 +00:00
|
|
|
/**
|
|
|
|
* This class contains information about the running game
|
2019-12-01 17:31:50 +00:00
|
|
|
*
|
2019-11-19 00:55:13 +00:00
|
|
|
* @author dmlux
|
2019-12-07 23:15:20 +00:00
|
|
|
* @author I. Schumacher; modified by Gruppe 175 (Moritz Henseleit, Ruben Meyer)
|
2019-11-19 00:55:13 +00:00
|
|
|
*/
|
2019-12-01 17:31:50 +00:00
|
|
|
public class Level extends Thread implements ILevel {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The game to which the level belongs
|
|
|
|
*/
|
|
|
|
private Game game;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of the level
|
|
|
|
*/
|
|
|
|
private int levelnr;
|
|
|
|
|
|
|
|
/**
|
2019-11-19 00:55:13 +00:00
|
|
|
* The score of the level
|
|
|
|
*/
|
2019-12-01 17:31:50 +00:00
|
|
|
private int score;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ball of the level
|
|
|
|
*/
|
|
|
|
private Ball ball;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag that shows if the ball was started
|
|
|
|
*/
|
2019-12-17 19:38:47 +00:00
|
|
|
private boolean ballWasStarted = false;
|
2019-12-01 17:31:50 +00:00
|
|
|
|
2019-12-17 21:15:00 +00:00
|
|
|
/**
|
|
|
|
* Flag that shows if the level was finished
|
|
|
|
*/
|
|
|
|
private boolean levelFinished = false;
|
|
|
|
|
2019-12-01 17:31:50 +00:00
|
|
|
/**
|
|
|
|
* The paddles of the level
|
|
|
|
*/
|
|
|
|
private Paddle paddleTop, paddleBottom;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The constructor creates a new level object and needs the current game object,
|
|
|
|
* the number of the level to be created and the current score
|
|
|
|
*
|
|
|
|
* @param game The game object
|
|
|
|
* @param levelnr The number of the new level object
|
|
|
|
* @param score The score
|
|
|
|
*/
|
|
|
|
public Level(Game game, int levelnr, int score) {
|
|
|
|
this.game = game;
|
|
|
|
this.levelnr = levelnr;
|
|
|
|
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);
|
|
|
|
|
2019-12-12 18:20:52 +00:00
|
|
|
// set paddles color
|
|
|
|
this.paddleTop.setColor(Constants.COLOR_PADDLE_TOP);
|
|
|
|
this.paddleBottom.setColor(Constants.COLOR_PADDLE_BOTTOM);
|
|
|
|
|
2019-12-01 17:31:50 +00:00
|
|
|
loadLevelData(levelnr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The getter for the ball object
|
|
|
|
*
|
|
|
|
* @return ball The ball of the level
|
|
|
|
*/
|
|
|
|
public Ball getBall() {
|
|
|
|
return this.ball;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets ballWasStarted to true, the ball is moving
|
|
|
|
*/
|
|
|
|
public void startBall() {
|
|
|
|
ballWasStarted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets ballWasStarted to false, the ball is stopped
|
|
|
|
*/
|
|
|
|
public void stopBall() {
|
|
|
|
ballWasStarted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if the ball is moving or stopped
|
|
|
|
*
|
|
|
|
* @return ballWasStarted True: the ball is moving; false: the ball is stopped
|
|
|
|
*/
|
|
|
|
public boolean ballWasStarted() {
|
|
|
|
return ballWasStarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The method of the level thread
|
|
|
|
*/
|
|
|
|
public void run() {
|
|
|
|
game.notifyObservers();
|
|
|
|
|
|
|
|
// endless loop
|
2019-12-17 21:15:00 +00:00
|
|
|
while (!levelFinished) {
|
2019-12-01 17:31:50 +00:00
|
|
|
// if ballWasStarted is true, the ball is moving
|
2019-12-17 20:26:34 +00:00
|
|
|
if (ballWasStarted()) {
|
2019-12-01 17:31:50 +00:00
|
|
|
|
|
|
|
// Call here the balls method for updating his position on the playground
|
2019-12-17 20:26:34 +00:00
|
|
|
getBall().updatePosition();
|
2019-12-01 17:31:50 +00:00
|
|
|
|
|
|
|
// Call here the balls method for reacting on the borders of the playground
|
2019-12-17 20:26:34 +00:00
|
|
|
getBall().reactOnBorder();
|
2019-12-01 17:31:50 +00:00
|
|
|
|
2019-12-02 23:03:36 +00:00
|
|
|
// if ball hits paddle (top|bottom), reflect ball
|
2019-12-17 20:26:34 +00:00
|
|
|
if(getBall().hitsPaddle(paddleTop)) getBall().reflectOnPaddle(paddleTop);
|
|
|
|
if(getBall().hitsPaddle(paddleBottom)) getBall().reflectOnPaddle(paddleBottom);
|
|
|
|
if(getBall().hitsPaddle(paddleTop) || getBall().hitsPaddle(paddleBottom)) {
|
|
|
|
getBall().setHitState(true);
|
2019-12-13 00:14:58 +00:00
|
|
|
// 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
|
2019-12-17 20:26:34 +00:00
|
|
|
else if(getBall().getHitState()) {
|
|
|
|
getBall().setHitState(false);
|
|
|
|
getBall().newRandomColor();
|
2019-12-13 00:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 20:26:34 +00:00
|
|
|
// update paddles position
|
|
|
|
getPaddleTop().updatePosition(getBall());
|
|
|
|
getPaddleBottom().updatePosition(getBall());
|
2019-12-02 23:03:36 +00:00
|
|
|
|
2019-12-01 17:31:50 +00:00
|
|
|
|
|
|
|
// Tells the observer to repaint the components on the playground
|
|
|
|
game.notifyObservers();
|
|
|
|
|
|
|
|
}
|
|
|
|
// The thread pauses for a short time
|
|
|
|
try {
|
|
|
|
Thread.sleep(4);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the information for the level from a json-file located in the folder /res of the project
|
|
|
|
*
|
|
|
|
* @param levelnr The number X for the LevelX.json file
|
|
|
|
*/
|
|
|
|
private void loadLevelData(int levelnr) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The getter for the top paddle object
|
|
|
|
*
|
|
|
|
* @return paddleTop The top paddle of the level
|
|
|
|
*/
|
|
|
|
public Paddle getPaddleTop() {
|
|
|
|
return paddleTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The getter for the bottom paddle object
|
|
|
|
*
|
|
|
|
* @return paddleBottom The bottom paddle of the level
|
|
|
|
*/
|
|
|
|
public Paddle getPaddleBottom() {
|
|
|
|
return paddleBottom;
|
|
|
|
}
|
|
|
|
|
2019-12-17 17:10:30 +00:00
|
|
|
/**
|
|
|
|
* The setter for the levels game state
|
|
|
|
* @param finished game state
|
|
|
|
*/
|
|
|
|
public void setFinished(boolean finished) {
|
2019-12-17 21:15:00 +00:00
|
|
|
this.levelFinished = finished;
|
2019-12-17 17:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 00:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|