171 lines
3.4 KiB
Java
171 lines
3.4 KiB
Java
package break_out.model;
|
|
|
|
import break_out.Constants;
|
|
|
|
import java.awt.*;
|
|
|
|
/**
|
|
* This class contains the information about the paddles characteristics and behavior
|
|
*
|
|
* @author Gruppe 175: Moritz Henseleit, Ruben Meyer
|
|
*/
|
|
public class Paddle implements IPaddle {
|
|
|
|
/**
|
|
* The paddles position on the playground
|
|
*/
|
|
private Position position;
|
|
|
|
/**
|
|
* The paddles direction on the playground
|
|
* only -1,0,+1 are valid values
|
|
*/
|
|
private int direction;
|
|
|
|
/**
|
|
* The paddles sizing
|
|
*/
|
|
private int width;
|
|
private int height;
|
|
|
|
/**
|
|
* The paddles color
|
|
*/
|
|
private Color color;
|
|
|
|
/**
|
|
* The constructor of a paddle
|
|
*
|
|
* @param position paddles initial position
|
|
*/
|
|
public Paddle(Position position) {
|
|
this.position = position;
|
|
|
|
// set sizing
|
|
width = Constants.PADDLE_WIDTH;
|
|
height = Constants.PADDLE_HEIGHT;
|
|
|
|
// set color
|
|
color = Color.CYAN;
|
|
}
|
|
|
|
/**
|
|
* The getter for the paddles position
|
|
*
|
|
* @return position The paddles current position
|
|
*/
|
|
public Position getPosition() {
|
|
return position;
|
|
}
|
|
|
|
/**
|
|
* The setter for the paddles position
|
|
*
|
|
* @param position The paddles new position
|
|
*/
|
|
public void setPosition(Position position) {
|
|
this.position = position;
|
|
}
|
|
|
|
/**
|
|
* The getter for the paddles color
|
|
*
|
|
* @return color The paddles current color
|
|
*/
|
|
public Color getColor() {
|
|
return color;
|
|
}
|
|
|
|
/**
|
|
* The setter for the paddles color
|
|
*
|
|
* @param color The paddles new color
|
|
*/
|
|
public void setColor(Color color) {
|
|
this.color = color;
|
|
}
|
|
|
|
/**
|
|
* The getter for the paddles width
|
|
*
|
|
* @return width The paddles current width
|
|
*/
|
|
public int getWidth() {
|
|
return width;
|
|
}
|
|
|
|
/**
|
|
* The setter for the paddles width
|
|
*
|
|
* @param width The paddles new width
|
|
*/
|
|
public void setWidth(int width) {
|
|
this.width = width;
|
|
}
|
|
|
|
/**
|
|
* The getter for the paddles height
|
|
*
|
|
* @return height The paddles current height
|
|
*/
|
|
public int getHeight() {
|
|
return height;
|
|
}
|
|
|
|
/**
|
|
* The setter for the paddles height
|
|
*
|
|
* @param height The paddles new height
|
|
*/
|
|
public void setHeight(int height) {
|
|
this.height = height;
|
|
}
|
|
|
|
/**
|
|
* The getter for the paddles direction
|
|
*
|
|
* @return direction The paddles current direction
|
|
*/
|
|
public int getDirection() {
|
|
return direction;
|
|
}
|
|
|
|
/**
|
|
* The setter for the paddles direction
|
|
*
|
|
* @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;
|
|
}
|
|
|
|
/**
|
|
* Updates paddles position based on balls position
|
|
*
|
|
* @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);
|
|
}
|
|
}
|
|
}
|