Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.47 KB | None | 0 0
  1. package asteroids.model;
  2.  
  3. import be.kuleuven.cs.som.annotate.Basic;
  4. import be.kuleuven.cs.som.annotate.Immutable;
  5. import be.kuleuven.cs.som.annotate.Raw;
  6.  
  7. public class Entity {
  8.        
  9.     /**
  10.      * Initialize this new entity with given x- and y-coordinates for the position, x-and y-coordinate
  11.      * for the velocity and the given radius.
  12.      *
  13.      * @param  x
  14.      *         the x-coordinate of the position for this new entity.
  15.      * @param  y
  16.      *         the y-coordinate of the position for this new entity.
  17.      * @param  velocityx
  18.      *         The x-coordinate of the velocity for this new entity.
  19.      * @param  velocityy
  20.      *         The y-coordinate of the velocity for this new entity.
  21.      * @param  radius
  22.      *         The radius for this new entity.
  23.      * @throws IllegalNumberException
  24.      *        The given number is not valid
  25.      *         | ! isValidNumber(number)
  26.      *@throw   IllegalNumberException
  27.      *        the given radius is not valid.
  28.      *        | ! canHaveAsRadius(radius)
  29.      * @post   The position of this new entity is equal to a double[] with coordinates the given x and y.
  30.      *         |new.getPosition()=={x,y}.
  31.      * @post   If the given x-coordinate and y-coordinate are valid coordinates for the velocity of any entity
  32.      *        and the speed is valid for any entity, the velocity of this new entity is equal to
  33.      *        a double[] with coordinates the given x and y.
  34.      *         Otherwise, the velocity of this new entity is equal to {300000/sqrt(2) , 300000/sqrt(2)}.
  35.      *         | if (isValidNumber(velocityx) && IsValidNumber(velocityy) && isValidSpeed(new.speed) )
  36.      *         |    then new.getVelocity() == {velocityx , velocityy}
  37.      *         | else new.getVelocity() == {300000/Math.sqrt(2) , 300000/Math.sqrt(2)}
  38.      * @post   The radius of this new entity is equal to the given radius.
  39.      *         | new.getRadius()== radius
  40.      */
  41.     public Entity(double x, double y, double velocityx, double velocityy, double radius)
  42.             throws IllegalNumberException {
  43.         setPosition(x,y);      
  44.         setVelocity(velocityx,velocityy);
  45.  
  46.         this.radius = radius;
  47.     }  
  48.    
  49.     /**
  50.      * Terminate this entity.
  51.      *
  52.      * @post   This entity  is terminated.
  53.      *       | new.isTerminated()
  54.      * @post   ...
  55.      *       | ...
  56.      */
  57.      public void terminate() {
  58.          this.isTerminated = true;
  59.      }
  60.      
  61.      /**
  62.       * Return a boolean indicating whether or not this entity
  63.       * is terminated.
  64.       */
  65.      @Basic @Raw
  66.      public boolean isTerminated() {
  67.          return this.isTerminated;
  68.      }
  69.      
  70.      /**
  71.       * Variable registering whether this person is terminated.
  72.       */
  73.      private boolean isTerminated = false;
  74.      
  75.    
  76.    
  77.     /**
  78.     * Check whether the given number is a valid number for any entity
  79.     * @param number
  80.     *        The number to check.
  81.     * @return true if the number is not infinitive and not NaN.
  82.     *              |result == (number != infinity && number!= -infinity && number!=NaN)
  83.     */
  84.     public static boolean isValidNumber(double number) {
  85.         return number != Double.POSITIVE_INFINITY && number != Double.NEGATIVE_INFINITY && ! Double.isNaN(number);
  86.     }
  87.    
  88.    
  89.     //Methods about the position of the entity
  90.    
  91.     /**
  92.      * @return the position of this entity.
  93.      */
  94.    
  95.     @Basic @Raw
  96.     public double[] getPosition() {
  97.         return this.position;
  98.     }
  99.    
  100.     /**
  101.      * set the position of the entity to {x,y} with x and y the given coordinates.
  102.      * @param  x
  103.      *         the new x-coordinate for this entity.
  104.      * @param  y
  105.      *         the new y-coordinate for this entity.
  106.      * @post   If the x- and y-coordinates are valid, the position is equal to the double[] {x,y}.
  107.      *         | new.position=Double[] {x,y}
  108.      * @throws IllegalNumberException
  109.      *         The given x- or y-coordinate is not valid for the position of this entity.
  110.      *         | ! isValidNumber(x) || ! isValidNumber(y)
  111.      */
  112.    
  113.    
  114.     public void setPosition(double x,double y) throws IllegalNumberException {
  115.         if ( !isValidNumber(x))
  116.             throw new IllegalNumberException(x);
  117.         if ( !isValidNumber(y))
  118.             throw new IllegalNumberException(y);
  119.         double[] loc = {x,y};
  120.         this.position = loc;
  121.     }
  122.    
  123.     /**
  124.      * variable registering the position of this entity.
  125.      */
  126.     private double[] position;
  127.    
  128.    
  129.     //methods about the velocity of the entity
  130.    
  131.     /**
  132.      * @return the x-coordinate of the velocity of this entity.
  133.      */
  134.     @Basic @Raw
  135.     public double getVelocityx() {
  136.         return this.xvelocity;
  137.     }
  138.    
  139.     /**
  140.      *
  141.      * @return the y-coordinate of the velocity of this entity.
  142.      */
  143.     @Basic @Raw
  144.     public double getVelocityy() {
  145.         return this.yvelocity;
  146.     }    
  147.    
  148.     /**
  149.      * Set the x-coordinate of the velocity of this entity to the given coordinate.
  150.      *
  151.      * @param  velocityx
  152.      *         The new x-coordinate for the velocity for this entity.
  153.      * @post   If the given x-coordinate is a valid coordinate for any entity,
  154.      *          the x-coordinate of the velocity of this new entity is equal to the given
  155.      *          coordinate.
  156.      *          | if ( isValidNumber(velocityx))
  157.      *          |   then new.getVelocityx() == velocityx
  158.      */
  159.     @Raw
  160.     public void setVelocityx(double velocityx) {
  161.         if (! isValidNumber(velocityx))
  162.             this.xvelocity =  0;
  163.         else
  164.             this.xvelocity= velocityx;
  165.     }
  166.     /**
  167.      * Set the y-coordinate of the velocity of this entity to the given coordinate.
  168.      *
  169.      * @param  velocityy
  170.      *         The new y-coordinate for the velocity for this entity.
  171.      * @post   If the given y-coordinate is a valid coordinate for any entity,
  172.      *         the y-coordinate of the velocity of this new entity is equal to the given
  173.      *         coordinate.
  174.      *       | if ( isValidNumber(velocityy))
  175.      *       |   then new.getVelocityy() == velocityy
  176.      */
  177.     @Raw
  178.     public void setVelocityy(double velocityy) {
  179.         if ( ! isValidNumber(velocityy))
  180.             this.yvelocity =  0;
  181.         else
  182.             this.yvelocity=velocityy;
  183.     }
  184.    
  185.     /**
  186.      * variable registering the x-coördinate of the velocity of the entity
  187.      */
  188.     private double xvelocity;
  189.    
  190.     /**
  191.      * variable registering the y-coördinate of the velocity of the entity
  192.      */
  193.     private double yvelocity;
  194.    
  195.    
  196.     /**
  197.      * Check whether the given speed is a valid speed for
  198.      * any entity.
  199.      *  
  200.      * @param  speed
  201.      *         The speed to check.
  202.      * @return true if speed<=300000
  203.      *         |result == (velocity<=300000)
  204.      */
  205.     public static boolean isValidSpeed(double speed) {
  206.         return speed<=300000;
  207.     }
  208.    
  209.     /**
  210.      * Variable registering the speed of this entity.
  211.      */
  212.     private double speed;
  213.    
  214.     /**
  215.      * @Return the velocity of this entity.
  216.      */
  217.     @Basic @Raw
  218.     public double getSpeed() {
  219.         return this.speed;
  220.     }
  221.    
  222.     /**
  223.      * @Return the velocity of this entity.
  224.      */
  225.     @Basic @Raw
  226.     public double[] getVelocity() {
  227.         return this.velocity;
  228.     }
  229.    
  230.    
  231.    
  232.     /**
  233.      * Set the velocity of this entity to a double[] {x,y} with x and y
  234.      * the given coordinates of the velocity.
  235.      *
  236.      * @param velocityx
  237.      *        The new x-coordinate of the velocity for this entity.
  238.      * @param velocityy
  239.      *        The new y-coordinate of the velocity for this entity.
  240.      * @post  If the calculated speed is a valid speed for any entity,
  241.      *        the velocity of this new entity is equal to the the double[] {x,y}
  242.      *        with x and y the given coordinates.
  243.      *        Otherwise, the velocity is equal to {3000000/sqrt(2), 300000/sqrt(2)}
  244.      *       | if (isValidSpeed(Speed))
  245.      *       |   then new.getVelocity() == double[] {velocityx, velocityy}
  246.      *       | else new.getVelocity() == double[] {3000000/Math.sqrt(2), 300000/Math.sqrt(2)}
  247.      */
  248.     @Raw
  249.     public void setVelocity(double velocityx,double velocityy) {
  250.        
  251.         this.speed =  Math.sqrt(Math.pow(velocityx, 2) + Math.pow(velocityy, 2));
  252.         if (!isValidSpeed(this.speed)){
  253.             setVelocityx(300000/Math.sqrt(2));
  254.             setVelocityy(300000/Math.sqrt(2));
  255.             this.speed=300000;}
  256.         else
  257.             {setVelocityx(velocityx);
  258.             setVelocityy(velocityy);}
  259.         double[] velocityList= {this.getVelocityx(),this.getVelocityy()};
  260.         this.velocity = velocityList;
  261.        
  262.     }  
  263.    
  264.    
  265.    
  266.     /**
  267.      * variable registering the velocity of this entity.
  268.      */
  269.     private double[] velocity;
  270.    
  271.     //methods about the radius of the entity
  272.    
  273.     /**
  274.      * Return the radius of this entity.
  275.      */
  276.     @Basic @Raw @Immutable
  277.     public double getRadius() {
  278.         return this.radius;
  279.     }
  280.    
  281.     /**
  282.      * Variable registering the radius of this entity.
  283.      */
  284.     private final double radius;
  285.    
  286.     //method getDistanceBetween
  287.    
  288.     /**
  289.      * Get the distance between this entity and a given entity.
  290.      * @param  entity
  291.      *         the other entity of which we need to get the distance from this entity.
  292.      * @return 0
  293.      *         If the given entity is equal to this entity, the distance is zero.
  294.      *         | distance == zero
  295.      * @return distance
  296.      *         returns the calculated distance between this entity and the given entity.
  297.      *         | distance == Math.sqrt(Math.pow(this.getPosition().get(0)-entity.getPosition().get(0), 2)
  298.      *           +Math.pow(this.getPosition().get(1)-entity.getPosition().get(1),2))-this.getRadius()-entity.getRadius();
  299.      * @throws IllegalNumberException
  300.      *         The given distance isn't a valid number for any entity.
  301.      *         ( ! isValidNumber(distance))
  302.      */
  303.     public double getDistanceBetween(Entity entity) throws IllegalNumberException {
  304.         double distance=Math.sqrt(Math.pow(this.getPosition()[0]-entity.getPosition()[0], 2)
  305.                 +Math.pow(this.getPosition()[1]-entity.getPosition()[1],2))-this.getRadius()-entity.getRadius();
  306.         if (! isValidNumber(distance))
  307.             throw new IllegalNumberException(distance);
  308.         if (this==entity)
  309.             return 0;
  310.         return distance;
  311.     }
  312.  
  313.    //method overlap
  314.     /**
  315.      * Determine if this entity overlaps with the given entity.
  316.      * @param   entity
  317.      *          The given entity which may or may not overlap with this entity.
  318.      * @return  true if the distance between this entity and the given entity is less than 0
  319.      *          or if this entity is equal to the given entity.
  320.      *          | (this.getDistanceBetween(entity) <= 0 )
  321.      * @throws  IllegalNumberException
  322.      *          The distance between this entity and a given entity is not a valid number.
  323.      *          | (! isValidNumber(getDistanceBetween(entity))
  324.      */  
  325.     public boolean overlap(Entity entity) throws IllegalNumberException {
  326.         return (getDistanceBetween(entity) <= 0);
  327.     }
  328.    
  329.     //methods concerning getTimeCollision
  330.    
  331.     /**
  332.      * Return when, if ever, two space entitys collide.
  333.      * @param entity
  334.      * @return Positive infinity
  335.      *         if the scalar product of the velocity vector and the position vector
  336.      *         is greater than or equal to 0,
  337.      *         meaning the entitys won't ever collide.
  338.      * @return Positive infinity
  339.      *         if the scalar product of the velocity vector and the position vector squared
  340.      *         - the product of the length of the velocity vector squared and (the length of the position vector squared
  341.      *         - the sum of the radii squared), is less than or equal to 0,
  342.      *         meaning the entitys won't ever collide.
  343.      * @return The time needed for the two entitys to collide,
  344.      *         by collide meaning that the distance between this entity and a given entity is equal to the sum of the two radii.
  345.      *         |timeToCollision == -(this.scalarProductVeloPos(entity)
  346.      *         | + Math.sqrt(this.calculatedD(entity))) / this.lengthVelocitySquared(entity)
  347.      * @throws IllegalArgumentException
  348.      *         if the two entitys already overlap.
  349.      *         if (overlap(entity))
  350.      * @throws IllegalNumberException
  351.  
  352.      */
  353.     public double getTimeToCollision(Entity entity) throws IllegalNumberException,IllegalArgumentException {
  354.         if (this.overlap(entity))
  355.             throw new IllegalArgumentException();
  356.         if (this.scalarProductVeloPos(entity) >= 0)
  357.             return Double.POSITIVE_INFINITY;
  358.         if (( this.calculatedD(entity) <= 0))
  359.             return Double.POSITIVE_INFINITY;
  360.         return -( this.scalarProductVeloPos(entity) + Math.sqrt(this.calculatedD(entity))) / this.lengthVelocitySquared(entity);
  361.     }
  362.    
  363.     /**
  364.      * calculate the position vector between the two positions
  365.      * @param entity
  366.      * @return  a double[] list {x,y} where x is the position vector between the x-coordinates of this entity and a given entity
  367.      *          and y is the position vector between the y-coordinates of this entity and a given entity.
  368.      *          | newposition == (entity.getPosition()[0] - this.getPosition()[0], entity.getPosition()[1] - this.getPosition[1])
  369.      */
  370.     public double[] positionVector(Entity entity) {
  371.         double [] positionVector = {entity.getPosition()[0] - this.getPosition()[0],entity.getPosition()[1] - this.getPosition()[1]};
  372.         return positionVector;
  373.     }
  374.    
  375.     /**
  376.      * calculate the velocity vector between the two velocities
  377.      * @param  entity
  378.      * @return a double[] (x,y) where x is the velocity vector between this entity and a given entity in x direction
  379.      *         and y is the velocity vector between this entity and a given entity in the y direction.
  380.      *         | newvelocity == (entity.getVelocityx() - this.getVelocityx(), entity.getVelocityy() - this.getVelocityy())
  381.      */
  382.     public double[] velocityVector(Entity entity) {
  383.         double[] velocityVector=new double[2];
  384.         velocityVector[0] = entity.getVelocityx() - this.getVelocityx();
  385.         velocityVector[1] = entity.getVelocityy() - this.getVelocityy();
  386.         return velocityVector;
  387.     }
  388.    
  389.     /**
  390.      * calculate the scalar product of the position vector with itself
  391.      * @param entity
  392.      * @return the length of the vector between the center of this entity and a given entity squared
  393.      *         | length == Math.pow(differenceInPosition(entity)[0], 2) + Math.pow(differenceInPosition(entity)[1], 2);
  394.      * @throws IllegalNumberException
  395.      *         if the calculated length is not valid.
  396.      *         | ( ! isValidNumber(length))
  397.      */
  398.     public double lengthPositionSquared(Entity entity) throws IllegalNumberException {
  399.         double length=Math.pow(positionVector(entity)[0], 2) + Math.pow(positionVector(entity)[1], 2);
  400.         if  ( ! isValidNumber(length))
  401.             throw new IllegalNumberException(length);
  402.         return  length;
  403.     }
  404.    
  405.     /**
  406.      * calculate the scalar product of the velocity vector with itself
  407.      * @param entity
  408.      * @return the length of the velocity vector between the center of this entity and a given entity squared
  409.      *         | differenceVeSq == Math.pow(differenceInVelocity(entity)[0], 2) + Math.pow(differenceInVelocity(entity)[1], 2);
  410.      * @throws IllegalNumberException
  411.      *         if the calculated length is not valid.
  412.      *         | ( ! isValidNumber(number))
  413.      */
  414.     public double lengthVelocitySquared(Entity entity) throws IllegalNumberException {
  415.             lengthVeSq = Math.pow(this.velocityVector(entity)[0], 2) + Math.pow(this.velocityVector(entity)[1], 2);
  416.         if (!isValidNumber(lengthVeSq))
  417.                 throw new IllegalNumberException(lengthVeSq);
  418.         return lengthVeSq; 
  419.     }
  420.    
  421.     /**
  422.      * variable registering the the length of the velocity vector squared
  423.      */
  424.     private double lengthVeSq;
  425.  
  426.    
  427.     /**
  428.      * calculate the scalar product of the velocity vector and the position vector
  429.      * @param  entity
  430.      * @return the velocity vector in the x-direction times the position vector on the x-axis
  431.      *         + the velocity vector in the y-direction times the position vector on the y-axis
  432.      *         |differenceVeLo == ((differenceInVelocity(entity)[0] * differenceInPosition(entity)[0])
  433.      *         |+ (differenceInVelocity(entity)[1] * differenceInPosition(entity)[1]))
  434.      * @throws IllegalNumberException
  435.      *         if the calculated number is not valid.
  436.      *         | ( ! isValidNumber(number))
  437.      */
  438.     public double scalarProductVeloPos(Entity entity) throws IllegalNumberException{
  439.         differenceVePos = ((this.velocityVector(entity)[0] * this.positionVector(entity)[0])
  440.                 + (this.velocityVector(entity)[1] * this.positionVector(entity)[1]));
  441.         if (!isValidNumber(differenceVePos))
  442.                 throw new IllegalNumberException(differenceVePos);
  443.         return differenceVePos;
  444.     }
  445.    
  446.     /**
  447.      * variable registering the product of the velocity vector and the position vector.
  448.      */
  449.     private double differenceVePos;
  450.    
  451.     /**
  452.      * calculate the sum of the radii squared
  453.      * @param entity
  454.      * @return the sum of the radii squared.
  455.      *         | sigmasquared == Math.pow(this.getRadius() + entity.getRadius(),2);
  456.      * @throws IllegalNumberException
  457.      *         if the calculated number is not valid.
  458.      *         | ( ! isValidNumber(number))
  459.      */
  460.     public double sigmasquared(Entity entity)throws IllegalNumberException{
  461.         sigmasquared = Math.pow(this.getRadius() + entity.getRadius(),2);
  462.         if (!isValidNumber(sigmasquared))
  463.                 throw new IllegalNumberException(sigmasquared);
  464.         return sigmasquared;
  465.     }
  466.    
  467.     /**
  468.      * variable registering the sum of the radii squared.
  469.      */
  470.     private double sigmasquared;
  471.    
  472.     /**
  473.      * calculate d
  474.      * @param entity
  475.      * @return ( the scalar product of the velocity vector and the position vector ) squared
  476.      *         - the product of length of the velocity vector squared
  477.      *         and (the length of the position vector squared - the sum of the radii squared)
  478.      *         |calculatedD == Math.pow((differenceVeloLoc(entity)),2)
  479.      *         |- ((differenceVelocitySquared(entity)) * ((differenceLocationSquared(entity) - sigmaSquared(entity))))
  480.      * @throws IllegalNumberException
  481.      *         if the calculated number is not valid.
  482.      *         | ( ! isValidNumber(number))            
  483.      */
  484.     public double calculatedD(Entity entity) throws IllegalNumberException {
  485.        
  486.         try{
  487.             calculatedD = Math.pow((this.scalarProductVeloPos(entity)),2) -
  488.                     ((this.lengthVelocitySquared(entity)) * ((this.lengthPositionSquared(entity) - this.sigmasquared(entity))));
  489.         }catch (IllegalNumberException e){
  490.             throw new IllegalNumberException(calculatedD);
  491.             }
  492.         return calculatedD;
  493.     }
  494.    
  495.     /**
  496.      * variable registering the calculated D.
  497.      */
  498.     private double calculatedD;
  499.    
  500.     //methods concerning getCollisionPosition
  501.    
  502.     /**
  503.      * variable registering the center of this entity while colliding
  504.      */
  505.     private double[] thisCoordCollision;
  506.    
  507.     /**
  508.      * variable registering the center of the other entity while colliding
  509.      */
  510.     private double[] entityCoordCollision;
  511.    
  512.     /**
  513.      * calculate the center of the circles at the time of collision
  514.      * @param  entity
  515.      * @post   set the double[] thisCoordCollision to this.getPosition() + (getTimeToCollision(entity) * this.getVelocity());
  516.      *         |this.thisCoordCollision==this.getPosition() + (getTimeToCollision(entity) * this.getVelocity())
  517.      * @post   set the double[] entityCoordCollision to entity.getPosition() + (getTimeToCollision(entity) * entity.getVelocity());
  518.      *         |this.entityCoordCollision==entity.getPosition() + (getTimeToCollision(entity) * entity.getVelocity())
  519.      * @throws IllegalNumberException
  520.      */
  521.     public void calculateCollision(Entity entity) throws IllegalNumberException {
  522.         double[] coordcollthis=new double[2];
  523.         double[] coordcollentity=new double[2];
  524.         coordcollthis[0] = this.getPosition()[0] + (getTimeToCollision(entity) * this.getVelocityx());
  525.         coordcollthis[1] = this.getPosition()[1] + (getTimeToCollision(entity) * this.getVelocityy());
  526.         coordcollentity[0] = entity.getPosition()[0] + (getTimeToCollision(entity) * entity.getVelocityx());
  527.         coordcollentity[1] = entity.getPosition()[1] + (getTimeToCollision(entity) * entity.getVelocityy());
  528.         this.thisCoordCollision=coordcollthis;
  529.         this.entityCoordCollision=coordcollentity;
  530.     }
  531.    
  532.     /**
  533.      * get the position of the collision
  534.      * @param  entity
  535.      * @return null
  536.      *         if the time before the collision is infinity.
  537.      *         |(getTimeToCollision(entity)==Double.POSITIVE_INFINITY)  
  538.      * @return collisionPosition
  539.      *         the position where the two entitys collide
  540.      *         | collisionPosition= thisCoordCollision+direction * (this.getRadius()/(this.getRadius()+entity.getRadius()));
  541.      * @throws IllegalArgumentException
  542.      *         if the two entitys already overlap
  543.      *         | (this.overlap(entity))
  544.      * @throws IllegalNumberException
  545.      *         if one of the elements of the position of the collision is negative or NaN
  546.      *         | (collisionPosition[0]==Double.NEGATIVE_INFINITY || collisionPosition[0]==Double.NaN)
  547.      *         | (collisionPosition[1]==Double.NEGATIVE_INFINITY || collisionPosition[1]==Double.NaN)
  548.      */
  549.     public double[] getCollisionPosition(Entity entity) throws IllegalArgumentException,IllegalNumberException{
  550.         if (overlap(entity))
  551.             throw new IllegalArgumentException();
  552.         if (this.getTimeToCollision(entity)==Double.POSITIVE_INFINITY)
  553.             return null;
  554.         calculateCollision(entity);
  555.         double[] direction=new double[2];
  556.         direction[0]=entityCoordCollision[0]-thisCoordCollision[0];
  557.         direction[1]=entityCoordCollision[1]-thisCoordCollision[1];
  558.         double[] colposition= new double[2];
  559.         colposition[0]= thisCoordCollision[0]+direction[0] * (this.getRadius()/(this.getRadius()+entity.getRadius()));
  560.         colposition[1]= thisCoordCollision[1]+direction[1] * (this.getRadius()/(this.getRadius()+entity.getRadius()));
  561.         if (colposition[0]==Double.NEGATIVE_INFINITY || colposition[0]==Double.NaN)
  562.             throw new IllegalNumberException(colposition[0]);
  563.         if (colposition[0]==Double.NEGATIVE_INFINITY || colposition[0]==Double.NaN)
  564.             throw new IllegalNumberException(colposition[1]);
  565.         this.collisionPosition=colposition;
  566.         return this.collisionPosition;
  567.     }
  568.    
  569.    /**
  570.     * variable registering the position of the collision.
  571.     */
  572.     private double[] collisionPosition;
  573.    
  574.    
  575.     //methods associating with world    
  576.    
  577.     /**
  578.      * Return the world to which this entity belongs.
  579.      */
  580.     @Basic @Raw
  581.     public World getWorld() {
  582.         return this.world;
  583.     }
  584.  
  585.     /**
  586.      * Check whether this entity can have the given world as
  587.      * its world.
  588.      *
  589.      * @param  world
  590.      *         The world to check.
  591.      * @return If this entity is terminated, true if and only if the
  592.      *         given world is not effective.
  593.      *       | if (this.isTerminated())
  594.      *       |   then result == (world == null)
  595.      * @return If this entity is not terminated, true if and only if the given
  596.      *         world is effective and not yet terminated.
  597.      *       | if (! this.isTerminated())
  598.      *       |   then result ==
  599.      *       |     (world != null) &&
  600.      *       |     (! world.isTerminated())
  601.      */
  602.     @Raw
  603.     public boolean canHaveAsWorld(World world) {
  604.         if (this.isTerminated())
  605.             return world == null;
  606.         if (this.getWorld()!=world)
  607.             return false;
  608.         return (world != null)
  609.                 && (!world.isTerminated());
  610.     }
  611.  
  612.     /**
  613.      * Check whether this entity has a proper world.
  614.      *
  615.      * @return True if and only if this entity can have its world as its
  616.      *         world, and if the world of this entity is either not effective
  617.      *         or if it has this entity as one of its entitys.
  618.      *       | result ==
  619.      *       |   canHaveAsworld(getworld()) &&
  620.      *       |   ( (getworld() == null) || getworld().hasAsentity(this))
  621.      * @note   At this point we do not impose the condition that the world
  622.      *         may not have another entity of the same share. That condition
  623.      *         is taken care of in the invariants of world.
  624.      */
  625.     @Raw
  626.     public boolean hasProperWorld() {
  627.         return canHaveAsWorld(getWorld())
  628.                 && ((getWorld() == null) || (getWorld().hasAsEntity(this)));
  629.     }
  630.  
  631.     /**
  632.      * Set the world of this entity to the given world.
  633.      *
  634.      * @param  world
  635.      *         The new world for this entity.
  636.      * @post   The world of this entity is the same as the
  637.      *         given world.
  638.      *       | new.getworld() == world
  639.      * @throws IllegalArgumentException
  640.      *         This entity cannot have the given world as its world.
  641.      *       | ! canHaveAsworld(world)
  642.      */
  643.     @Raw
  644.     public void setWorld(World world) throws IllegalArgumentException {
  645.         if (!canHaveAsWorld(world))
  646.             throw new IllegalArgumentException("Inappropriate world!");
  647.         this.world = world;
  648.     }
  649.  
  650.     /**
  651.      * Variable referencing the world to which this entity belongs.
  652.      */
  653.     private World world = null;
  654.  
  655.    
  656. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement