2017-09-01 228 views
0

我想突出显示放置在六角形瓦片系统中的单元范围内的图块。例如,如果我在6 | 5上放置一个范围为2的单位,我想强调5 | 4,6 | 4,7 | 4,7 | 5,6 | 6,5 | 5,4 | 5,4 | 4,5 | 3等等...获取坐标系中相邻六角形瓦片的坐标

example map

如何计算从坐标原点的坐标和范围?目前,我使用很多if语句来检查每一个可能是这样的:

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 
     { 
       [...] 
     } 
    } 
} 

但随着范围,复杂性也增加了......必须有一个更好的办法。有任何想法吗?

回答

1

递归。与照亮运动可达到的格子相同。

+0

此外,这篇文章可能是值得拥有看看:[Naidamast](https://www.codeproject.com/Articles/1119973/Part-I-Creating-a-Digital-Hexagonal-瓦图) – MartinB

0

感谢MartinB,我尝试了递归方法,它像一个魅力一样工作。 :)

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; 
      } 
     }