Implementing the A* algorithm
First, we implement the basic classes that we introduced before, such as the Node
class, the GridManager
class, and the PriorityQueue
class. Then, we use them in the main AStar
class.
Node
The Node
class represents each tile object in the 2D grid. Its code is shown in the Node.cs
file:
using UnityEngine; using System; public class Node { Â Â Â Â public float costSoFar; Â Â Â Â public float fScore; Â Â Â Â public bool isObstacle; Â Â Â Â public Node parent; Â Â Â Â public Vector3 position; Â Â Â Â Â Â Â Â public Node(Vector3 pos) { Â Â Â Â Â Â Â Â fScore = 0.0f; Â Â Â Â Â Â Â Â costSoFar = 0.0f; Â Â Â Â Â Â Â Â isObstacle = false; Â Â Â Â Â Â Â Â parent = null; Â Â Â Â Â Â Â Â ...