当前位置: 首页 > news >正文

A* (AStar) 寻路

//调用工具类获取路线

        let route = AStarSearch.getRoute(start_point, end_point, this.mapFloor.map_point);

map_point 是所有可走点的集合

import { _decorator, Component, Node, Prefab, instantiate, v3, Vec2 } from 'cc';
import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { GameWorld } from "../../../GameWorld";
import { MapFloor, RowColData } from "../../../../Scripts/MapEditor/MapFloor";export class Point {x: number;y: number;constructor(x: number, y: number) {this.x = x;this.y = y;}G: number = 0;   //G = 从起点A,沿着产生的路径,移动到网格上指定方格的移动耗费。H: number = 0;   //H = 从网格上那个方格移动到终点B的预估移动耗费F: number = 0;   //F = G + Hfather: Point = null;   //这个点的上一个点,通过回溯可以找到起点 is_close: boolean = false;   //是否关闭搜索
}export class AStarSearch {static start: Point = null;      //起点static end: Point = null;        //终点static map: Map<string, Point> = null;   //地图pointstatic openSet: Set<Point> = new Set();  //开放队列static pppp: Point = null;       //执行完寻路,它就有值了,除非没找到/*** 获取路线 (此寻路不走斜线)*/static getRoute(start: Point, end: Point, map_point: Map<string, { row: number, col: number }>): Point[] {//清空上次寻路,并赋值this.is_find = false;this.openSet.clear();this.pppp = null;this.start = { ...start };this.end = { ...end };this.map = new Map<string, Point>();map_point.forEach((value, key) => {let point = new Point(value.row, value.col);this.map.set(key, point);});let route = new Array<Point>();let keyStr = this.start.x + "_" + this.start.y;if (!this.map.has(keyStr)) {return route;}this.map.get(keyStr).G = 0;       //起点的G是0//开始寻路try {this.search(this.start);     //内存不够会报错,一般是起点或终点封闭} catch (error) {console.warn("位置不对", error);return route;}if (this.pppp) {this.getFather(this.pppp, route);}return route;}/*** 寻路*/static is_find = false;    //是否已经找到路线static search(point: Point) {if (point.x == this.end.x && point.y == this.end.y) {this.is_find = true;this.pppp = point;return;}let arr = this.getAround(point);arr.forEach(p => {this.setFather(p, point);});//arr按照F排序 从小到大this.openSet = new Set([...this.openSet].sort(this.compare));//递归继续找this.openSet.forEach((pp, index, arr) => {if (pp.is_close) {        //删除没用的this.openSet.delete(pp);}if (!this.is_find) {this.search(pp);}});}/*** 获取周围4个点,上下左右*/static getAround(point: Point) {point.is_close = true;let arr = new Array<Point>();let index: string;let p: Point;//上、下、左、右let aroundPos = [{ x: point.x, y: point.y - 1 },{ x: point.x, y: point.y + 1 },{ x: point.x - 1, y: point.y },{ x: point.x + 1, y: point.y }]aroundPos.forEach(point => {index = point.x + "_" + point.y;p = this.map.get(index);if (p && !p.is_close) {arr.push(p);this.openSet.add(p);}})return arr;}/*** point换父亲,并重新计算G、H、F*/static setFather(son: Point, father: Point) {if (!son.father || son.father.G > father.G) {son.father = father;son.G = son.father.G + 1;son.H = Math.abs(son.x - this.end.x) + Math.abs(son.y - this.end.y);son.F = son.G + son.H;}}/*** 比较器*/static compare(p1: Point, p2: Point) {if (p1.F > p2.F) {return 1;} else {return -1;}}/*** 递归 把祖宗放进route里面*/static getFather(point: Point, route: Array<Point>) {let father = point.father;if (father) {this.getFather(father, route);}route.push(point);}
}

http://www.xdnf.cn/news/358183.html

相关文章:

  • 读取传感器发来的1Byte数据:分低位先行和高位先行的处理方法
  • 【iptables】--命令基本使用
  • Web 架构之数据读写分离
  • 配置Java Selenium Web自动化测试环境
  • 5.0.5 变换(旋转、缩放、扭曲)
  • 云手机解决方案
  • 图像匹配导航定位技术 第 11 章
  • 蓝桥杯青少 图形化编程(Scratch)编程题每日一练——小猫的城堡
  • 电动汽车充换电设施可调能力聚合评估与预测 - 使用说明文档
  • Java设计模式全面详解:从基础到高级的23种模式简介
  • Vue 系列之:defineProps、defineEmits、...
  • vue3: pdf.js 2.16.105 using typescript
  • 字符函数和字符串函数
  • MKS RGA 校准调试MKS eVision和Vision 1000p RGA步骤(图文并茂)
  • 使用 Spring 和 Redis 创建处理敏感数据的服务
  • 4.2【LLaMA-Factory实战】金融财报分析系统:从数据到部署的全流程实践
  • 20250509 哲学上的真空和哲学上的虚无是一个概念吗
  • 量子计算在软件开发中的兴起
  • Baklib智能内容推荐中台是什么?
  • canvas坐标系转webgl坐标系
  • 数字化转型-4A架构之数据架构
  • selenium替代----playwright
  • XML Forms Data Format (XFDF) 工作原理、数据结构、使用场景以及与缓冲区的交互方式
  • 【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
  • 从 JMS 到 ActiveMQ:API 设计与扩展机制分析(一)
  • 37-智慧医疗服务平台(在线接诊/问诊)
  • Windows系统下【Celery任务队列】python使用celery 详解(二)
  • Jsoup与HtmlUnit:两大Java爬虫工具对比解析
  • PostgreSQL逻辑复制(logic replication)
  • 《内存单位:解锁数字世界的“度量衡”》