本文整理汇总了C#中PathNode.setG方法的典型用法代码示例。如果您正苦于以下问题:C# PathNode.setG方法的具体用法?C# PathNode.setG怎么用?C# PathNode.setG使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PathNode
的用法示例。
在下文中一共展示了PathNode.setG方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPath
public List<MapGrid> GetPath(int fromGridX, int fromGridZ, int toGridX, int toGridZ, TileType validType)
{
List<MapGrid> path = new List<MapGrid>();
if ((this[fromGridX, fromGridZ] & validType) == 0)
return path;
if ((this[toGridX, toGridZ] & validType) == 0)
return path;
// 同一个点也导出路径
if(fromGridX == toGridX && fromGridZ == toGridZ)
{
path.Add(new MapGrid(){x = fromGridX, z = fromGridZ});
path.Add(new MapGrid(){x = toGridX, z = toGridZ});
return path;
}
PathNode pnFrom = new PathNode(fromGridZ * gridXNum + fromGridX, 0, 0, 0, this);
PathNode pnTo = new PathNode(toGridZ * gridXNum + toGridX, 0, 0, 0, this);
Dictionary<int, PathNode> totalDic = new Dictionary<int, PathNode>();
Dictionary<int, PathNode> openDic = new Dictionary<int, PathNode>();
Dictionary<int, PathNode> closeDic = new Dictionary<int, PathNode>();
openDic.Add(pnFrom.index, pnFrom);
totalDic.Add(pnFrom.index, pnFrom);
float disCornor = Mathf.Sqrt(MapGrid.Width * MapGrid.Width + MapGrid.Height * MapGrid.Height);
NodeOffset[] offsets = new NodeOffset[]{new NodeOffset(gridXNum-1, disCornor), new NodeOffset(gridXNum, MapGrid.Height),new NodeOffset(gridXNum+1, disCornor),
new NodeOffset(-1, MapGrid.Width), new NodeOffset(1, MapGrid.Width),
new NodeOffset(-gridXNum-1, disCornor), new NodeOffset(-gridXNum, MapGrid.Height),new NodeOffset(-gridXNum+1, disCornor)};
while(openDic.Count > 0)
{
PathNode pn = null;
foreach(KeyValuePair<int, PathNode> t in openDic)
{
if (pn == null) pn = t.Value;
if (t.Value.f < pn.f) pn = t.Value;
}
closeDic.Add(pn.index, pn);
openDic.Remove(pn.index);
if (pn.index == pnTo.index) break;
foreach(NodeOffset offset in offsets)
{
int index = pn.index + offset.offset;
if (index < 0 || index > grids.Length) continue;
if (closeDic.ContainsKey(index)) continue;
if ((grids[index] & validType) == 0) continue;
PathNode pntmp = null;
float g = pn.g + offset.distance;
if (openDic.ContainsKey(index))
{
pntmp = openDic[index];
if (g < pntmp.g)
{
pntmp.parentIndex = pn.index;
pntmp.setG(g);
}
}
else
{
pntmp = new PathNode(index, pn.index, 0, 0, this);
openDic[index] = pntmp;
totalDic[index] = pntmp;
pntmp.h = Mathf.Abs(Vector3.Distance(pntmp.position, pnTo.position));
pntmp.setG(g);
}
}
}
if(!closeDic.ContainsKey(pnTo.index))
return path;
List<MapGrid> tmpPath = new List<MapGrid>();
PathNode node = closeDic[pnTo.index];
while(node != null)
{
tmpPath.Add(node.grid);
if(node.parentIndex == 0)
break;
node = totalDic[node.parentIndex];
}
path = LinePathEx(tmpPath, validType);
return path;
}