// the following is for the main body of the A* algorithm void ObtainPathbyVector(Vector2 Start, Vector2 End) { // the following line of code assigns the starting node from the grid to the nodes script Node start = World.GetNodeFromWorld(Start); // the code underneath assigns the end node from the grid to the nodes script Node end = World.GetNodeFromWorld(End); // this is to store all available points List <Node> OpenPoints = new List <Node>(); HashSet <Node> ClosePoints = new HashSet <Node>(); // the following code adds the start point to the openlist OpenPoints.Add(start); // the loop below is executed until there are no open points left while (OpenPoints.Count > 0) { // the following line of code is used to initialise a current position Node CurrentPosition = OpenPoints[0]; // for loop to run through f.cost is compared to openpoints for (int i = 0; i < OpenPoints.Count; i++) { // the following if statement checks whether the openpoints Fcos is smaller than the Fcos of the current position, // or the openpoints Fcos is equal to the Fcos of the current position if (OpenPoints[i].GetFcos < CurrentPosition.GetFcos || OpenPoints[i].GetFcos == CurrentPosition.GetFcos) { // the following if statement checks whether the Hcos of the openpoints is smaller than // the Hcos of the current position if (OpenPoints[i].Hcos < CurrentPosition.Hcos) { //so now the current position will equal to whatever the most efficient path is CurrentPosition = OpenPoints[i]; } } } // will store the most efficient path ClosePoints.Add(CurrentPosition); // removes the currentposition and moves on OpenPoints.Remove(CurrentPosition); // checks whether the current position is equal to the destination node if (CurrentPosition == end) { PathGeneration(start, end); //exits the loop return; } foreach (Node sidenode in World.NeighbourOfPoint(CurrentPosition)) { // the following code gets access to unwalkable areas if (!sidenode.Walkable || ClosePoints.Contains(sidenode))// we say this because firstly we dont need to have access to unwalkable areas //secondly there may be times where one of our side nodes has already been added to our closed list so we want to ignore those sidenodes { // the continues ignores nodes that have already been added to the closed list and moves on continue; } // will calculate any distances int MovementCost = CurrentPosition.Gcos + GetMoveCost(CurrentPosition, sidenode); // checks if this sidenode is good or not if (MovementCost < sidenode.Gcos || !OpenPoints.Contains(sidenode)) { // makes the Gcos value of the side node equal to movementcost sidenode.Gcos = MovementCost; // is where the value of Hcos is calculated sidenode.Hcos = GetMoveCost(sidenode, end); // need this parent to back track our path sidenode.Parent = CurrentPosition; // checks whether the open points dont contain sidenode if (!OpenPoints.Contains(sidenode)) { // adds new points to the list OpenPoints.Add(sidenode); } } } } }