3-18 整理代码逻辑

  在之前的章节中,我们完成了各类高低差不同的地图单元之间,其三角形连接区域的构建,有些是阶梯化的,有些则是一半阶梯一半三角形的。所有的情况如下图所示


  
  判断地图单元格之间的连接关系,并决定使用哪种方式构建三角形连接区域的语句都在TriangulateCorner方法中。这里我们来整理一下这部分代码,用if else语句来代替现在的return。代码如下:

HexCell.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
private void TriangulateCorner(
Vector3 bottom, HexCell bottomCell,
Vector3 left, HexCell leftCell,
Vector3 right, HexCell rightCell)
{


if (leftEdgeType == HexEdgeType.Slope)
{
if (rightEdgeType == HexEdgeType.Slope)
{
TriangulateCornerTerraces(bottom, bottomCell, left, leftCell, right, rightCell);
}
else if (rightEdgeType == HexEdgeType.Flat)
{
TriangulateCornerTerraces(left, leftCell, right, rightCell, bottom, bottomCell);
}
else
{
TriangulateCornerTerracesCliff(bottom, bottomCell, left, leftCell, right, rightCell);
}
}
else if (rightEdgeType == HexEdgeType.Slope)
{
if (leftEdgeType == HexEdgeType.Flat)
{
TriangulateCornerTerraces(right, rightCell, bottom, bottomCell, left, leftCell);
}
else
{
TriangulateCornerCliffTerraces(bottom, bottomCell, left, leftCell, right, rightCell);
}
}
else if (leftCell.GetEdgeType(rightCell) == HexEdgeType.Slope)
{
if (leftCell.Elevation < rightCell.Elevation)
{
TriangulateCornerCliffTerraces(right, rightCell, bottom, bottomCell, left, leftCell);
}
else
{
TriangulateCornerTerracesCliff(left, leftCell, right, rightCell, bottom, bottomCell);
}
}
else
{
AddTriangle(bottom, left, right);
AddTriangleColor(bottomCell.color, leftCell.color, rightCell.color);
}
}
}

  至此,我们就完成了所有地图单元之间连接类型的判断,以及构建其相邻的三角形连接区域的功能了。相比之前没有任何段落感的地形,现在的地形层次更加丰富,立体感更强。在接下来的章节中,我们要继续丰富地图的细节,使用一张随机的噪点图,对地图上的单元素进行一定的形变。

Github代码