Resource getSurroundingTiles

Discussion in 'Tutorials & Resources' started by Aidden, Sep 12, 2014.

  1. Here's a quick code snippet from a method i find useful.
    It will return a list of the tiles around the outside of an area.
    I guess it's the bounding tiles of the areas outter tiles.
    If corners is set to true it will also include the four corners which may or may not be useful.

    This can be used to check if an object such as a tree is reachable. As you can't actually walk on the coordinate that the tree is sitting on, tree.isReachable() would return false. With this you can simply loop through the returned list and check if any of those coordinates is reachable, if so the tree itself is reachable.
    Code (Text):
    2.     public static List<Coordinate> getSurroundingTiles(final Area area, final boolean corners) {
    3.         final List<Coordinate> coordinates = new LinkedList<Coordinate>();
    4.         for (int i=corners ? -1 : 0; i<area.getWidth();i++) {
    5.             coordinates.add(area.getBottomLeft().derive(i, -1));
    6.             coordinates.add(area.getTopRight().derive(-i, 1));
    7.         }
    8.         for (int i=0; i<area.getHeight() + (corners ? 1 : 0);i++) {
    9.             coordinates.add(area.getBottomLeft().derive(-1, i));
    10.             coordinates.add(area.getTopRight().derive(1, -i));
    11.         }
    12.         return coordinates;
    13.     }
    #1 Aidden, Sep 12, 2014
    Last edited: Sep 20, 2014
  2. A modified version of this method will be included in the Area.Rectangular class in the next release.
    Aidden likes this.

Share This Page