public PathFinderDijkstra(NodeDi start, NodeDi end) { Stopwatch sw = new Stopwatch(); sw.Start(); height = TileSetManager.height; width = TileSetManager.width; matrix = new NodeDi[height, width]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { matrix[i, j] = TileSetManager.GetNodeDi(i, j); matrix[i, j].isOpen = true; } } pathList = new List <NodeDi>(); NodeDi node = DijkstrasAlgo(matrix[start.i, start.j], matrix[end.i, end.j]); while (node != null) { pathList.Add(node); node = node.parent; } pathList.Reverse(); sw.Stop(); time = (sw.ElapsedTicks / (float)Stopwatch.Frequency) * 1000; }
public static NodeDi GetNodeDi(int i, int j) { if (i >= 0 && j >= 0) { if (j < width && i < height) { var type = tileMap[i, j].type; var node = new NodeDi(i, j, type == 0 ? false : true); return(node); } } return(null); }
public override bool Equals(object obj) { NodeDi node = obj as NodeDi; if (node.i == i && node.j == j) { return(true); } else { return(false); } }
// возвращает соседей указанной ячейки (тайла) private List <NodeDi> GetNeighbours(NodeDi node) { List <NodeDi> retList = new List <NodeDi>(); NodeDi[] temp = new NodeDi[] { matrix[node.i - 1, node.j], matrix[node.i + 1, node.j], matrix[node.i, node.j - 1], matrix[node.i, node.j + 1] }; foreach (var retVal in temp) { if (retVal != null && retVal.isWalkable && retVal.isOpen) { retList.Add(retVal); } } //UnityEngine.Debug.Log("nei count: " + retList.Count.ToString()); return(retList); }
private NodeDi DijkstrasAlgo(NodeDi start, NodeDi end) { openList = new List <NodeDi>(); workList = new List <NodeDi>(); start.setWeight(0); start.isOpen = false; openList.Add(start); while (openList.Count > 0) { int index = GetMinFNodeAS(); if (index < 0) { //UnityEngine.Debug.Log("index -1"); break; } NodeDi current = openList[index]; openList.RemoveAt(index); workList.Add(current); if (current.Equals(end)) { return(current); } foreach (NodeDi neighNode in GetNeighbours(current)) { float distance = Vector3.Distance(neighNode.getPosition(), current.getPosition()); distance = current.weight + distance; neighNode.isOpen = false; neighNode.setWeight(distance); neighNode.setParentNode(current); openList.Add(neighNode); } } return(null); }
// -------------------------------- Setters -------------------------------- public void setParentNode(NodeDi node) { parent = node; }
public void resetNode() { weight = int.MaxValue; parent = null; }