Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.56 KB | None | 0 0
  1. package asteroids.model;
  2.  
  3. import be.kuleuven.cs.som.annotate.*;
  4.  
  5. /**
  6.  *
  7.  * A class of ships with a position, a velocity, a radius and an orientation.
  8.  *
  9.  * @invar The x-and y-coordinates of the position of each ship must be valid numbers for
  10.  *        | isValidNumber(x) && isValidNumber(y)
  11.  * @invar The x-and y-coordinates of each ship must be valid for any
  12.           ship.The calculated speed of the ship must be a valid speed for any ship.
  13.  *        | isValidNumber(getVelocityx()) && isValidNumber(getVelocityy())
  14.  *        | isValidSpeed(new.speed)
  15.  * @invar The orientation of each ship must be a valid orientation for any
  16.  *        ship.
  17.  *        | isValidorientation(getorientation())
  18.  * @invar  Each ship can have its radius as radius.
  19.  *       | canHaveAsRadius(this.getRadius())
  20.  *
  21.  
  22. */
  23.  
  24. public class Ship extends Entity{
  25.    
  26.        /**
  27.     * Initialize this new ship with given x- and y-coordinates for the position, x-and y-coordinate
  28.     * for the velocity, given orientation and the given radius.
  29.     *
  30.     * @param  x
  31.     *         the x-coordinate of the position for this new ship.
  32.     * @param  y
  33.     *         the y-coordinate of the position for this new ship.
  34.     * @param  velocityx
  35.     *         The x-coordinate of the velocity for this new ship.
  36.     * @param  velocityy
  37.     *         The y-coordinate of the velocity for this new ship.
  38.     * @param  orientation
  39.     *         The orientation for this new ship.
  40.     * @param  radius
  41.     *         The radius for this new ship.
  42.     * @throws IllegalNumberException
  43.     *         The given number is not valid
  44.     *         | ! isValidNumber(number)
  45.     *@throw   IllegalNumberException
  46.     *         the given radius is not valid.
  47.     *         | ! canHaveAsRadius(radius)
  48.     * @pre    The given orientation must be a valid orientation for any ship.
  49.     *         | isValidorientation(orientation)
  50.     * @post   The position of this new ship is equal to a double[] with coordinates the given x and y.
  51.     *         |new.getPosition()=={x,y}.
  52.     * @post   If the given x-coordinate and y-coordinate are valid coordinates for the velocity of any ship
  53.     *         and the speed is valid for any ship, the velocity of this new ship is equal to
  54.     *         a double[] with coordinates the given x and y.
  55.     *         Otherwise, the velocity of this new ship is equal to {300000/sqrt(2) , 300000/sqrt(2)}.
  56.     *         | if (isValidNumber(velocityx) && IsValidNumber(velocityy) && isValidSpeed(new.speed) )
  57.     *         |     then new.getVelocity() == {velocityx , velocityy}
  58.     *         | else new.getVelocity() == {300000/Math.sqrt(2) , 300000/Math.sqrt(2)}
  59.     * @post   The orientation of this new ship is equal to the given orientation.
  60.     *         | new.getOrientation() == orientation
  61.     * @post   The radius of this new ship is equal to the given radius.
  62.     *         | new.getRadius()== radius
  63.     */
  64.     public Ship(double x, double y, double velocityx, double velocityy, double radius, double orientation)
  65.             throws IllegalNumberException{
  66.         super(x,y,velocityx,velocityy,radius);
  67.         setOrientation(orientation);
  68.         if (!canHaveAsRadius(radius))
  69.             throw new IllegalNumberException(radius);
  70.         this.radius = radius;
  71.     }  
  72.    
  73.     /**
  74.     * Check whether the given number is a valid number for any ship
  75.     * @param number
  76.     *        The number to check.
  77.     * @return true if the number is not infinitive and not NaN.
  78.     *              |result == (number != infinity && number!= -infinity && number!=NaN)
  79.     */
  80.     public static boolean isValidNumber(double number) {
  81.         return number != Double.POSITIVE_INFINITY && number != Double.NEGATIVE_INFINITY && ! Double.isNaN(number);
  82.     }
  83.    
  84.     //methods about the orientation of the ship
  85.    
  86.     /**
  87.      * @Return the orientation of this ship.
  88.      */
  89.     @Basic @Raw
  90.     public double getOrientation() {
  91.         return this.orientation;
  92.     }
  93.  
  94.     /**
  95.      * Check whether the given orientation is a valid orientation for
  96.      * any ship.
  97.      *  
  98.      * @param  orientation
  99.     *          The orientation to check.
  100.     * @return  true if the orientation of the ship is between 0 and 2*pi.
  101.     *          | result == (orientation >= 0 && orientation <=2pi && isValidNumber(orientation))
  102.     */
  103.     public static boolean isValidOrientation(double orientation) {
  104.         return ((orientation >= 0) && (orientation <= 2*Math.PI)) &&(isValidNumber(orientation));
  105.     }
  106.  
  107.     /**
  108.      * Set the orientation of this ship to the given orientation.
  109.      *
  110.      * @param  orientation
  111.      *         The new orientation for this ship.
  112.      * @pre    The given orientation must be a valid orientation for any
  113.      *         ship.
  114.      *         | isValidorientation(orientation)
  115.      * @post   The orientation of this ship is equal to the given orientation.
  116.      *         | new.getOrientation() == orientation
  117.      */
  118.     @Raw
  119.     public void setOrientation(double orientation) {
  120.         assert isValidOrientation(orientation);
  121.         this.orientation = orientation;
  122.     }
  123.  
  124.    /**
  125.     * Variable registering the orientation of this ship.
  126.     */
  127.     private double orientation;
  128.    
  129.    
  130.     //methods about the radius of the ship
  131.  
  132.     /**
  133.      * Return the radius of this ship.
  134.      */
  135.     @Basic @Raw @Immutable @Override
  136.     public double getRadius() {
  137.         return this.radius;
  138.     }
  139.  
  140.     /**
  141.      * Check whether this ship can have the given radius as its radius.
  142.      *  
  143.      * @param  radius
  144.      *         The radius to check.
  145.      * @return true if the radius of the ship is larger than or equal to 10 and the radius is a valid number.
  146.      *       | result == (radius>=10 && isValidNumber(radius)
  147.      */
  148.     @Raw
  149.     public static boolean canHaveAsRadius(double radius) {
  150.         return (radius >=10 && isValidNumber(radius));
  151.     }
  152.  
  153.     /**
  154.      * Variable registering the radius of this ship.
  155.      */
  156.     private final double radius;
  157.    
  158.    
  159.    //method move
  160.    
  161.    /**
  162.     * Change the position of the ship based on the current position, current velocity and on a given time duration.
  163.     * @param  duration
  164.     *         the duration of the movement of this ship
  165.     * @post   If the velocity of this ship is zero or the given duration is zero,
  166.     *         the new position is equal to the current position of the ship.
  167.     *         | new.getPosition() == this.getPosition()
  168.     * @post   If the velocity is not zero and the duration is greater than zero, the new position is given by
  169.     *         new.position=this.velocity*duration
  170.     *         | new.getPosition()== duration*this.getVelocity()
  171.     * @throws IllegalNumberException
  172.     *         the given duration is less than zero.
  173.     *         | duration < 0
  174.     */
  175.     public void move(double duration) throws IllegalNumberException {
  176.         if (duration < 0)
  177.             throw new IllegalNumberException(duration);
  178.         if ( (this.getSpeed() == 0) || (duration == 0) )
  179.             setPosition(getPosition()[0],getPosition()[1]);
  180.         setPosition(getPosition()[0] + (duration*getVelocityx()), getPosition()[1] + (duration*getVelocityy()));
  181.     }
  182.    
  183.     //method turn
  184.    
  185.     /**
  186.      * Turn the ship by adding a given angle to the current orientation of this ship.
  187.      * @param angle
  188.      *      the angle to be added to the current orientation.
  189.      * @pre the sum of the current orientation and the given angle must be a valid orientation.
  190.      *      | isValidOrientation(angle+orientation)
  191.      * @post the new orientation is equal to the sum of the old orientation and the given angle.
  192.      *      |new.orientation= getOrientation()+angle
  193.      */
  194.     public void turn(double angle) {
  195.         double NewAngle = (angle + this.getOrientation());
  196.         assert (isValidOrientation(NewAngle));
  197.         this.orientation = NewAngle;
  198.     }
  199.  
  200.     //method thrust
  201.    
  202.     /**
  203.      * change the velocity of this ship based on the current velocity, current orientation and a given amount.
  204.      *
  205.      * @param amount
  206.      *        amount which changes the velocity to a new velocity.
  207.      * @post  If the given amount is negative, the new amount is zero.
  208.      *        | if amount < 0
  209.      *        |    amount == 0
  210.      * @post The new velocity is set to this.velocity+amount*cos(this.orientation).
  211.      *        | new.getVelocity()=this.getVelocity()+amount*cos(this.getOrientation()).
  212.      */
  213.    
  214.     public void thrust(double amount) {
  215.         if (amount < 0)
  216.             amount = 0;
  217.         setVelocity(this.getVelocityx() + amount*Math.cos(getOrientation()),
  218.                 this.getVelocityy() + amount*Math.sin(getOrientation()));      
  219.     }
  220.    
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement