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

95 lines
2.4 KiB
Java
Raw Normal View History

2019-11-19 00:55:13 +00:00
package break_out.model;
import break_out.Constants;
/**
* This class contains the information about the balls characteristics and behavior
*
* @author iSchumacher
* @author modified by Gruppe 175: Moritz Henseleit, Ruben Meyer
2019-11-19 00:55:13 +00:00
*
*/
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);
2019-11-19 02:05:45 +00:00
this.direction = new Vector2D(Constants.BALL_SPEED,Constants.BALL_SPEED);
2019-11-19 13:11:56 +00:00
this.direction.rescale();
2019-11-19 02:05:45 +00:00
// start at bottom-center
2019-11-26 22:45:59 +00:00
this.position.setX((Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER)/2);
this.position.setY(Constants.SCREEN_HEIGHT-Constants.BALL_DIAMETER);
2019-11-19 00:55:13 +00:00
}
/**
* 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;
}
/**
2019-11-19 14:06:25 +00:00
* updates ball position
2019-11-19 00:55:13 +00:00
*/
public void updatePosition() {
2019-11-19 14:06:25 +00:00
// sets X position
2019-11-19 02:05:45 +00:00
this.position.setX(this.position.getX()+this.direction.getDx());
2019-11-19 14:06:25 +00:00
// sets Y position
2019-11-19 02:05:45 +00:00
this.position.setY(this.position.getY()+this.direction.getDy());
2019-11-19 00:55:13 +00:00
}
/**
2019-11-19 14:06:25 +00:00
* Ball reacts to contact with the borders
2019-11-19 00:55:13 +00:00
*/
public void reactOnBorder() {
2019-11-19 14:06:25 +00:00
// reacts on left border
2019-11-26 22:45:59 +00:00
if(this.position.getX() <= 0) {
this.position.setX(0);
this.direction.setDx(-(this.direction.getDx()));
}
2019-11-19 02:05:45 +00:00
2019-11-26 22:45:59 +00:00
// reacts on right border (-Diameter because of hitbox)
if(this.position.getX() >= Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER) {
this.position.setX(Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER);
this.direction.setDx(-(this.direction.getDx()));
}
2019-11-19 02:05:45 +00:00
2019-11-19 14:06:25 +00:00
// reacts on top border
2019-11-26 22:45:59 +00:00
if(this.position.getY() <= 0) {
this.position.setY(0);
this.direction.setDy(-(this.direction.getDy()));
}
2019-11-19 02:05:45 +00:00
2019-11-19 14:06:25 +00:00
// reacts on bottom border (+Diameter because of hitbox)
2019-11-26 22:45:59 +00:00
if(this.position.getY() >= Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER) {
this.position.setY(Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER);
this.direction.setDy(-(this.direction.getDy()));
}
2019-11-19 00:55:13 +00:00
}
}