83 lines
1.9 KiB
Java
83 lines
1.9 KiB
Java
package break_out.model;
|
|
|
|
import break_out.Constants;
|
|
|
|
/**
|
|
* This class contains the information about the balls characteristics and behavior
|
|
*
|
|
* @author iSchumacher
|
|
*
|
|
*/
|
|
public class Ball implements IBall{
|
|
|
|
/**
|
|
* The balls position on the playground
|
|
*/
|
|
private Position position;
|
|
|
|
/**
|
|
* The balls direction
|
|
*/
|
|
private Vector2D direction;
|
|
|
|
/**
|
|
* The constructor of a ball
|
|
* The balls position and direction are initialized here.
|
|
*/
|
|
public Ball() {
|
|
this.position = new Position(0, 0);
|
|
this.direction = new Vector2D(Constants.BALL_SPEED,Constants.BALL_SPEED);
|
|
this.direction.rescale();
|
|
|
|
// start at bottom-center
|
|
this.position.setX(Constants.SCREEN_WIDTH/2);
|
|
this.position.setY(Constants.SCREEN_HEIGHT-3*Constants.BALL_DIAMETER);
|
|
}
|
|
|
|
/**
|
|
* The getter for the balls position
|
|
* @return position The balls current position
|
|
*/
|
|
public Position getPosition() {
|
|
return this.position;
|
|
}
|
|
|
|
/**
|
|
* The getter for the balls direction
|
|
* @return direction The balls current direction
|
|
*/
|
|
public Vector2D getDirection() {
|
|
return this.direction;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public void updatePosition() {
|
|
// @rxbn, Ruben Meyer
|
|
this.position.setX(this.position.getX()+this.direction.getDx());
|
|
this.position.setY(this.position.getY()+this.direction.getDy());
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public void reactOnBorder() {
|
|
|
|
// @rxbn, Ruben Meyer
|
|
|
|
// left border
|
|
if(this.position.getX() < 0) this.direction.setDx(-(this.direction.getDx()));
|
|
|
|
// right border
|
|
if(this.position.getX()+Constants.BALL_DIAMETER > Constants.SCREEN_WIDTH) this.direction.setDx(-(this.direction.getDx()));
|
|
|
|
// top border
|
|
if(this.position.getY() < 0) this.direction.setDy(-(this.direction.getDy()));
|
|
|
|
// bottom border
|
|
if(this.position.getY()+Constants.BALL_DIAMETER > Constants.SCREEN_HEIGHT) this.direction.setDy(-(this.direction.getDy()));
|
|
}
|
|
|
|
}
|