1
0
Fork 0
uni_informatik_projekt/src/break_out/model/Ball.java

83 lines
2.1 KiB
Java

package break_out.model;
import break_out.Constants;
/**
* This class contains the information about the balls characteristics and behavior
*
* @author iSchumacher
* @author modified by 175
*
*/
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;
}
/**
* updates ball position
*/
public void updatePosition() {
// sets X position
this.position.setX(this.position.getX()+this.direction.getDx());
// sets Y position
this.position.setY(this.position.getY()+this.direction.getDy());
}
/**
* Ball reacts to contact with the borders
*/
public void reactOnBorder() {
// reacts on left border
if(this.position.getX() < 0) this.direction.setDx(-(this.direction.getDx()));
// reacts on right border (+Diameter because of hitbox)
if(this.position.getX()+Constants.BALL_DIAMETER > Constants.SCREEN_WIDTH) this.direction.setDx(-(this.direction.getDx()));
// reacts on top border
if(this.position.getY() < 0) this.direction.setDy(-(this.direction.getDy()));
// reacts on bottom border (+Diameter because of hitbox)
if(this.position.getY()+Constants.BALL_DIAMETER > Constants.SCREEN_HEIGHT) this.direction.setDy(-(this.direction.getDy()));
}
}