implement a* pathfinding for unity in csharp

main.cs
using System.Collections.Generic;
using UnityEngine;

public class AStar : MonoBehaviour
{
    private List<Node> OpenList = new List<Node>();
    private List<Node> ClosedList = new List<Node>();
    private List<Node> FinalPath = new List<Node>();

    public List<Node> FindPath(Node startNode, Node targetNode)
    {
        OpenList = new List<Node> { startNode };
        ClosedList = new List<Node>();
        
        while (OpenList.Count > 0)
        {
            Node currentNode = OpenList[0];
            for (int i = 1; i < OpenList.Count; i++)
            {
                if (OpenList[i].FCost < currentNode.FCost || OpenList[i].FCost == currentNode.FCost && OpenList[i].hCost < currentNode.hCost)
                {
                    currentNode = OpenList[i];
                }
            }
            OpenList.Remove(currentNode);
            ClosedList.Add(currentNode);

            if (currentNode == targetNode)
            {
                GetFinalPath(startNode, targetNode);
                break;
            }

            foreach (Node neighborNode in GetNeighborNodes(currentNode))
            {
                if (!neighborNode.isWalkable || ClosedList.Contains(neighborNode))
                {
                    continue;
                }

                int moveCost = currentNode.gCost + GetManhattenDistance(currentNode, neighborNode);
                if (moveCost < neighborNode.gCost || !OpenList.Contains(neighborNode))
                {
                    neighborNode.gCost = moveCost;
                    neighborNode.hCost = GetManhattenDistance(neighborNode, targetNode);
                    neighborNode.Parent = currentNode;

                    if (!OpenList.Contains(neighborNode))
                        OpenList.Add(neighborNode);
                }
            }
        }
        return FinalPath;
    }

    private int GetManhattenDistance(Node nodeA, Node nodeB)
    {
        int dstX = Mathf.Abs(nodeA.gridX - nodeB.gridX);
        int dstY = Mathf.Abs(nodeA.gridY - nodeB.gridY);
        return dstX + dstY;
    }

    private List<Node> GetNeighborNodes(Node currentNode)
    {
        List<Node> neighborNodes = new List<Node>();
        // Add logic to get neighboring nodes based on your grid setup
        return neighborNodes;
    }

    private void GetFinalPath(Node startNode, Node endNode)
    {
        FinalPath = new List<Node>();
        Node currentNode = endNode;
        while (currentNode != startNode)
        {
            FinalPath.Add(currentNode);
            currentNode = currentNode.Parent;
        }
        FinalPath.Reverse();
    }
}

public class Node
{
    public bool isWalkable;
    public int gridX;
    public int gridY;
    public int gCost;
    public int hCost;
    public Node Parent;
    
    public int FCost { get { return gCost + hCost; } }

    // Add additional properties if needed
}
2900 chars
96 lines

gistlibby LogSnag