- Home /
Selecting a range of hex tiles within radius of main tile
Using a grid of hexes I generate, i can hover over any tile and it will highlight just fine. Now what I'm trying to figure out is how to allow for a radial selection of tiles around that hovered tile (brush size basically). Eventually allowing for a somewhat radial soft-selection gradient to allow falloff.
Each hex already knows about their neighbors, so currently I am hard-coding logic to handle a radius of 1, 2 or 3. I could duplicate that logic over and over to handle bigger radii but this is definitely not going to work for much longer, so I am trying to figure out a more efficient and cleaner way using loops/recursion/whatever.
Can anyone offer any pseudo-code or possibly a website that explains how to do this? Below is a (probably bad) attempt at what i think it would look like.
OnMouseOver(){
// This would select a radius of x hex tiles including center tile
gameObject.GetComponent<HexTile>().SelectTile(3);
}
SelectTile(selectionRadius){
if(!thisTile.alreadySelected){
thisTile.Select();
}
for(int i = 0; i <= selectionRadius; ++i){
if(i < selectionRadius){
foreach(HexTile tile in thisTile.neighbors){
tile.SelectTile(selectionRadius - i);
}
}
}
}
Edit: Im using this fantastic resource to help with hexes - http://www.redblobgames.com/grids/hexagons/ - so might there be a section on this page that may explain what i need and im just missing?
Answer by Mungyun · Dec 26, 2013 at 07:53 PM
Gah, i knew as i posted this i would see something i missed.. http://www.redblobgames.com/grids/hexagons/#range