Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.89 KB | None | 0 0
  1. package asteroids.model;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Set;
  5.  
  6. import be.kuleuven.cs.som.annotate.*;
  7.  
  8. /**
  9.  *
  10.  * A class of ships with a position, a velocity, a radius and an orientation.
  11.  *
  12.  * @invar The x-and y-coordinates of the position of each ship must be valid numbers for
  13.  *        | isValidNumber(x) && isValidNumber(y)
  14.  * @invar The x-and y-coordinates of each ship must be valid for any
  15.           ship.The calculated speed of the ship must be a valid speed for any ship.
  16.  *        | isValidNumber(getVelocityx()) && isValidNumber(getVelocityy())
  17.  *        | isValidSpeed(new.speed)
  18.  * @invar The orientation of each ship must be a valid orientation for any
  19.  *        ship.
  20.  *        | isValidorientation(getOrientation())
  21.  * @invar the radius of each ship must be a valid radius for any ship.
  22.  *        | isValidRadius(getRadius())
  23.  *
  24.  
  25. */
  26. public class Ship extends Entity {
  27.    
  28.        /**
  29.     * Initialize this new ship with given x- and y-coordinates for the position, x-and y-coordinate
  30.     * for the velocity, given orientation and the given radius.
  31.     *
  32.     * @param  x
  33.     *         the x-coordinate of the position for this new ship.
  34.     * @param  y
  35.     *         the y-coordinate of the position for this new ship.
  36.     * @param  velocityx
  37.     *         The x-coordinate of the velocity for this new ship.
  38.     * @param  velocityy
  39.     *         The y-coordinate of the velocity for this new ship.
  40.     * @param  orientation
  41.     *         The orientation for this new ship.
  42.     * @param  radius
  43.     *         The radius for this new ship.
  44.     * @throws IllegalNumberException
  45.     *         The given number is not valid
  46.     *         | ! isValidNumber(number)
  47.     * @pre    The given orientation must be a valid orientation for any ship.
  48.     *         | isValidorientation(orientation)
  49.     * @post   The position of this new ship is equal to a double[] with coordinates the given x and y.
  50.     *         |new.getPosition()=={x,y}.
  51.     * @post   If the given x-coordinate and y-coordinate are valid coordinates for the velocity of any ship
  52.     *         and the speed is valid for any ship, the velocity of this new ship is equal to
  53.     *         a double[] with coordinates the given x and y.
  54.     *         Otherwise, the velocity of this new ship is equal to {300000/sqrt(2) , 300000/sqrt(2)}.
  55.     *         | if (isValidNumber(velocityx) && IsValidNumber(velocityy) && isValidSpeed(new.speed) )
  56.     *         |     then new.getVelocity() == {velocityx , velocityy}
  57.     *         | else new.getVelocity() == {300000/Math.sqrt(2) , 300000/Math.sqrt(2)}
  58.     * @post   The orientation of this new ship is equal to the given orientation.
  59.     *         | new.getOrientation() == orientation
  60.     * @post   The radius of this new ship is equal to the given radius.
  61.     *         | new.getRadius()== radius
  62.     */
  63.     public Ship(double x, double y, double velocityx, double velocityy, double radius, double orientation)
  64.             throws IllegalNumberException {
  65.         super(x,y,velocityx,velocityy,radius);
  66.         setOrientation(orientation);
  67.         setRadius(radius);
  68.         if (! isValidMass(mass))
  69.             mass = 0;
  70.         setMass(mass);
  71.         //while (bullets.size() < 15)
  72.             //addBullet(bullets);
  73.     }  
  74.    
  75.     private final Set<Bullet> standardbullets = new HashSet<Bullet>();
  76.     public void createStandardbullets() {
  77.         while (standardbullets.size() < 15) {
  78.            
  79.         }
  80.     }
  81.     //methods about the orientation of the entity
  82.  
  83.     /**
  84.      * @Return the orientation of this entity.
  85.      */
  86.     @Basic @Raw
  87.     public double getOrientation() {
  88.         return this.orientation;
  89.     }
  90.  
  91.     /**
  92.      * Check whether the given orientation is a valid orientation for
  93.      * any entity.
  94.      *  
  95.      * @param  orientation
  96.     *          The orientation to check.
  97.     * @return  true if the orientation of the entity is between 0 and 2*pi.
  98.     *          | result == (orientation >= 0 && orientation <=2pi && isValidNumber(orientation))
  99.     */
  100.     public static boolean isValidOrientation(double orientation) {
  101.         return ((orientation >= 0) && (orientation <= 2*Math.PI)) &&(isValidNumber(orientation));
  102.     }
  103.  
  104.     /**
  105.      * Set the orientation of this entity to the given orientation.
  106.      *
  107.      * @param  orientation
  108.      *         The new orientation for this entity.
  109.      * @pre    The given orientation must be a valid orientation for any
  110.      *         entity.
  111.      *         | isValidorientation(orientation)
  112.      * @post   The orientation of this entity is equal to the given orientation.
  113.      *         | new.getOrientation() == orientation
  114.      */
  115.     @Raw
  116.     public void setOrientation(double orientation) {
  117.         assert isValidOrientation(orientation);
  118.         this.orientation = orientation;
  119.     }
  120.  
  121.    /**
  122.     * Variable registering the orientation of this entity.
  123.     */
  124.     private double orientation;
  125.    
  126.     @Raw
  127.     public void setRadius(double radius) throws IllegalNumberException {
  128.         if (! isValidRadius(radius))
  129.             throw new IllegalNumberException(radius);
  130.         this.radius=radius;
  131.     }
  132.  
  133.     /**
  134.      * Check whether the given radius is valid for any entity.
  135.      * @param  radius
  136.      *         The radius to check.
  137.      * @return true if the radius of the entity is larger than or equal to 10 and the radius is a valid number.
  138.      *         | result == (radius>=10 && isValidNumber(radius)
  139.      */
  140.  
  141.     public static boolean isValidRadius(double radius){
  142.         return !(radius <10 || !isValidNumber(radius));
  143.     }
  144.  
  145.  
  146.    /**
  147.     * variable registering the radius of the entity.
  148.     */
  149.     private double radius;
  150.    
  151.     /**
  152.      * @invar  The mass of each ship must be a valid mass for any
  153.      *         ship.
  154.      *       | isValidMass(getMass())
  155.      */
  156.  
  157. /**
  158.  * Initialize this new ship with given mass.
  159.  *
  160.  * @param  mass
  161.  *         The mass for this new ship.
  162.  * @post   If the given mass is a valid mass for any ship,
  163.  *         the mass of this new ship is equal to the given
  164.  *         mass. Otherwise, the mass of this new ship is equal
  165.  *         to 0.
  166.  *       | if (isValidMass(mass))
  167.  *       |   then new.getMass() == mass
  168.  *       |   else new.getMass() == 0
  169.  */
  170.  
  171.  
  172. /**
  173.  * Return the mass of this ship.
  174.  */
  175. @Basic @Raw
  176. public double getMass() {
  177.     return this.mass;
  178. }
  179.  
  180. /**
  181.  * Check whether the given mass is a valid mass for
  182.  * any ship.
  183.  *  
  184.  * @param  mass
  185.  *         The mass to check.
  186.  * @return
  187.  *       | result ==
  188. */
  189. public boolean isValidMass(double mass) {
  190.     double m = 4/3 * Math.PI * 1.42 * Math.pow(10, 12) * Math.pow(this.getRadius(), 3);
  191.     return (mass >= m && isValidRadius(this.getRadius()));
  192. }
  193.  
  194. /**
  195.  * Set the mass of this ship to the given mass.
  196.  *
  197.  * @param  mass
  198.  *         The new mass for this ship.
  199.  * @post   If the given mass is a valid mass for any ship,
  200.  *         the mass of this new ship is equal to the given
  201.  *         mass.
  202.  *       | if (isValidMass(mass))
  203.  *       |   then new.getMass() == mass
  204.  */
  205. @Raw
  206. public void setMass(double mass) {
  207.     if (isValidMass(mass) && bullets.size() == 0)
  208.         this.mass = mass;
  209.     if (isValidMass(mass) && bullets.size() > 0)
  210.         this.mass = mass;
  211.         for (Bullet bullet : bullets)
  212.             this.mass += bullet.getMass();
  213. }
  214.  
  215. /**
  216.  * Variable registering the mass of this ship.
  217.  */
  218. private double mass;
  219.  
  220. //method turn
  221.  
  222. /**
  223.  * Turn the entity by adding a given angle to the current orientation of this entity.
  224.  * @param angle
  225.  *      the angle to be added to the current orientation.
  226.  * @pre the sum of the current orientation and the given angle must be a valid orientation.
  227.  *      | isValidOrientation(angle+orientation)
  228.  * @post the new orientation is equal to the sum of the old orientation and the given angle.
  229.  *      |new.orientation= getOrientation()+angle
  230.  */
  231. public void turn(double angle) {
  232.     double NewAngle = (angle + this.getOrientation());
  233.     assert (isValidOrientation(NewAngle));
  234.     this.orientation = NewAngle;
  235. }
  236. private double acceleration;
  237. public void thrust(double amount) {
  238.     this.acceleration = (double) 1.1 * Math.pow(10, 21) / this.getMass();
  239.     if (this.acceleration < 0)
  240.         this.acceleration = 0; 
  241.     this.setVelocity((double) this.getVelocityx() + (this.acceleration * Math.cos(this.getOrientation())*amount),(double) this.getVelocityy() + (this.acceleration * Math.sin(this.getOrientation())* amount));
  242. }
  243. public void thrustOn() {
  244.     this.thrust = true;
  245. }
  246. public void thrustOff() {
  247.     this.thrust = false;
  248. }
  249.  
  250. private boolean thrust = false;
  251. private final Set<Bullet> bullets = new HashSet<Bullet>();
  252. public void addBullet(Bullet... bullets2) throws IllegalArgumentException,IllegalNumberException {
  253.     for (Bullet bullet : bullets2)
  254.             bullets.add(bullet);
  255. }
  256.  
  257. public boolean canHaveAsBullet(Bullet bullet) {
  258.     return (Math.sqrt(Math.pow(this.getPosition()[0] - bullet.getPosition()[0],2) + Math.pow(this.getPosition()[1] - bullet.getPosition()[1],2)) <= Math.abs(this.getRadius() - bullet.getRadius()) );
  259. }
  260.  
  261.  
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement