I want to highlight tiles in range of a unit which is placed in a system of hexagonal tiles. For example if I place a unit with range=2 on 6|5, I want to highlight 5|4, 6|4, 7|4, 7|5, 6|6, 5|5, 4|5, 4|4, 5|3 and so on...
How can I calculate those coordinates from the origin coordinate and the range? At the moment I use many if clauses to check every possibility like this:
if (gameField[x, y].IsHighlighted && gameField[x, y].DeployedUnit != null)
{
if (gameField[x, y].DeployedUnit.AttackRange > 0)
{
if (x % 2 == 0)
{
if (x > 0 && y > 0)
{
gameField[x - 1, y - 1].IsGreenRange = true;
}
if (x > 0)
{
gameField[x - 1, y].IsGreenRange = true;
}
if (y < height - 1)
{
gameField[x, y + 1].IsGreenRange = true;
}
if (x < length - 1)
{
gameField[x + 1, y].IsGreenRange = true;
}
if (x < length - 1 && y > 0)
{
gameField[x + 1, y - 1].IsGreenRange = true;
}
if (y > 0)
{
gameField[x, y - 1].IsGreenRange = true;
}
}
else
{
[...]
}
}
}
But with increasing range, the complexity also increases... There has to be a better way. Any ideas?
Thanks to MartinB, I tried the recursive approach and it worked like a charm. :)