black_friday/Assets/Scripts/Pathfinder.cs
/// <summary>
/// Calculates the quickest distance between two points (Not ideal because of the manhattan distance hueristic)
/// </summary>
/// <param name="startingVertexPos">The starting position</param>
/// <param name="goalVertexPos">The goal position</param>
Calculates the quickest distance between two points (Not ideal because of the manhattan distance hueristic)
Calculates the quickest distance between two points ( Not ideal because of the manhattan distance hueristic )
public void AStar(PathNode start, PathNode end)
        {
            PathNode startingVertex = start;
            PathNode goalVertex = end;
            openSet = new PriorityQueue<PathNode>();
            openSet.Enqueue(startingVertex);
            closedSet = new List<PathNode>();
            PathNode currentVertex;
            while (openSet.Peek() != goalVertex)
            {
                currentVertex = openSet.Dequeue();
                closedSet.Add(currentVertex);
                currentVertex.InOpenList[id] = false;
                currentVertex.InClosedList[id] = true;
                List<PathNode> neighbors = currentVertex.AdjacentVertices;
                for (int i = 0; i < neighbors.Count; i++)
                {
                    PathNode neighbor = neighbors[i];
                    float H = Vector3.Distance(neighbor, goalVertex);
                    if (neighbor.InClosedList[id])
                    {
                        continue;
                    }
                    if (!neighbor.InOpenList[id])
                    {
                        neighbor.InOpenList[id] = true;
                        neighbor.InClosedList[id] = false;
                        neighbor.Parent[id] = currentVertex;
                        neighbor.G = currentVertex.G + 1;
                        neighbor.H = H;
                        neighbor.F = neighbor.G + neighbor.H;
                        openSet.Enqueue(neighbor);
                    }
                    else
                    {
                        if (currentVertex.G + 1 < neighbor.G)
                        {
                            neighbor.Parent[id] = currentVertex;
                            neighbor.G = currentVertex.G + 1;
                            neighbor.F = neighbor.G + neighbor.H;
                        }
                    }
                }
            }
        }
public void AStar ( PathNode start , PathNode end ) { PathNode startingVertex = start ; PathNode goalVertex = end ; openSet = new PriorityQueue < PathNode > ( ) ; openSet . Enqueue ( startingVertex ) ; closedSet = new List < PathNode > ( ) ; PathNode currentVertex ; while ( openSet . Peek ( ) != goalVertex ) { currentVertex = openSet . Dequeue ( ) ; closedSet . Add ( currentVertex ) ; currentVertex . InOpenList [ id ] = false ; currentVertex . InClosedList [ id ] = true ; List < PathNode > neighbors = currentVertex . AdjacentVertices ; for ( int i = 0 ; i < neighbors . Count ; i ++ ) { PathNode neighbor = neighbors [ i ] ; float H = Vector3 . Distance ( neighbor , goalVertex ) ; if ( neighbor . InClosedList [ id ] ) { continue ; } if ( ! neighbor . InOpenList [ id ] ) { neighbor . InOpenList [ id ] = true ; neighbor . InClosedList [ id ] = false ; neighbor . Parent [ id ] = currentVertex ; neighbor . G = currentVertex . G + 1 ; neighbor . H = H ; neighbor . F = neighbor . G + neighbor . H ; openSet . Enqueue ( neighbor ) ; } else { if ( currentVertex . G + 1 < neighbor . G ) { neighbor . Parent [ id ] = currentVertex ; neighbor . G = currentVertex . G + 1 ; neighbor . F = neighbor . G + neighbor . H ; } } } } }
Calculates the quickest distance between two points (Not ideal because of the manhattan distance hueristic)
Calculates the quickest distance between two points ( Not ideal because of the manhattan distance hueristic )
//Reset the the open and closed lists of verticies //Add the starting vertex to the open list //Loop through the open set until we reach the goal vertex //Set the current vertex to the first vertex in the priority queue //Move it to the closed list //for neighbors of current //Calculate the neighbors hueristic //Math.Abs(neighbor.X - goalVertex.X) + Math.Abs(neighbor.Y - goalVertex.Y); //If it's in the closed list skip this neighbor //If it's not in the open list //Make it in the open list //Set its parent to the current vertex //Calculate distances //Add it to the open list //If its not in the open list, but this path is more ideal //Change its parent and recalculate distances
start
PathNode
end
PathNode
startingVertexPos
The starting position
The starting position
goalVertexPos
The goal position
The goal position