A star pathfinding is used when you want the enemy AI to have an intelligent behavior in which given the location of the player, the enemy will be able to navigate through the map and get to the location of the player using the shortest distance.
To calculate the shortest path, we will find the cost of getting to the location of the player. This cost, called the F cost, is the total of the G cost and H cost. The G cost is the cost of moving from the starting point of the enemy to the current position of the object. The H cost is the cost of the moving from the current location to the destination, which is the location of the player.
From a position, the player can move up, down, left, or right, and for each movement, the cost is calculated. There are two arrays created that keep track of the squares that are open and the others that are closed. The open array list keeps track of the squares that can be considered to calculate the shortest path, and the closed list contains boxes...