/**
  * Sets the size of the world. <br>
  * This will remove all objects from the world. TODO Maybe it shouldn't!
  */
 private void initialize(int width, int height, int cellSize) {
   this.width = width;
   this.height = height;
   this.cellSize = cellSize;
   this.tiled = true;
   collisionChecker.initialize(width, height, cellSize, false);
 }
Example #2
0
 /**
  * Returns the neighbours to the given location. This method only looks at the logical location
  * and not the extent of objects. Hence it is most useful in scenarios where objects only span one
  * cell.
  *
  * @param actor The actor whose neighbours to locate
  * @param distance Distance in which to look for other objects
  * @param diag Is the distance also diagonal?
  * @param cls Class of objects to look for (null or Object.class will find all classes)
  * @return A collection of all neighbours found
  */
 @SuppressWarnings("unchecked")
 List getNeighbours(Actor actor, int distance, boolean diag, Class cls) {
   if (distance < 0) {
     throw new IllegalArgumentException("Distance must not be less than 0. It was: " + distance);
   }
   return collisionChecker.getNeighbours(actor, distance, diag, cls);
 }
 /**
  * Remove an object from the world.
  *
  * @param object the object to remove
  */
 public synchronized void removeObject(Actor object) {
   if (objects.remove(object)) {
     // we only want to remove it once.
     collisionChecker.removeObject(object);
   }
   object.setWorld(null);
 }
  /**
   * Add an Actor to the world (at the object's specified location).
   *
   * @param object The new object to add.
   * @throws IndexOutOfBoundsException If the coordinates are outside the bounds of the world.
   */
  public synchronized void addObject(Actor object, int x, int y) throws IndexOutOfBoundsException {
    // TODO bad performance when using a List for the objects. But if we
    // want to have paint order, a List is necessary.
    if (objects.contains(object)) {
      return;
    }

    object.addToWorld(x, y, this);

    collisionChecker.addObject(object);
    objects.add(object);

    object.addedToWorld(this);
  }
Example #5
0
  /**
   * Remove an object from the world.
   *
   * @param object the object to remove
   */
  public void removeObject(Actor object) {
    if (object == null || object.world != this) {
      return;
    }

    objectsDisordered.remove(object);
    collisionChecker.removeObject(object);
    if (objectsDisordered != objectsInActOrder && objectsInActOrder != null) {
      objectsInActOrder.remove(object);
    } else if (objectsDisordered != objectsInPaintOrder && objectsInPaintOrder != null) {
      objectsInPaintOrder.remove(object);
    }
    object.setWorld(null);
  }
Example #6
0
  /**
   * Add an Actor to the world.
   *
   * @param object The new object to add.
   * @param x The x coordinate of the location where the object is added.
   * @param y The y coordinate of the location where the object is added.
   */
  public void addObject(Actor object, int x, int y) {
    if (object.world != null) {
      if (object.world == this) {
        return; // Actor is already in the world
      }
      object.world.removeObject(object);
    }

    objectsDisordered.add(object);
    addInPaintOrder(object);
    addInActOrder(object);

    // Note we must call this before adding the object to the collision checker,
    // so that the cached bounds are cleared:
    object.addToWorld(x, y, this);

    collisionChecker.addObject(object);
    object.addedToWorld(this);

    WorldHandler whInstance = WorldHandler.getInstance();
    if (whInstance != null) {
      WorldHandler.getInstance().objectAddedToWorld(object);
    }
  }
 /**
  * Used to indicate the start of an animation sequence. For use in the collision checker.
  *
  * @see greenfoot.collision.CollisionChecker#startSequence()
  */
 void startSequence() {
   collisionChecker.startSequence();
 }
 void updateObjectLocation(Actor object, int oldX, int oldY) {
   collisionChecker.updateObjectLocation(object, oldX, oldY);
 }
 void updateObjectSize(Actor object) {
   collisionChecker.updateObjectSize(object);
 }
  /**
   * Return all objects that intersect a straight line from the location at a specified angle. The
   * angle is clockwise.
   *
   * @param x x-coordinate
   * @param y y-coordinate
   * @param angle The angle relative to current rotation of the object. (0-359)
   * @param length How far we want to look (in cells)
   * @param cls Class of objects to look for (passing 'null' will find all objects).
   */
  List getObjectsInDirection(int x0, int y0, int angle, int length, Class cls) {

    return collisionChecker.getObjectsInDirection(x0, y0, angle, length, cls);
  }
 Collection getObjectsAtPixel(int x, int y) {
   return collisionChecker.getObjectsAt(toCellFloor(x), toCellFloor(y), null);
 }
 /**
  * Returns all objects with the logical location within the specified circle. In other words an
  * object A is within the range of an object B if the distance between the center of the two
  * objects is less thatn r.
  *
  * @param x Center of the cirle
  * @param y Center of the cirle
  * @param r Radius of the cirle
  * @param cls Class of objects to look for (null or Object.class will find all classes)
  */
 List getObjectsInRange(int x, int y, int r, Class cls) {
   return collisionChecker.getObjectsInRange(x, y, r, cls);
 }
 /**
  * Returns the neighbours to the given location. This method only looks at the logical location
  * and not the extent of objects. Hence it is most useful in scenarios where objects only span one
  * cell.
  *
  * @param x Location
  * @param y Location
  * @param distance Distance in which to look for other objects
  * @param diag Is the distance also diagonal?
  * @param cls Class of objects to look for (null or Object.class will find all classes)
  * @return A collection of all neighbours found
  */
 List getNeighbours(Actor actor, int distance, boolean diag, Class cls) {
   return collisionChecker.getNeighbours(actor, distance, diag, cls);
 }
 /**
  * Return all objects at a given cell.
  *
  * <p>An object is defined to be at that cell if its graphical representation overlaps with the
  * cell at any point.
  *
  * @param x X-coordinate of the cell to be checked.
  * @param y Y-coordinate of the cell to be checked.
  * @param cls Class of objects to look return ('null' will return all objects).
  */
 public List getObjectsAt(int x, int y, Class cls) {
   return collisionChecker.getObjectsAt(x, y, cls);
 }
 /**
  * Return all the objects that intersect the given object. This takes the graphical extent of
  * objects into consideration.
  *
  * @param actor An Actor in the world
  * @param cls Class of objects to look for (null or Object.class will find all classes)
  */
 List getIntersectingObjects(Actor actor, Class cls) {
   return collisionChecker.getIntersectingObjects(actor, cls);
 }
 Actor getOneObjectAt(Actor object, int dx, int dy, Class cls) {
   return collisionChecker.getOneObjectAt(object, dx, dy, cls);
 }
Example #17
0
 /**
  * Return all objects at a given cell.
  *
  * <p>An object is defined to be at that cell if its graphical representation overlaps the center
  * of the cell.
  *
  * @param x X-coordinate of the cell to be checked.
  * @param y Y-coordinate of the cell to be checked.
  * @param cls Class of objects to look return ('null' will return all objects).
  */
 @SuppressWarnings("unchecked")
 public List getObjectsAt(int x, int y, Class cls) {
   return collisionChecker.getObjectsAt(x, y, cls);
 }
Example #18
0
 /**
  * Return all the objects that intersect the given object. This takes the graphical extent of
  * objects into consideration.
  *
  * @param actor An Actor in the world
  * @param cls Class of objects to look for (null or Object.class will find all classes)
  */
 @SuppressWarnings("unchecked")
 List getIntersectingObjects(Actor actor, Class cls) {
   return collisionChecker.getIntersectingObjects(actor, cls);
 }
Example #19
0
 /**
  * Returns all objects with the logical location within the specified circle. In other words an
  * object A is within the range of an object B if the distance between the centre of the two
  * objects is less than r.
  *
  * @param x Centre of the cirle
  * @param y Centre of the cirle
  * @param r Radius of the cirle
  * @param cls Class of objects to look for (null or Object.class will find all classes)
  */
 @SuppressWarnings("unchecked")
 List getObjectsInRange(int x, int y, int r, Class cls) {
   return collisionChecker.getObjectsInRange(x, y, r, cls);
 }
Example #20
0
 @SuppressWarnings("unchecked")
 Actor getOneIntersectingObject(Actor object, Class cls) {
   return collisionChecker.getOneIntersectingObject(object, cls);
 }
Example #21
0
 @SuppressWarnings("unchecked")
 Actor getOneObjectAt(Actor object, int dx, int dy, Class cls) {
   return collisionChecker.getOneObjectAt(object, dx, dy, cls);
 }
Example #22
0
 /**
  * Return all objects that intersect a straight line from the location at a specified angle. The
  * angle is clockwise.
  *
  * @param x x-coordinate
  * @param y y-coordinate
  * @param angle The angle relative to current rotation of the object. (0-359)
  * @param length How far we want to look (in cells)
  * @param cls Class of objects to look for (passing 'null' will find all objects).
  */
 @SuppressWarnings("unchecked")
 List getObjectsInDirection(int x0, int y0, int angle, int length, Class cls) {
   return collisionChecker.getObjectsInDirection(x0, y0, angle, length, cls);
 }
 Actor getOneIntersectingObject(Actor object, Class cls) {
   return collisionChecker.getOneIntersectingObject(object, cls);
 }
 /**
  * Get all the objects in the world.<br>
  * If iterating through these objects, you should synchronize on this world to avoid
  * ConcurrentModificationException.
  *
  * <p>The objects are returned in their paint order. The first object in the List is the one
  * painted first. The last object is the one painted on top of all other objects.
  *
  * <p>If a class is specified as a parameter, only objects of that class (or its subclasses) will
  * be returned.
  *
  * <p>
  *
  * @param cls Class of objects to look for ('null' will find all objects).
  * @return An unmodifiable list of objects.
  */
 public synchronized List getObjects(Class cls) {
   return Collections.unmodifiableList(collisionChecker.getObjects(cls));
 }