package pong.server.model;
import pong.common.GlobalSettings;
import pong.common.Position;
/**
* This is the ball class. It stores ball position and direction.
*
* @author Lorenzo Gatto
*
*/
public class Ball {
/**
* This variable stores angular coefficient of the straight line the ball is moving through.
*/
private float angularCoefficient;
private Direction direction;
private Position position;
/**
* Enum to indicate the direction the ball is moving.
*
* @author Lorenzo Gatto
*
*/
public enum Direction {
/**
* Left to right direction.
*/
LEFT_TO_RIGHT,
/**
* Right to left direction.
*/
RIGHT_TO_LEFT
}
/**
* Initializes the ball to (0, 0) position.
*/
public Ball() {
super();
this.position = new Position();
}
/**
* This method initializes the ball on the middle of the field with a random direction.
*/
public synchronized void initializeForRoundPlaying() {
this.angularCoefficient = (float) Math.random();
if (Math.random() < 0.5) {
this.direction = Direction.LEFT_TO_RIGHT;
} else {
this.direction = Direction.RIGHT_TO_LEFT;
}
this.position.setX(GlobalSettings.FIELD_WIDTH / 2.0f - GlobalSettings.BALL_DIAMETER / 2.0f);
this.position
.setY(GlobalSettings.FIELD_HEIGHT / 2.0f - GlobalSettings.BALL_DIAMETER / 2.0f);
}
/**
* Get the tangent of the angle that is formed intersecting the x axis with the straight line
* the ball is moving along.
*
* @return the angular coefficient
*/
public synchronized float getAngularCoefficient() {
return this.angularCoefficient;
}
/**
* Set the tangent of the angle that is formed intersecting the x axis with the straight line
* the ball is moving along.
*
* @param angularCoefficient
* the new angular coefficient
*/
public synchronized void setAngularCoefficient(final float angularCoefficient) {
this.angularCoefficient = angularCoefficient;
}
/**
* Get the direction where the ball is moving.
*
* @return the direction
*/
public synchronized Direction getDirection() {
return this.direction;
}
/**
* Set the direction where the ball is moving.
*
* @param direction
* the direction
*/
public synchronized void setDirection(final Direction direction) {
this.direction = direction;
}
/**
* Get the position of the ball.
*
* @return the position
*/
public synchronized Position getPosition() {
return this.position;
}
/**
* Set the position of the ball.
*
* @param position
* the position
*/
public synchronized void setPosition(final Position position) {
this.position = position;
}
}