Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package asteroids.model;
- import be.kuleuven.cs.som.annotate.*;
- import java.util.*;
- import asteroids.model.Entity;
- /**
- * @invar Each world can have its width as width .
- * | canHaveAsWidth(this.getWidth())
- * @invar Each world can have its height as height .
- * | canHaveAsHeight(this.getHeight())
- * @invar Each world must have proper ships.
- * | hasProperShips()
- * @invar Each world must have proper bullets.
- * |hasProperBullets()
- */
- public class World {
- /**
- * Initialize this new world with given width and height as a non-terminated world with no ships or bullets yet.
- *
- * @param width
- * The width for this new world.
- * @param height
- * The height for this new world
- * @post If the given height is a valid height for any world,
- * the height of this new world is equal to the given
- * height. Otherwise, the height of this new world is equal
- * to 0.
- * | if (isValidValue(height))
- * | then new.getHeight == height
- * | else new.getHeight == 0
- * @post If the given width is a valid width for any world,
- * the width of this new world is equal to the given
- * width. Otherwise, the width of this new world is equal
- * to 0.
- * | if (isValidValue(width))
- * | then new.getWidth == width
- * | else new.getWidth == 0
- * @post This new world has no ships yet.
- * | new.getNbShips() == 0
- * @post This new world has no bullets yet.
- * | new.getNbBullets() == 0
- */
- @Raw
- public World(double width, double height) {
- if (! canHaveAsValue(width))
- width = 0;
- if (! canHaveAsValue(height))
- height = 0;
- this.width = width;
- this.height = height;
- }
- /**
- * Terminate this world.
- *
- * @post This world is terminated.
- * | new.isTerminated()
- * @post ...
- * | ...
- */
- public void terminate() {
- this.isTerminated = true;
- }
- /**
- * Return a boolean indicating whether or not this world
- * is terminated.
- */
- @Basic @Raw
- public boolean isTerminated() {
- return this.isTerminated;
- }
- /**
- * Variable registering whether this person is terminated.
- */
- private boolean isTerminated = false;
- /**
- * Return the width of this world.
- */
- @Basic @Raw @Immutable
- public double getWidth() {
- return this.width;
- }
- /**
- *
- * @return the height of this world.
- */
- @Basic @Raw @Immutable
- public double getHeight() {
- return this.height;
- }
- /**
- * Check whether this world can have the given value as its height/width.
- *
- * @param value
- * The value to check.
- * @return
- * | result == (value>=0 && value <= Double.MAX_VALUE && value !=Double.NaN)
- */
- @Raw
- public boolean canHaveAsValue(double value) {
- return ((value >= 0) && (value <= Double.MAX_VALUE) && (value != Double.NaN));
- }
- /**
- * Variable registering the width of this world.
- */
- private final double width;
- /**
- * Variable registering the height of this world.
- */
- private final double height;
- /**
- * Check whether this world has the given ship as one of its
- * ships.
- *
- * @param ship
- * The ship to check.
- */
- @Basic
- @Raw
- public boolean hasAsShip(@Raw Ship ship) {
- return ships.contains(ship);
- }
- /**
- * Check whether this world can have the given ship
- * as one of its ships.
- *
- * @param ship
- * The ship to check.
- * @return True if and only if the given ship is effective
- * and that ship is a valid ship for a world.
- * | result ==
- * | (ship != null) &&
- * | Ship.isValidWorld(this)
- */
- @Raw
- public boolean canHaveAsShip(Ship ship) throws IllegalNumberException {
- if ((ship == null) || (!ship.canHaveAsWorld(this)) || (hasAsShip(ship)))
- return false;
- if ((!canHaveAsWidthCoordinate(ship)) ||(!canHaveAsHeightCoordinate(ship)))
- return false;
- for (Entity entity : entities) {
- if (significantOverlap(ship, entity))
- return false;
- }
- return true;
- }
- public boolean significantOverlap(Entity entity1, Entity entity2) throws IllegalNumberException{
- if (entity1.overlap(entity2))
- if (entity1.getDistanceBetween(entity2)+entity1.getRadius()+entity2.getRadius()<=0.99*(entity1.getRadius()+entity2.getRadius()))
- return true;
- return false;
- }
- public boolean apparentlyWithinBoundaries(Entity entity){
- return (Math.abs(this.getWidth()-entity.getPosition()[0])>=0.99*entity.getRadius() && Math.abs(entity.getPosition()[0])>=0.99*entity.getRadius()
- &&Math.abs(entity.getPosition()[1])>=0.99*entity.getRadius()&& Math.abs(this.getHeight()-entity.getPosition()[1])>=0.99*entity.getRadius());
- }
- public boolean canHaveAsWidthCoordinate(Entity entity){
- if (!((entity.getPosition()[0] + entity.getRadius()) <= this.getWidth()))
- return false;
- if ((!canHaveAsValue(entity.getPosition()[0] + entity.getRadius())) || (! canHaveAsValue(entity.getPosition()[0] - entity.getRadius())))
- return false;
- return true;
- }
- public boolean canHaveAsHeightCoordinate(Entity entity){
- if (!((entity.getPosition()[1] + entity.getRadius()) <= this.getHeight()))
- return false;
- if ((!canHaveAsValue(entity.getPosition()[1] + entity.getRadius())) || (! canHaveAsValue(entity.getPosition()[1] - entity.getRadius())))
- return false;
- return true;
- }
- /**
- * Check whether this world has proper ships attached to it.
- *
- * @return True if and only if this world can have each of the
- * ships attached to it as one of its ships,
- * and if each of these ships references this world as
- * the world to which they are attached.
- * | for each ship in Ship:
- * | if (hasAsShip(ship))
- * | then canHaveAsShip(ship) &&
- * | (ship.getWorld() == this)
- */
- public boolean hasProperShips() throws IllegalNumberException{
- for (Ship ship : ships) {
- if (!canHaveAsShip(ship))
- return false;
- if (ship.getWorld() != this)
- return false;
- }
- return true;
- }
- /**
- * Return the number of ships associated with this world.
- *
- * @return The total number of ships collected in this world.
- * | result ==
- * | card({ship:Ship | hasAsShip({ship)})
- */
- public int getNbShips() {
- return ships.size();
- }
- /**
- * Add the given ship to the set of ships of this world.
- *
- * @param ship
- * The ship to be added.
- * @pre The given ship is effective and already references
- * this world.
- * | (ship != null) && (ship.getWorld() == this)
- * @post This world has the given ship as one of its ships.
- * | new.hasAsShip(ship)
- */
- public void addShip(@Raw Ship ship) throws IllegalArgumentException, IllegalNumberException{
- if (!canHaveAsShip(ship))
- throw new IllegalArgumentException();
- ships.add(ship);
- entities.add(ship);
- }
- /**
- * Remove the given ship from the set of ships of this world.
- *
- * @param ship
- * The ship to be removed.
- * @pre This world has the given ship as one of
- * its ships, and the given ship does not
- * reference any world.
- * | this.hasAsShip(ship) &&
- * | (ship.getWorld() == null)
- * @post This world no longer has the given ship as
- * one of its ships.
- * | ! new.hasAsShip(ship)
- */
- @Raw
- public void removeShip(Ship ship) throws IllegalArgumentException{
- if( !this.hasAsShip(ship) || !(ship.getWorld() == null))
- throw new IllegalArgumentException();
- ships.remove(ship);
- entities.remove(ship);
- }
- /**
- * Variable referencing a set collecting all the ships
- * of this world.
- *
- * @invar The referenced set is effective.
- * | ships != null
- * @invar Each ship registered in the referenced list is
- * effective and not yet terminated.
- * | for each ship in ships:
- * | ( (ship != null) &&
- * | (! ship.isTerminated()) )
- */
- private final Set<Ship> ships = new HashSet<Ship>();
- private final Set<Entity> entities = new HashSet<Entity>();
- /**
- * Check whether this world has the given entity as one of its
- * entity.
- *
- * @param entity
- * The entity to check.
- */
- @Basic
- @Raw
- public boolean hasAsEntity(@Raw Entity entity) {
- return entities.contains(entity);
- }
- /**
- * Check whether this world can have the given bullet
- * as one of its bullets.
- *
- * @param bullet
- * The bullet to check.
- * @return True if and only if the given bullet is effective
- * and that bullet is a valid bullet for a world.
- * | result ==
- * | (bullet != null) &&
- * | bullet.isValidWorld(this)
- */
- @Raw
- public boolean canHaveAsEntity(Entity entity) throws IllegalNumberException{
- if ((entity == null) || (!entity.canHaveAsWorld(this)) || (hasAsEntity(entity)))
- return false;
- if ((!canHaveAsWidthCoordinate(entity)) ||(!canHaveAsHeightCoordinate(entity)))
- return false;
- for (Entity entity1 : entities) {
- if (significantOverlap(entity1, entity))
- return false;
- }
- return true;
- }
- /**
- * Check whether this world has proper bullets attached to it.
- *
- * @return True if and only if this world can have each of the
- * bullets attached to it as one of its bullets,
- * and if each of these bullets references this world as
- * the world to which they are attached.
- * | for each bullet in bullet:
- * | if (hasAsbullet(bullet))
- * | then canHaveAsbullet(bullet) &&
- * | (bullet.getWorld() == this)
- */
- public boolean hasProperEntities() throws IllegalNumberException{
- for (Entity entity : entities) {
- if (!canHaveAsEntity(entity))
- return false;
- if (entity.getWorld() != this)
- return false;
- }
- return true;
- }
- /**
- * Return the number of bullets associated with this world.
- *
- * @return The total number of bullets collected in this world.
- * | result ==
- * | card({bullet:bullet | hasAsbullet({bullet)})
- */
- public int getNbEntities() {
- return entities.size();
- }
- /**
- * Check whether this world has the given entity as one of its
- * entity.
- *
- * @param entity
- * The entity to check.
- */
- @Basic
- @Raw
- public boolean hasAsBullet(@Raw Bullet bullet) {
- return bullets.contains(bullet);
- }
- /**
- * Check whether this world can have the given bullet
- * as one of its bullets.
- *
- * @param bullet
- * The bullet to check.
- * @return True if and only if the given bullet is effective
- * and that bullet is a valid bullet for a world.
- * | result ==
- * | (bullet != null) &&
- * | bullet.isValidWorld(this)
- */
- @Raw
- public boolean canHaveAsBullet(Bullet bullet) throws IllegalNumberException{
- if ((bullet == null) || (!bullet.canHaveAsWorld(this)) || (hasAsBullet(bullet)))
- return false;
- if ((!canHaveAsWidthCoordinate(bullet)) ||(!canHaveAsHeightCoordinate(bullet)))
- return false;
- for (Entity entity : entities) {
- if (significantOverlap(bullet, entity))
- return false;
- }
- return true;
- }
- /**
- * Check whether this world has proper bullets attached to it.
- *
- * @return True if and only if this world can have each of the
- * bullets attached to it as one of its bullets,
- * and if each of these bullets references this world as
- * the world to which they are attached.
- * | for each bullet in bullet:
- * | if (hasAsbullet(bullet))
- * | then canHaveAsbullet(bullet) &&
- * | (bullet.getWorld() == this)
- */
- public boolean hasProperBullets() throws IllegalNumberException{
- for (Bullet bullet : bullets) {
- if (!canHaveAsBullet(bullet))
- return false;
- if (bullet.getWorld() != this)
- return false;
- }
- return true;
- }
- /**
- * Return the number of bullets associated with this world.
- *
- * @return The total number of bullets collected in this world.
- * | result ==
- * | card({bullet:bullet | hasAsbullet({bullet)})
- */
- public int getNbBullets() {
- return bullets.size();
- }
- /**
- * Add the given bullet to the set of bullets of this world.
- *
- * @param bullet
- * The bullet to be added.
- * @pre The given bullet is effective and already references
- * this world.
- * | (bullet != null) && (bullet.getWorld() == this)
- * @post This world has the given bullet as one of its bullets.
- * | new.hasAsbullet(bullet)
- */
- public void addBullet(@Raw Bullet bullet) throws IllegalArgumentException,IllegalNumberException{
- if (!canHaveAsBullet(bullet))
- throw new IllegalArgumentException();
- bullets.add(bullet);
- entities.add(bullet);
- }
- /**
- * Remove the given bullet from the set of bullets of this world.
- *
- * @param bullet
- * The bullet to be removed.
- * @pre This world has the given bullet as one of
- * its bullets, and the given bullet does not
- * reference any world.
- * | this.hasAsbullet(bullet) &&
- * | (bullet.getWorld() == null)
- * @post This world no longer has the given bullet as
- * one of its bullets.
- * | ! new.hasAsbullet(bullet)
- */
- @Raw
- public void removeBullet(Bullet bullet) throws IllegalArgumentException{
- if( !this.hasAsBullet(bullet) || !(bullet.getWorld() == null))
- throw new IllegalArgumentException();
- bullets.remove(bullet);
- entities.remove(bullet);
- }
- /**
- * Variable referencing a set collecting all the bullets
- * of this world.
- *
- * @invar The referenced set is effective.
- * | bullets != null
- * @invar Each bullet registered in the referenced list is
- * effective and not yet terminated.
- * | for each bullet in bullets:
- * | ( (bullet != null) &&
- * | (! bullet.isTerminated()) )
- */
- private final Set<Bullet> bullets = new HashSet<Bullet>();
- public Entity entityAtPosition(double[] position){
- for (Entity entity : entities) {
- if (entity.getPosition()[0]==position[0] && entity.getPosition()[1]==position[1])
- return entity;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement