Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Resource getSurroundingTiles

Author of MaxiBots
Joined
Dec 3, 2013
Messages
7,157
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:
    public static List<Coordinate> getSurroundingTiles(final Area area, final boolean corners) {
        final List<Coordinate> coordinates = new LinkedList<Coordinate>();
        for (int i=corners ? -1 : 0; i<area.getWidth();i++) {
            coordinates.add(area.getBottomLeft().derive(i, -1));
            coordinates.add(area.getTopRight().derive(-i, 1));
        }
        for (int i=0; i<area.getHeight() + (corners ? 1 : 0);i++) {
            coordinates.add(area.getBottomLeft().derive(-1, i));
            coordinates.add(area.getTopRight().derive(1, -i));
        }
        return coordinates;
    }
 
Last edited:
Engineer
Joined
Jul 28, 2013
Messages
2,776
A modified version of this method will be included in the Area.Rectangular class in the next release.
 
Top