How to find tiles within X range?
This is probably a bit longer, so bear with me:
I have the following field (Yes, the textures are garbage, ignore them. ;P) :
Now, the playing field is seperated into tiles, each of those blocks is one tile. A character has a certain move range, for example, 4. This would mean he could move on these tiles:
Now, the question for me is, how do I figure out that range? At first I thought about drawing a circle over the block the player stands on with the circle diameter being 2 x range. That doesn't quite work, sadly, it always touches blocks that are out of range.
My second idea was finding the adjacent blocks via their names. The block the player stands on, for example, is called "GridElement8". Now, the adjacent blocks north and south will be the 7 and 9. In order to figure out the west and east blocks, I wrote a method to figure out the length of the board and subtract/add the result to the block number, meaning I had 8 - 6 = 2 and 8 - 6 = 14. Which does work in this case.
Sadly, that doesn't work if the board is not in rectangular shape. As soon as one block falls out of order, I'm in trouble. So I can't use that method either.
Now I'm kinda out of options. One other idea I had was to write a method that find's the adjacent blocks independent of the name, call it recursively maybe with collission? But how, the blocks ain't rigidbodies and shouldn't be, and I don't want to find the adjacent tiles by iterating through the entire board and finding them by their positions.
I quickly mocked up a pseudocode, maybe it explains my problems a bit better (or makes it even worse to understand)
public List<GameObject> findAllTilesInRange(GameObject targetTile, int range) {
List<GameObject> gameObjectList = new List<GameObject>();
List<GameObject> gameObjectList2 = new List<GameObject>();
//abort condition
if (range == 0) {
return gameObjectList;
}
gameObjectList = targetTile.GetComponent<GridElementController>().findAdjacentTiles();
foreach(GameObject gameObject in gameObjectList) {
// find all Tiles in Range again, but subtract range by one
gameObjectList2 = gameObject.GetComponent<GridElementController>().findAllTilesInRange(gameObject, range - 1);
//merge the returned list with the old one
List.Merge(gameObjectList, gameObjectList2);
//make it unique again
gameObjectList.Unique();
}
return gameObjectList;
}
I'm kinda clueless on how to find the adjacent tiles tho ... once I figure that out, getting the range is probably relatively easy.
I hope you can help me here, because that's actually a very damn essential function of the game and I can't really continue until I figure this out. :(
Also, I'm really sorry about the formatting, but it just won't accept my line breaks.
Your answer
Follow this Question
Related Questions
How to make gridbased movement while also using collision? 0 Answers
[Unity 2D]Grid Based Character Movement Issues with Vector3D.movetowards 0 Answers
Help with adding collision to GridMove script 0 Answers
How to provide a chraracter to dissamble the parts of a bridge by range while crossing? 0 Answers
My collisions dont detect in gridbased system (check if in certain area) 0 Answers