Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package asteroids.model;
- import be.kuleuven.cs.som.annotate.*;
- /**
- *
- * A class of ships with a position, a velocity, a radius and an orientation.
- *
- * @invar The x-and y-coordinates of the position of each ship must be valid numbers for
- * | isValidNumber(x) && isValidNumber(y)
- * @invar The x-and y-coordinates of each ship must be valid for any
- ship.The calculated speed of the ship must be a valid speed for any ship.
- * | isValidNumber(getVelocityx()) && isValidNumber(getVelocityy())
- * | isValidSpeed(new.speed)
- * @invar The orientation of each ship must be a valid orientation for any
- * ship.
- * | isValidorientation(getorientation())
- * @invar Each ship can have its radius as radius.
- * | canHaveAsRadius(this.getRadius())
- *
- */
- public class Ship extends Entity{
- /**
- * Initialize this new ship with given x- and y-coordinates for the position, x-and y-coordinate
- * for the velocity, given orientation and the given radius.
- *
- * @param x
- * the x-coordinate of the position for this new ship.
- * @param y
- * the y-coordinate of the position for this new ship.
- * @param velocityx
- * The x-coordinate of the velocity for this new ship.
- * @param velocityy
- * The y-coordinate of the velocity for this new ship.
- * @param orientation
- * The orientation for this new ship.
- * @param radius
- * The radius for this new ship.
- * @throws IllegalNumberException
- * The given number is not valid
- * | ! isValidNumber(number)
- *@throw IllegalNumberException
- * the given radius is not valid.
- * | ! canHaveAsRadius(radius)
- * @pre The given orientation must be a valid orientation for any ship.
- * | isValidorientation(orientation)
- * @post The position of this new ship is equal to a double[] with coordinates the given x and y.
- * |new.getPosition()=={x,y}.
- * @post If the given x-coordinate and y-coordinate are valid coordinates for the velocity of any ship
- * and the speed is valid for any ship, the velocity of this new ship is equal to
- * a double[] with coordinates the given x and y.
- * Otherwise, the velocity of this new ship is equal to {300000/sqrt(2) , 300000/sqrt(2)}.
- * | if (isValidNumber(velocityx) && IsValidNumber(velocityy) && isValidSpeed(new.speed) )
- * | then new.getVelocity() == {velocityx , velocityy}
- * | else new.getVelocity() == {300000/Math.sqrt(2) , 300000/Math.sqrt(2)}
- * @post The orientation of this new ship is equal to the given orientation.
- * | new.getOrientation() == orientation
- * @post The radius of this new ship is equal to the given radius.
- * | new.getRadius()== radius
- */
- public Ship(double x, double y, double velocityx, double velocityy, double radius, double orientation)
- throws IllegalNumberException{
- super(x,y,velocityx,velocityy,radius);
- setOrientation(orientation);
- if (!canHaveAsRadius(radius))
- throw new IllegalNumberException(radius);
- this.radius = radius;
- }
- /**
- * Check whether the given number is a valid number for any ship
- * @param number
- * The number to check.
- * @return true if the number is not infinitive and not NaN.
- * |result == (number != infinity && number!= -infinity && number!=NaN)
- */
- public static boolean isValidNumber(double number) {
- return number != Double.POSITIVE_INFINITY && number != Double.NEGATIVE_INFINITY && ! Double.isNaN(number);
- }
- //methods about the orientation of the ship
- /**
- * @Return the orientation of this ship.
- */
- @Basic @Raw
- public double getOrientation() {
- return this.orientation;
- }
- /**
- * Check whether the given orientation is a valid orientation for
- * any ship.
- *
- * @param orientation
- * The orientation to check.
- * @return true if the orientation of the ship is between 0 and 2*pi.
- * | result == (orientation >= 0 && orientation <=2pi && isValidNumber(orientation))
- */
- public static boolean isValidOrientation(double orientation) {
- return ((orientation >= 0) && (orientation <= 2*Math.PI)) &&(isValidNumber(orientation));
- }
- /**
- * Set the orientation of this ship to the given orientation.
- *
- * @param orientation
- * The new orientation for this ship.
- * @pre The given orientation must be a valid orientation for any
- * ship.
- * | isValidorientation(orientation)
- * @post The orientation of this ship is equal to the given orientation.
- * | new.getOrientation() == orientation
- */
- @Raw
- public void setOrientation(double orientation) {
- assert isValidOrientation(orientation);
- this.orientation = orientation;
- }
- /**
- * Variable registering the orientation of this ship.
- */
- private double orientation;
- //methods about the radius of the ship
- /**
- * Return the radius of this ship.
- */
- @Basic @Raw @Immutable @Override
- public double getRadius() {
- return this.radius;
- }
- /**
- * Check whether this ship can have the given radius as its radius.
- *
- * @param radius
- * The radius to check.
- * @return true if the radius of the ship is larger than or equal to 10 and the radius is a valid number.
- * | result == (radius>=10 && isValidNumber(radius)
- */
- @Raw
- public static boolean canHaveAsRadius(double radius) {
- return (radius >=10 && isValidNumber(radius));
- }
- /**
- * Variable registering the radius of this ship.
- */
- private final double radius;
- //method move
- /**
- * Change the position of the ship based on the current position, current velocity and on a given time duration.
- * @param duration
- * the duration of the movement of this ship
- * @post If the velocity of this ship is zero or the given duration is zero,
- * the new position is equal to the current position of the ship.
- * | new.getPosition() == this.getPosition()
- * @post If the velocity is not zero and the duration is greater than zero, the new position is given by
- * new.position=this.velocity*duration
- * | new.getPosition()== duration*this.getVelocity()
- * @throws IllegalNumberException
- * the given duration is less than zero.
- * | duration < 0
- */
- public void move(double duration) throws IllegalNumberException {
- if (duration < 0)
- throw new IllegalNumberException(duration);
- if ( (this.getSpeed() == 0) || (duration == 0) )
- setPosition(getPosition()[0],getPosition()[1]);
- setPosition(getPosition()[0] + (duration*getVelocityx()), getPosition()[1] + (duration*getVelocityy()));
- }
- //method turn
- /**
- * Turn the ship by adding a given angle to the current orientation of this ship.
- * @param angle
- * the angle to be added to the current orientation.
- * @pre the sum of the current orientation and the given angle must be a valid orientation.
- * | isValidOrientation(angle+orientation)
- * @post the new orientation is equal to the sum of the old orientation and the given angle.
- * |new.orientation= getOrientation()+angle
- */
- public void turn(double angle) {
- double NewAngle = (angle + this.getOrientation());
- assert (isValidOrientation(NewAngle));
- this.orientation = NewAngle;
- }
- //method thrust
- /**
- * change the velocity of this ship based on the current velocity, current orientation and a given amount.
- *
- * @param amount
- * amount which changes the velocity to a new velocity.
- * @post If the given amount is negative, the new amount is zero.
- * | if amount < 0
- * | amount == 0
- * @post The new velocity is set to this.velocity+amount*cos(this.orientation).
- * | new.getVelocity()=this.getVelocity()+amount*cos(this.getOrientation()).
- */
- public void thrust(double amount) {
- if (amount < 0)
- amount = 0;
- setVelocity(this.getVelocityx() + amount*Math.cos(getOrientation()),
- this.getVelocityy() + amount*Math.sin(getOrientation()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement