public Node_AStar(Node_AStar copiedNode) { this.X = copiedNode.X; this.Z = copiedNode.Z; this.Parent = copiedNode.Parent; this.Passable = copiedNode.Passable; this.DistanceFromStart = copiedNode.DistanceFromStart; this.DistanceToDestination = copiedNode.DistanceToDestination; this.CostToDestination = copiedNode.CostToDestination; }
private bool NodeInClosedList(Node_AStar checkedNode) { for (int i = 0; i < closedList.Count; i++) { if (closedList[i] == checkedNode) { return(true); } } return(false); }
private bool NodeInOpenList(Node_AStar checkedNode) { for (int i = 0; i < openList.Count; i++) { if (openList[i] == checkedNode) { return(true); } } return(false); }
private void Refresh(Scratch_TileData[,] tileData, Scratch_TileData destinationTile) { openList.Clear(); closedList.Clear(); FinalTrack.Clear(); currentNode = null; for (int z = 0; z < column; z++) { for (int x = 0; x < row; x++) { node[x, z].Initialize(tileData[x, z], destinationTile); } } }
public Scratch_AStar(Scratch_MapData mapData) { row = mapData.TilesRow; column = mapData.TilesColumn; currentNode = null; node = new Node_AStar[row, column]; for (int z = 0; z < column; z++) { for (int x = 0; x < row; x++) { node[x, z] = new Node_AStar(mapData.TileData[x, z]); } } openList = new List <Node_AStar>(); closedList = new List <Node_AStar>(); FinalTrack = new List <Node_AStar>(); }
public bool FindPath(Scratch_TileData[,] tileData, Scratch_TileData startingTile, Scratch_TileData destinationTile, bool doorTile = false) { Refresh(tileData, destinationTile); currentNode = node[startingTile.X, startingTile.Z]; closedList.Add(currentNode); int failureCount = row * column; if (doorTile == true) { node[destinationTile.X, destinationTile.Z].Passable = true; } while (true) { for (int z = currentNode.Z - 1; z < currentNode.Z + 2; z++) { for (int x = currentNode.X - 1; x < currentNode.X + 2; x++) { if (NodeIndexAvailable(x, z) == false) { continue; } if (node[x, z].Passable == false) { continue; } if (NodeInClosedList(node[x, z]) == true) { continue; } if (NodeInOpenList(node[x, z]) == false) { node[x, z].Parent = currentNode; node[x, z].CalculateCostToDestination(); openList.Add(node[x, z]); } else { Node_AStar newData = new Node_AStar(node[x, z]); newData.Parent = currentNode; newData.CalculateCostToDestination(); if (newData.CostToDestination < node[x, z].CostToDestination) { node[x, z].Parent = currentNode; node[x, z].CalculateCostToDestination(); } } } } int lowestCost = 99999999; for (int i = 0; i < openList.Count; i++) { if (openList[i].CostToDestination < lowestCost) { lowestCost = openList[i].CostToDestination; currentNode = openList[i]; } } if (currentNode == node[destinationTile.X, destinationTile.Z]) { int whileBreaker = row * column; if (doorTile == true) { currentNode = currentNode.Parent; } while (true) { FinalTrack.Add(currentNode); if (currentNode == node[startingTile.X, startingTile.Z]) { FinalTrack.Reverse(); return(true); } currentNode = currentNode.Parent; whileBreaker--; if (whileBreaker < 0) { return(false); } } } openList.Remove(currentNode); closedList.Add(currentNode); failureCount--; if (failureCount < 0) { return(false); } } }