unity二维数组A*寻路

2023-10-27

public class AStarData
{
    public float G;//到下一个的距离
    public float H;//到终点的步数
    public float F;//G+H
    public Vector2 pos;//坐标
    public AStarData last;//上一个

    public AStarData(Vector2 pos)
    {
        this.pos = pos;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AStar : Singleton<AStar>
{
    int[,] arr;
    Vector2 begin;
    Vector2 end;
    bool isEight = false;//是否八方向寻路
    //取出数组中第一个元素out没有第一个元素的数组
    public AStarData Shift(List<AStarData> list, out List<AStarData> outlist)
    {
        AStarData data = list[0];
        List<AStarData> arr = new List<AStarData>();
        for (int i = 1; i < list.Count; i++)
        {
            arr.Add(list[i]);
        }
        outlist = arr;
        return data;
    }
    //获取点在数据集合中的位置
    public int GetIndex(Vector2 pos, List<AStarData> list)
    {
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i].pos == pos)
            {
                return i;
            }
        }
        return -1;
    }
    //获取路径(起点,终点,二维数组地图)
    public List<Vector2> GetPath(Vector2 begin,Vector2 end, int[,] arr)
    {
        this.begin = begin;
        this.end = end;
        this.arr = arr;
        List<AStarData> openlist = new List<AStarData>();
        List<AStarData> closedlist = new List<AStarData>();
        List<Vector2> paths = new List<Vector2>();
        openlist.Add(new AStarData(begin));
        do
        {
            AStarData data = Shift(openlist, out List<AStarData> newlist);
            openlist = newlist;
            closedlist.Add(data);
            //找到终点就结束
            if (data.pos == end)
            {
                //查找所有父级返回路径
                AStarData last = data;
                do
                {
                    paths.Add(last.pos);
                    last = last.last;
                } while (last != null);
                break;
            }
            //获取所有可走的路
            List<Vector2> list = GetSetp(data.pos, closedlist);
            //想获取最短路径记得给list排序,目前是不排序直接返回第一条路径
            for (int i = 0; i < list.Count; i++)
            {
                Vector2 p = list[i];
                AStarData step = new AStarData(p);
                int index = GetIndex(p, openlist);
                //判断路是否存在open集合里,不存在则创建,存在则留下G比较小的
                if (index == -1)
                {
                    step.last = data;
                    step.G = data.G + Vector2.Distance(p, data.pos);
                    step.H = Mathf.Abs((int)end.x - (int)p.x) + Mathf.Abs((int)end.y - (int)p.y);
                    step.F = step.G + step.H;
                    openlist.Add(step);
                }
                else
                {
                    step = openlist[index];
                    if (data.G + Vector2.Distance(p, data.pos) < step.G)
                    {
                        step.G = data.G + Vector2.Distance(p, data.pos);
                        step.F = step.G + step.H;
                        openlist.Add(step);
                    }
                }
            }
            //重新排序open集合
            openlist.Sort((a, b) =>
            {
                if (a.F < b.F)
                {
                    return -1;
                }
                else if (a.F == b.F)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            });
        } while (openlist.Count > 0);
        return paths;
    }
    public List<Vector2> GetSetp(Vector2 pos, List<AStarData> closedlist)
    {
        List<Vector2> list = new List<Vector2>();
        if (AddOpen(pos.x, pos.y + 1, closedlist))
        {
            list.Add(new Vector2(pos.x, pos.y + 1));
        }
        if (AddOpen(pos.x + 1, pos.y, closedlist))
        {
            list.Add(new Vector2(pos.x + 1, pos.y));
        }
        if (AddOpen(pos.x, pos.y - 1, closedlist))
        {
            list.Add(new Vector2(pos.x, pos.y - 1));
        }
        if (AddOpen(pos.x - 1, pos.y, closedlist))
        {
            list.Add(new Vector2(pos.x - 1, pos.y));
        }
        if (isEight)
        {
            if (AddOpen(pos.x + 1, pos.y + 1, closedlist))
            {
                list.Add(new Vector2(pos.x + 1, pos.y + 1));
            }
            if (AddOpen(pos.x + 1, pos.y - 1, closedlist))
            {
                list.Add(new Vector2(pos.x + 1, pos.y - 1));
            }
            if (AddOpen(pos.x - 1, pos.y - 1, closedlist))
            {
                list.Add(new Vector2(pos.x - 1, pos.y - 1));
            }
            if (AddOpen(pos.x - 1, pos.y + 1, closedlist))
            {
                list.Add(new Vector2(pos.x - 1, pos.y + 1));
            }
        }
        return list;
    }
    public bool AddOpen(float x, float y, List<AStarData> closedlist)
    {
        Vector2 pos = new Vector2(x, y);
        
        if (x >= 0 && x < arr.GetLength(0) && y >= 0 && y < arr.GetLength(1) && arr[(int)x, (int)y] == 0 && GetIndex(pos, closedlist) == -1)
        {
            return true;
        }
        return false;
    }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

unity二维数组A*寻路 的相关文章

  • 如何以编程方式移动 Windows 任务栏?

    我想知道任何类型的 API 或解决方法 例如脚本或注册表 来将 Windows 任务栏移动 或调整大小 到另一个位置 包括另一个显示器 如果是双显示器 当然 我们可以使用鼠标来移动任务栏 但我想通过程序或某种自动化方式来移动它 我试图找到
  • Windows Azure 虚拟机在扩展时访问网络速度很慢

    我正在我的小型 azure VM 上运行一些启动脚本 cmd bat 其中包括从已安装的 VHD 进行文件传输操作 通常会在大约 3 分钟内完成 复制文件并使用命令行提取 500Mb zip 文件 7z 当我扩展到约 150 个实例时 相同
  • 如何仅获取窗口的可见部分(Windows、gdi32、user32 等)

    我只想获取窗口中窗口的可见部分 作为一个区域 只想获取用户看到的区域 当然 以编程方式 这是一个例子 我有以下窗口组成 A C
  • c Sharp exe 在打开时应要求“以管理员身份运行”提示

    我有一个由 c Sharp 程序生成的 exe 当我运行 exe 时 我希望出现 UAC 提示 并提供以管理员身份运行 exe 的选项 我见过默认以管理员身份运行 exe 的示例 但是 如何让 UAC 要求我以管理员身份运行 exe 任何想
  • 使用标准用户帐户在提升的脚本中获取登录用户名

    在 Windows 7 中运行的批处理脚本中 我有几个 IF 子句 例如 IF USERNAME foo GOTO bar 不幸的是 当我运行这个批处理脚本时 用 以管理员身份运行 从上下文菜单中 USERNAME 始终是管理员的用户名 而
  • 导出的 DLL 函数未按词法排序?

    嗯 今天我遇到了一个奇怪的事情 我不久前编写了自己的 GetProcAddress 版本 用于从远程进程获取函数地址 显然我花了很多时间阅读 PE 架构来找出解决这个问题的最佳方法 根据 PECOFF v8 规范 我认为这是最新的官方规范
  • 如何使文件自我更新(Native C++)

    我将 Microsoft Visual Studio 2008 与 Windows 目标部署结合使用 我如何使文件 自我更新 我已经完成了 通过网络传输 部分 但是如何使可执行文件重写自身 基本上 我想为还包含自动更新程序的目录编写一个自动
  • Windows 上的 ruby​​ 中出现 SSL 错误

    我收到以下错误 C Users user Desktop folder gt ruby exchange rate rb C Ruby23 x64 lib ruby 2 3 0 net http rb 933 in connect nonb
  • 如何使用 Windows API 检索 HD 供应商/序列号

    我说的是physical磁盘驱动器 而不是卷 分区 逻辑驱动器 所以通常建议GetVolumeInformation函数不适用于我的情况 确切地说 我直接使用尚未分区的磁盘 我通过打开它的句柄CreateFile功能 hDisk Creat
  • 如何将 Windows 窗体应用程序 (C++) 设置为具有 Aero/Glass 背景?

    我正在使用 Visual Studio 2010 Pro 用 C 创建 Windows 窗体应用程序 我想创建一个透明背景 即使用 Aero Glass 效果 类似于它围绕 Windows 照片查看器中 UI 底部的方式 此时 我已经查看了
  • 关于 Windows、原始鼠标数据的代码要 #include 哪些内容

    我发现下面的帖子对于做我自己的项目非常有用 那么新手问题是 我必须包含什么才能使其发挥作用 Link 如何准确测量具有已知 DPI 的鼠标的鼠标移动 以英寸或厘米为单位 https stackoverflow com questions 1
  • 如何检查窗口在用户屏幕上是否完全可见?

    有没有办法检查 WinForm 在屏幕上完全可见 例如是否超出屏幕范围 我已经尝试使用 SystemInformation VirtualScreen 来实现此目的 只要虚拟屏幕是一个矩形 它就可以很好地工作 但是一旦它不是 例如 L 形的
  • 关于在 Windows 上使用 WiFi Direct Api?

    我目前正在开发一个应用程序 我需要在其中创建链接 阅读 无线网络连接 在桌面应用程序 在 Windows 10 上 和平板电脑 Android 但无关紧要 之间 工作流程 按钮 gt 如果需要提升权限 gt 创建类似托管网络的 WiFi 网
  • WHQL认证过程有多痛苦?

    您能估计一下 WHQL Windows 硬件徽标 认证过程有多痛苦吗 我看到有一个 43 页的分步指南 其中第一步是安装 Windows Server 2008 R2 x64 提交之前完成整个准备过程大约需要多长时间 然后需要多长时间才能得
  • git lineends redux - Mac OS git 与 Windows 用户的贡献

    我在 Mac OS X 上进行开发 我有一个用户正在贡献带有 CRLF 行结尾的代码 他目前不使用 git 我创建一个分支 然后将我的工作树切换到它 我将他的文件复制到工作树中 当我尝试暂存文件时 收到错误fatal CRLF would
  • QML 缩放不适用于非整数缩放因子

    我正在使用 QML 编写一个应用程序 当我按非整数因子缩放 GUI 时遇到麻烦 根据文档 https doc qt io qt 5 highdpi html Qt AA EnableHighDpiScaling应该启用与设备无关的像素 因此
  • 如何从不同的进程关闭窗口

    我有一个 C 应用程序 我想指示它从不同的进程中很好地关闭 我还希望能够要求它打开主窗口 我有一个对其主窗口句柄的引用 我知道我可以使用远程处理或 WCF 等复杂的方案来做到这一点 问题是我可以使用更简单的机制 例如窗口消息 或 c 应用程
  • .NET 4.0 是否与 Windows XP SP2 或更低版本兼容? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我读过这里http www microsoft com downloads en details aspx FamilyID 5765
  • 如何在使用 Apache 2 的 Windows 上忽略 Perl shebang?

    我已经在我的 Windows 机器上设置了本地 Perl Web 环境 我正在开发的应用程序最初来自 Linux 服务器 因此源代码的 shebang pl文件看起来像这样 usr bin perl 这会导致我的 Windows 开发机器上
  • 字符串比较在 PowerShell 函数中不起作用 - 我做错了什么?

    我正在尝试创建一个别名git commit它还将消息记录到单独的文本文件中 然而 如果git commit回报 nothing to commit working directory clean 它不应该将任何内容记录到单独的文件中 这是我

随机推荐