unity A Star寻路算法

怎甘沉沦 提交于 2019-12-12 04:34:30

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AStar : MonoBehaviour
{
    private static AStar _instance;
    public static AStar Inst() { return _instance; }
    void Awake() { _instance = this; }

    public int width;//地图的宽(需要为双数)
    public int height;//地图的高(需要为双数)
    public const float gridWidth = 0.5f;//格子的宽高
    private const float gridGapDis = 0.05f;//格子之间的间隙

    public Point[,] map;//格子

    private List<Vector2> aimPosList_Test = new List<Vector2>();//测试用目标点位置列表

    void Start()
    {
        SetMapWH();//根据屏幕的高宽设置格子的个数
        map = new Point[width, height];//地图格子二维数组
        InitMap();//地图初始化
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))//鼠标左键点击添加目标点
        {
            Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            aimPosList_Test.Add(pos);
        }
        else if (Input.GetMouseButtonDown(1))//鼠标右键点击得到路径列表
        {
            FindPath(aimPosList_Test);
        }
    }

    public void InitMap()//初始化地图
    {
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                map[i, j] = new Point(i, j);
            }
        }
    }

    void OnDrawGizmos()
    {
        if (map == null) return;

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                if (map[i, j] != null)
                {
                    Point point = new Point(i, j);
                    if (map[i, j].IsObstacle) Gizmos.color = Color.red;
                    else if (map[i, j].isRoad) Gizmos.color = Color.black;
                    else Gizmos.color = Color.green;
                    Gizmos.DrawCube(PointToPos(point), new Vector3(gridWidth - gridGapDis, gridWidth - gridGapDis, 0.1f));
                }
            }
        }
    }

    //根据屏幕的宽高设置地图格子的宽高数量
    private void SetMapWH()
    {
        float worldMapWidth = Camera.main.orthographicSize / ((float)Screen.height / Screen.width) * 2f;//世界坐标下屏幕的宽度
        float worldMapHeight = Camera.main.orthographicSize * 2f;//世界坐标下屏幕的高度
        int tempWidth = (int)Math.Ceiling(worldMapWidth / gridWidth);
        int tempHeight = (int)Math.Ceiling(worldMapHeight / gridWidth);
        width = tempWidth % 2 == 0 ? tempWidth : tempWidth + 1;//向上取整
        height = tempHeight % 2 == 0 ? tempHeight : tempHeight + 1;//向上取整
    }

    //找到路径(多个目标点)
    public List<Vector2> FindPath(List<Vector2> aimPosList)
    {
        if (aimPosList.Count < 2)
        {
            Debug.Log("传入的列表长度至少为2个,包含起点和终点");
            return null;
        }

        List<Point> pointAimList = new List<Point>();
        for (int i = 0; i < aimPosList.Count; i++)
        {
            Point point = PosToPoint(aimPosList[i]);

            //过滤掉在障碍物的位置的目标点
            if (map[point.X, point.Y].IsObstacle) continue;

            pointAimList.Add(point);
        }

        List<Vector2> pathPointList = new List<Vector2>();
        for (int i = 0; i < pointAimList.Count - 1; i++)
        {
            List<Vector2> tempList = FindPath(pointAimList[i], pointAimList[i + 1]);
            if (tempList != null)
                tempList.ForEach((a) => { pathPointList.Add(a); });
        }

        return pathPointList;
    }

    //找到路径(开始位置,结束位置)
    public List<Vector2> FindPath(Point startPoint, Point endPoint)
    {
        startPoint = map[startPoint.X, startPoint.Y];//开始格子
        endPoint = map[endPoint.X, endPoint.Y];//结束格子

        List<Point> openList = new List<Point>();
        List<Point> closeList = new List<Point>();

        openList.Add(startPoint);

        while (openList.Count > 0)//只要开放列表还存在元素就继续
        {
            Point point = GetMinFOfList(openList);//选出open集合中F值最小的点
            openList.Remove(point);
            closeList.Add(point);
            List<Point> SurroundPoints = GetSurroundPoint(point.X, point.Y);

            foreach (Point p in closeList)//在周围点中把已经在关闭列表的点删除
            {
                if (SurroundPoints.Contains(p))
                {
                    SurroundPoints.Remove(p);
                }
            }

            foreach (Point p in SurroundPoints)//遍历周围的点
            {
                if (openList.Contains(p))//周围点已经在开放列表中
                {
                    //重新计算G,如果比原来的G更小,就更改这个点的父亲
                    int newG = 1 + point.G;
                    if (newG < p.G)
                    {
                        p.SetParent(point, newG);
                    }
                }
                else
                {
                    //设置父亲和F并加入开放列表
                    p.parent = point;
                    GetF(p, endPoint);
                    openList.Add(p);
                }
            }
            if (openList.Contains(endPoint))//只要出现终点就结束
            {
                break;
            }
        }

        //得到寻路路径的坐标点列表
        List<Vector2> pathList = new List<Vector2>();
        pathList.Insert(0, PointToPos(endPoint));
        map[endPoint.X, endPoint.Y].isRoad = true;
        Point temp = endPoint.parent;
        pathList.Insert(0, PointToPos(temp));
        map[temp.X, temp.Y].isRoad = true;
        while (temp != startPoint)
        {
            temp = temp.parent;
            map[temp.X, temp.Y].isRoad = true;
            pathList.Insert(0, PointToPos(temp));
        }

        return pathList;
    }

    //得到一个点周围的点(上下左右四个点,如果可以斜向走,就再加四个点)
    public List<Point> GetSurroundPoint(int x, int y)
    {
        List<Point> PointList = new List<Point>();

        //上下左右
        if (x > 0 && !map[x - 1, y].IsObstacle)
        {
            PointList.Add(map[x - 1, y]);
        }
        if (y > 0 && !map[x, y - 1].IsObstacle)
        {
            PointList.Add(map[x, y - 1]);
        }
        if (x < width - 1 && !map[x + 1, y].IsObstacle)
        {
            PointList.Add(map[x + 1, y]);
        }
        if (y < height - 1 && !map[x, y + 1].IsObstacle)
        {
            PointList.Add(map[x, y + 1]);
        }

        //斜向
        //if (x > 0 && y < height - 1 && !map[x - 1, y + 1].IsObstacle)
        //{
        //    PointList.Add(map[x - 1, y]);
        //}
        //if (x > 0 && y > 0 && !map[x - 1, y - 1].IsObstacle)
        //{
        //    PointList.Add(map[x, y - 1]);
        //}
        //if (x < width - 1 && y < height - 1 && !map[x + 1, y + 1].IsObstacle)
        //{
        //    PointList.Add(map[x + 1, y]);
        //}
        //if (x < width - 1 && y > 0 && !map[x + 1, y - 1].IsObstacle)
        //{
        //    PointList.Add(map[x, y + 1]);
        //}

        return PointList;
    }

    public void GetF(Point point, Point endPoint)//计算某个点的F值
    {
        int G = 0;
        int H = Mathf.Abs(endPoint.X - point.X) + Mathf.Abs(endPoint.Y - point.Y);
        if (point.parent != null)
        {
            G = 1 + point.parent.G;
        }
        int F = H + G;
        point.H = H;
        point.G = G;
        point.F = F;
    }

    public Point GetMinFOfList(List<Point> list)//得到一个集合中F值最小的点
    {
        int min = int.MaxValue;
        Point point = null;
        foreach (Point p in list)
        {
            if (p.F < min)
            {
                min = p.F;
                point = p;
            }
        }
        return point;
    }

    //坐标点转化为格子的x,y
    public Point PosToPoint(Vector2 pos)
    {
        int x = (int)((pos.x - (-width / 2 * gridWidth)) / gridWidth);
        int y = (int)((pos.y - (-height / 2 * gridWidth)) / gridWidth);
        return new Point(x, y);
    }

    //格子的x,y转化为坐标点
    public Vector2 PointToPos(Point point)
    {
        return new Vector2((point.X - width / 2) * gridWidth + gridWidth / 2, (point.Y - height / 2) * gridWidth + gridWidth / 2);
    }
}

public class Point
{
    public int X;
    public int Y;
    public int F;
    public int G;
    public int H;
    public Point parent = null;

    private bool isObstacle = false;
    public bool IsObstacle
    {
        get
        {
            if (AStar.Inst() == null) return isObstacle;

            Vector2 pos = AStar.Inst().PointToPos(this);
            //射线圆形范围内检测格子下方是否为障碍物
            RaycastHit2D hit = Physics2D.CircleCast(pos, AStar.gridWidth / 2f, Vector2.zero, 1 << 8);
            isObstacle = hit.transform != null;
            return isObstacle;
        }
        set => isObstacle = value;
    }

    public bool isRoad = false;//是否是路

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public void SetParent(Point parent, int g)
    {
        this.parent = parent;
        G = g;
        F = G + H;
    }
}
 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!