Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.25 KB | None | 0 0
  1. package asteroids.model;
  2.  
  3. import be.kuleuven.cs.som.annotate.*;
  4. import java.util.*;
  5. import asteroids.model.Entity;
  6.  
  7. /**
  8.  * @invar  Each world can have its width as width .
  9.  *       | canHaveAsWidth(this.getWidth())
  10.  * @invar  Each world can have its height as height .
  11.  *       | canHaveAsHeight(this.getHeight())
  12.  * @invar   Each world must have proper ships.
  13.  *        | hasProperShips()
  14.  * @invar Each world must have proper bullets.
  15.  *          |hasProperBullets()
  16.  */
  17. public class World {
  18.     /**
  19.      * Initialize this new world with given width and height as a non-terminated world with no ships or bullets yet.
  20.      *
  21.      * @param  width
  22.      *         The width for this new world.
  23.      * @param  height
  24.      *         The height for this new world
  25.      * @post   If the given height is a valid height for any world,
  26.      *         the height of this new world is equal to the given
  27.      *         height. Otherwise, the height of this new world is equal
  28.      *         to 0.
  29.      *       | if (isValidValue(height))
  30.      *       |   then new.getHeight == height
  31.      *       |   else new.getHeight == 0
  32.      * @post   If the given width is a valid width for any world,
  33.      *         the width of this new world is equal to the given
  34.      *         width. Otherwise, the width of this new world is equal
  35.      *         to 0.
  36.      *       | if (isValidValue(width))
  37.      *       |   then new.getWidth == width
  38.      *       |   else new.getWidth == 0
  39.      * @post   This new world has no ships yet.
  40.      *       | new.getNbShips() == 0
  41.      * @post   This new world has no bullets yet.
  42.      *       | new.getNbBullets() == 0
  43.      */
  44.    
  45.     @Raw
  46.     public World(double width, double height) {
  47.         if (! canHaveAsValue(width))
  48.             width = 0;
  49.         if (! canHaveAsValue(height))
  50.             height = 0;
  51.         this.width = width;
  52.         this.height = height;
  53.     }
  54.     /**
  55.      * Terminate this world.
  56.      *
  57.      * @post   This world  is terminated.
  58.      *       | new.isTerminated()
  59.      * @post   ...
  60.      *       | ...
  61.      */
  62.      public void terminate() {
  63.          this.isTerminated = true;
  64.      }
  65.      
  66.      /**
  67.       * Return a boolean indicating whether or not this world
  68.       * is terminated.
  69.       */
  70.      @Basic @Raw
  71.      public boolean isTerminated() {
  72.          return this.isTerminated;
  73.      }
  74.      
  75.      /**
  76.       * Variable registering whether this person is terminated.
  77.       */
  78.      private boolean isTerminated = false;
  79.      
  80.    
  81.     /**
  82.      * Return the width of this world.
  83.      */
  84.     @Basic @Raw @Immutable
  85.     public double getWidth() {
  86.         return this.width;
  87.     }
  88.    
  89.     /**
  90.      *
  91.      * @return the height of this world.
  92.      */
  93.     @Basic @Raw @Immutable
  94.     public double getHeight() {
  95.         return this.height;
  96.     }
  97.    
  98.     /**
  99.      * Check whether this world can have the given value as its height/width.
  100.      *  
  101.      * @param  value
  102.      *         The value to check.
  103.      * @return
  104.      *       | result == (value>=0 && value <= Double.MAX_VALUE && value !=Double.NaN)
  105.     */
  106.     @Raw
  107.     public boolean canHaveAsValue(double value) {
  108.         return ((value >= 0) && (value <= Double.MAX_VALUE) && (value != Double.NaN));
  109.     }
  110.    
  111.     /**
  112.      * Variable registering the width of this world.
  113.      */
  114.     private final double width;
  115.  
  116.     /**
  117.      * Variable registering the height of this world.
  118.      */
  119.     private final double height;
  120.    
  121.  
  122.  
  123.     /**
  124.      * Check whether this world has the given ship as one of its
  125.      * ships.
  126.      *
  127.      * @param  ship
  128.      *         The ship to check.
  129.      */
  130.     @Basic
  131.     @Raw
  132.     public boolean hasAsShip(@Raw Ship ship) {
  133.         return ships.contains(ship);
  134.     }
  135.    
  136.     /**
  137.      * Check whether this world can have the given ship
  138.      * as one of its ships.
  139.      *
  140.      * @param  ship
  141.      *         The ship to check.
  142.      * @return True if and only if the given ship is effective
  143.      *         and that ship is a valid ship for a world.
  144.      *       | result ==
  145.      *       |   (ship != null) &&
  146.      *       |   Ship.isValidWorld(this)
  147.      */
  148.     @Raw
  149.     public boolean canHaveAsShip(Ship ship) throws IllegalNumberException {
  150.         if ((ship == null) || (!ship.canHaveAsWorld(this)) || (hasAsShip(ship)))
  151.             return false;
  152.         if ((!canHaveAsWidthCoordinate(ship)) ||(!canHaveAsHeightCoordinate(ship)))
  153.             return false;
  154.         for (Entity entity : entities) {
  155.             if (significantOverlap(ship, entity))
  156.                 return false;          
  157.         }
  158.         return true;
  159.     }
  160.    
  161.     public boolean significantOverlap(Entity entity1, Entity entity2) throws IllegalNumberException{
  162.         if (entity1.overlap(entity2))
  163.             if (entity1.getDistanceBetween(entity2)+entity1.getRadius()+entity2.getRadius()<=0.99*(entity1.getRadius()+entity2.getRadius()))
  164.                 return true;
  165.         return false;
  166.            
  167.        
  168.     }
  169.    
  170.     public boolean apparentlyWithinBoundaries(Entity entity){
  171.         return (Math.abs(this.getWidth()-entity.getPosition()[0])>=0.99*entity.getRadius() && Math.abs(entity.getPosition()[0])>=0.99*entity.getRadius()
  172.                 &&Math.abs(entity.getPosition()[1])>=0.99*entity.getRadius()&& Math.abs(this.getHeight()-entity.getPosition()[1])>=0.99*entity.getRadius());
  173.     }
  174.    
  175.     public boolean canHaveAsWidthCoordinate(Entity entity){
  176.         if (!((entity.getPosition()[0] + entity.getRadius()) <= this.getWidth()))
  177.             return false;
  178.         if ((!canHaveAsValue(entity.getPosition()[0] + entity.getRadius())) || (! canHaveAsValue(entity.getPosition()[0] - entity.getRadius())))
  179.             return false;
  180.         return true;
  181.     }
  182.    
  183.     public boolean canHaveAsHeightCoordinate(Entity entity){
  184.         if (!((entity.getPosition()[1] + entity.getRadius()) <= this.getHeight()))
  185.             return false;
  186.         if ((!canHaveAsValue(entity.getPosition()[1] + entity.getRadius())) || (! canHaveAsValue(entity.getPosition()[1] - entity.getRadius())))
  187.             return false;
  188.         return true;
  189.     }
  190.    
  191.     /**
  192.      * Check whether this world has proper ships attached to it.
  193.      *
  194.      * @return True if and only if this world can have each of the
  195.      *         ships attached to it as one of its ships,
  196.      *         and if each of these ships references this world as
  197.      *         the world to which they are attached.
  198.      *       | for each ship in Ship:
  199.      *       |   if (hasAsShip(ship))
  200.      *       |     then canHaveAsShip(ship) &&
  201.      *       |          (ship.getWorld() == this)
  202.      */
  203.     public boolean hasProperShips() throws IllegalNumberException{
  204.         for (Ship ship : ships) {
  205.             if (!canHaveAsShip(ship))
  206.                 return false;
  207.             if (ship.getWorld() != this)
  208.                 return false;
  209.         }
  210.         return true;
  211.     }
  212.  
  213.     /**
  214.      * Return the number of ships associated with this world.
  215.      *
  216.      * @return  The total number of ships collected in this world.
  217.      *        | result ==
  218.      *        |   card({ship:Ship | hasAsShip({ship)})
  219.      */
  220.     public int getNbShips() {
  221.         return ships.size();
  222.     }
  223.  
  224.     /**
  225.      * Add the given ship to the set of ships of this world.
  226.      *
  227.      * @param  ship
  228.      *         The ship to be added.
  229.      * @pre    The given ship is effective and already references
  230.      *         this world.
  231.      *       | (ship != null) && (ship.getWorld() == this)
  232.      * @post   This world has the given ship as one of its ships.
  233.      *       | new.hasAsShip(ship)
  234.      */
  235.     public void addShip(@Raw Ship ship) throws IllegalArgumentException, IllegalNumberException{
  236.         if (!canHaveAsShip(ship))
  237.             throw new IllegalArgumentException();
  238.         ships.add(ship);
  239.         entities.add(ship);
  240.     }
  241.  
  242.     /**
  243.      * Remove the given ship from the set of ships of this world.
  244.      *
  245.      * @param  ship
  246.      *         The ship to be removed.
  247.      * @pre    This world has the given ship as one of
  248.      *         its ships, and the given ship does not
  249.      *         reference any world.
  250.      *       | this.hasAsShip(ship) &&
  251.      *       | (ship.getWorld() == null)
  252.      * @post   This world no longer has the given ship as
  253.      *         one of its ships.
  254.      *       | ! new.hasAsShip(ship)
  255.      */
  256.     @Raw
  257.     public void removeShip(Ship ship) throws IllegalArgumentException{
  258.         if( !this.hasAsShip(ship) || !(ship.getWorld() == null))
  259.             throw new IllegalArgumentException();
  260.         ships.remove(ship);
  261.         entities.remove(ship);
  262.     }
  263.  
  264.     /**
  265.      * Variable referencing a set collecting all the ships
  266.      * of this world.
  267.      *
  268.      * @invar  The referenced set is effective.
  269.      *       | ships != null
  270.      * @invar  Each ship registered in the referenced list is
  271.      *         effective and not yet terminated.
  272.      *       | for each ship in ships:
  273.      *       |   ( (ship != null) &&
  274.      *       |     (! ship.isTerminated()) )
  275.      */
  276.     private final Set<Ship> ships = new HashSet<Ship>();
  277.     private final Set<Entity> entities = new HashSet<Entity>();
  278.    
  279.     /**
  280.      * Check whether this world has the given entity as one of its
  281.      * entity.
  282.      *
  283.      * @param  entity
  284.      *         The entity to check.
  285.      */
  286.     @Basic
  287.     @Raw
  288.     public boolean hasAsEntity(@Raw Entity entity) {
  289.         return entities.contains(entity);
  290.     }
  291.    
  292.     /**
  293.      * Check whether this world can have the given bullet
  294.      * as one of its bullets.
  295.      *
  296.      * @param  bullet
  297.      *         The bullet to check.
  298.      * @return True if and only if the given bullet is effective
  299.      *         and that bullet is a valid bullet for a world.
  300.      *       | result ==
  301.      *       |   (bullet != null) &&
  302.      *       |   bullet.isValidWorld(this)
  303.      */
  304.     @Raw
  305.     public boolean canHaveAsEntity(Entity entity) throws IllegalNumberException{
  306.         if ((entity == null) || (!entity.canHaveAsWorld(this)) || (hasAsEntity(entity)))
  307.             return false;
  308.         if ((!canHaveAsWidthCoordinate(entity)) ||(!canHaveAsHeightCoordinate(entity)))
  309.             return false;
  310.         for (Entity entity1 : entities) {
  311.             if (significantOverlap(entity1, entity))
  312.                 return false;          
  313.         }
  314.         return true;
  315.     }
  316.    
  317.     /**
  318.      * Check whether this world has proper bullets attached to it.
  319.      *
  320.      * @return True if and only if this world can have each of the
  321.      *         bullets attached to it as one of its bullets,
  322.      *         and if each of these bullets references this world as
  323.      *         the world to which they are attached.
  324.      *       | for each bullet in bullet:
  325.      *       |   if (hasAsbullet(bullet))
  326.      *       |     then canHaveAsbullet(bullet) &&
  327.      *       |          (bullet.getWorld() == this)
  328.      */
  329.     public boolean hasProperEntities() throws IllegalNumberException{
  330.         for (Entity entity : entities) {
  331.             if (!canHaveAsEntity(entity))
  332.                 return false;
  333.             if (entity.getWorld() != this)
  334.                 return false;
  335.         }
  336.         return true;
  337.     }
  338.    
  339.     /**
  340.      * Return the number of bullets associated with this world.
  341.      *
  342.      * @return  The total number of bullets collected in this world.
  343.      *        | result ==
  344.      *        |   card({bullet:bullet | hasAsbullet({bullet)})
  345.      */
  346.     public int getNbEntities() {
  347.         return entities.size();
  348.     }
  349.    
  350.    
  351.     /**
  352.      * Check whether this world has the given entity as one of its
  353.      * entity.
  354.      *
  355.      * @param  entity
  356.      *         The entity to check.
  357.      */
  358.     @Basic
  359.     @Raw
  360.     public boolean hasAsBullet(@Raw Bullet bullet) {
  361.         return bullets.contains(bullet);
  362.     }
  363.    
  364.  
  365.     /**
  366.      * Check whether this world can have the given bullet
  367.      * as one of its bullets.
  368.      *
  369.      * @param  bullet
  370.      *         The bullet to check.
  371.      * @return True if and only if the given bullet is effective
  372.      *         and that bullet is a valid bullet for a world.
  373.      *       | result ==
  374.      *       |   (bullet != null) &&
  375.      *       |   bullet.isValidWorld(this)
  376.      */
  377.     @Raw
  378.     public boolean canHaveAsBullet(Bullet bullet) throws IllegalNumberException{
  379.         if ((bullet == null) || (!bullet.canHaveAsWorld(this)) || (hasAsBullet(bullet)))
  380.             return false;
  381.         if ((!canHaveAsWidthCoordinate(bullet)) ||(!canHaveAsHeightCoordinate(bullet)))
  382.             return false;
  383.         for (Entity entity : entities) {
  384.             if (significantOverlap(bullet, entity))
  385.                 return false;          
  386.         }
  387.         return true;
  388.     }
  389.    
  390.    
  391.     /**
  392.      * Check whether this world has proper bullets attached to it.
  393.      *
  394.      * @return True if and only if this world can have each of the
  395.      *         bullets attached to it as one of its bullets,
  396.      *         and if each of these bullets references this world as
  397.      *         the world to which they are attached.
  398.      *       | for each bullet in bullet:
  399.      *       |   if (hasAsbullet(bullet))
  400.      *       |     then canHaveAsbullet(bullet) &&
  401.      *       |          (bullet.getWorld() == this)
  402.      */
  403.     public boolean hasProperBullets() throws IllegalNumberException{
  404.         for (Bullet bullet : bullets) {
  405.             if (!canHaveAsBullet(bullet))
  406.                 return false;
  407.             if (bullet.getWorld() != this)
  408.                 return false;
  409.         }
  410.         return true;
  411.     }
  412.  
  413.     /**
  414.      * Return the number of bullets associated with this world.
  415.      *
  416.      * @return  The total number of bullets collected in this world.
  417.      *        | result ==
  418.      *        |   card({bullet:bullet | hasAsbullet({bullet)})
  419.      */
  420.     public int getNbBullets() {
  421.         return bullets.size();
  422.     }
  423.  
  424.     /**
  425.      * Add the given bullet to the set of bullets of this world.
  426.      *
  427.      * @param  bullet
  428.      *         The bullet to be added.
  429.      * @pre    The given bullet is effective and already references
  430.      *         this world.
  431.      *       | (bullet != null) && (bullet.getWorld() == this)
  432.      * @post   This world has the given bullet as one of its bullets.
  433.      *       | new.hasAsbullet(bullet)
  434.      */
  435.     public void addBullet(@Raw Bullet bullet) throws IllegalArgumentException,IllegalNumberException{
  436.         if (!canHaveAsBullet(bullet))
  437.             throw new IllegalArgumentException();
  438.         bullets.add(bullet);
  439.         entities.add(bullet);
  440.     }
  441.  
  442.     /**
  443.      * Remove the given bullet from the set of bullets of this world.
  444.      *
  445.      * @param  bullet
  446.      *         The bullet to be removed.
  447.      * @pre    This world has the given bullet as one of
  448.      *         its bullets, and the given bullet does not
  449.      *         reference any world.
  450.      *       | this.hasAsbullet(bullet) &&
  451.      *       | (bullet.getWorld() == null)
  452.      * @post   This world no longer has the given bullet as
  453.      *         one of its bullets.
  454.      *       | ! new.hasAsbullet(bullet)
  455.      */
  456.     @Raw
  457.     public void removeBullet(Bullet bullet) throws IllegalArgumentException{
  458.         if( !this.hasAsBullet(bullet) || !(bullet.getWorld() == null))
  459.             throw new IllegalArgumentException();
  460.         bullets.remove(bullet);
  461.         entities.remove(bullet);
  462.     }
  463.  
  464.     /**
  465.      * Variable referencing a set collecting all the bullets
  466.      * of this world.
  467.      *
  468.      * @invar  The referenced set is effective.
  469.      *       | bullets != null
  470.      * @invar  Each bullet registered in the referenced list is
  471.      *         effective and not yet terminated.
  472.      *       | for each bullet in bullets:
  473.      *       |   ( (bullet != null) &&
  474.      *       |     (! bullet.isTerminated()) )
  475.      */
  476.     private final Set<Bullet> bullets = new HashSet<Bullet>();
  477.    
  478.     public Entity entityAtPosition(double[] position){
  479.         for (Entity entity : entities) {
  480.             if (entity.getPosition()[0]==position[0] && entity.getPosition()[1]==position[1])
  481.                 return entity;
  482.         }
  483.         return null;
  484.     }
  485. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement