Get coordinates of adjacent hexagon tiles in a coordinate system

513 views Asked by At

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...

example map

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?

2

There are 2 answers

0
Marco Frost On

Thanks to MartinB, I tried the recursive approach and it worked like a charm. :)

private void HighlightRange(int originX, int originY, int range, bool greenRange = true)
        {
            if (range > 0)
            {
                List<Tuple<int, int>> hexCoordinates = new List<Tuple<int, int>>();
                if (originX % 2 == 0)
                {
                    hexCoordinates.Add(new Tuple<int, int>(originX, originY - 1));
                    hexCoordinates.Add(new Tuple<int, int>(originX - 1, originY - 1));
                    hexCoordinates.Add(new Tuple<int, int>(originX - 1, originY));
                    hexCoordinates.Add(new Tuple<int, int>(originX, originY + 1));
                    hexCoordinates.Add(new Tuple<int, int>(originX + 1, originY));
                    hexCoordinates.Add(new Tuple<int, int>(originX + 1, originY - 1));
                }
                else
                {
                    hexCoordinates.Add(new Tuple<int, int>(originX, originY - 1));
                    hexCoordinates.Add(new Tuple<int, int>(originX - 1, originY));
                    hexCoordinates.Add(new Tuple<int, int>(originX - 1, originY + 1));
                    hexCoordinates.Add(new Tuple<int, int>(originX, originY + 1));
                    hexCoordinates.Add(new Tuple<int, int>(originX + 1, originY + 1));
                    hexCoordinates.Add(new Tuple<int, int>(originX + 1, originY));
                }
                hexCoordinates.RemoveAll(t => (t.Item1 < 0 || t.Item1 >= length || t.Item2 < 0 || t.Item2 >= height));

                while (hexCoordinates.Count > 0)
                {
                    if (range > 1)
                    {
                        HighlightRange(hexCoordinates[0].Item1, hexCoordinates[0].Item2, range - 1, greenRange);
                    }
                    if (greenRange)
                    {
                        gameField[hexCoordinates[0].Item1, hexCoordinates[0].Item2].IsGreenRange = true;
                    }
                    else
                    {
                        gameField[hexCoordinates[0].Item1, hexCoordinates[0].Item2].IsRedRange = true;
                    }
                    hexCoordinates.RemoveAt(0);
                }
            }
            else
            {
                return;
            }
        }
1
MartinB On

Recursion. Same as you would for illuminating which hexes you can reach by movement.