【游戏跨场景寻路】基于egret(白鹭)的游戏地图跨场景寻路功能的实现

2024-08-27 03:58

本文主要是介绍【游戏跨场景寻路】基于egret(白鹭)的游戏地图跨场景寻路功能的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

每次时间久了算法就会淡忘,温故耗时,故做下整理,方便日后取材。

参考网址:

        原理性讲解:https://www.toutiao.com/a6540828594954830340/ 

        基于as3的代码:https://blog.csdn.net/sjt223857130/article/details/77199601

        堆优化理解:https://www.cnblogs.com/jason2003/p/7222182.html

        基于C++的代码:https://blog.csdn.net/qq_35644234/article/details/60870719

        相关注释:https://www.cnblogs.com/zzzPark/p/6060780.html

        代码参考:https://blog.csdn.net/u013052238/article/details/80273042

此文主要讲跨地图间的最短路径,AI 寻路参考 A* 算法:https://blog.csdn.net/u013052238/article/details/53375860,以及A*算法的优化:https://blog.csdn.net/u013052238/article/details/78126019

 

一、以下图作为多地图顶点:

二、地图数据字典配置简要设置如下:

{"A": {"B": {"len": 6},"C": {"len": 3}},"B": {"A": {"len": 6},"C": {"len": 2},"D": {"len": 5}},"C": {"A": {"len": 3},"B": {"len": 2},"D": {"len": 3},"E": {"len": 4}},"D": {"B": {"len": 5},"C": {"len": 3},"E": {"len": 2},"F": {"len": 3}},"E": {"C": {"len": 4},"D": {"len": 2},"F": {"len": 5}},"F": {"D": {"len": 3},"E": {"len": 5}}
}

三、为地图设置Vo类,这里简单设置MapVo类,带有mapId属性(如上的A~F顶点)


/**顶点地图数据 */
class MapVo {public mapID: string = "";
}

四、新建地图数据类存储对应矩阵的相关数据,这里设置MGraph(邻接矩阵)类:

/**邻接矩阵 */
class MGraph {/**邻接矩阵数组 */public edgeMatrixList: number[][];/**顶点数 */public pointNumber: number;/**存放顶点信息 */public mapDataList: MapVo[];public constructor() {this.edgeMatrixList = [];this.mapDataList = [];this.pointNumber = 0;}
}

五、新建类CrossMap初始化地图数据:

class CrossMap {/**地图配置表数据 */private gMapSource: any;/**INFINITY: 无穷大 */private INFINITY: number = 999999;/**邻接矩阵数据 */private gMGraph: MGraph;public constructor(mapSourcelist: any) {this.gMapSource = mapSourcelist;this.gMGraph = new MGraph();this.gMGraph.pointNumber = Object.keys(this.gMapSource).length;for (let point in this.gMapSource) {let _mapVo: MapVo = new MapVo();this.gMGraph.mapDataList.push(_mapVo);_mapVo.mapID = point;}//建立图的邻接矩阵for (let i: number = 0; i < this.gMGraph.pointNumber; i++) {if (!this.gMGraph.edgeMatrixList[i]) {this.gMGraph.edgeMatrixList[i] = [];}for (let j: number = 0; j < this.gMGraph.pointNumber; j++) {//计算i到j的权值let mapI: string = this.gMGraph.mapDataList[i].mapID;let mapJ: string = this.gMGraph.mapDataList[j].mapID;if (this.gMapSource[mapI]) {if (this.gMapSource[mapI][mapJ]) {				//判断地图I到地图J能不能走通this.gMGraph.edgeMatrixList[i][j] = this.gMapSource[mapI][mapJ].len;//权值设为配置// this.gMGraph.edgeMatrixList[i][j] = 1;	//默认给权值都为1continue;}}this.gMGraph.edgeMatrixList[i][j] = this.INFINITY;}}console.log("图的邻接矩阵为:", this.gMGraph.edgeMatrixList);/**导出路径数据 */this.exportPath();}/**保存搜索完后所有相关的路径字典 */private allPathDic: { [mapId: string]: string[] } = {};private exportPath(){......}
}

获得地图的邻接矩阵数据如下:

六、开始迪杰斯特拉算法查找各个点距离其他点的最短路径:

/**保存所有路径字典 */private allPathDic: { [mapId: string]: string[] } = {};private exportPath() {let time = egret.getTimer();let pointNum: number = this.gMGraph.pointNumber;for (let i: number = 0; i < pointNum; i++) {this.dijkstra(i, this.gMGraph);}console.log("跨地图数据生成耗时:" + (egret.getTimer() - time) + "ms");}private dijkstra(sourcePoint: number, _MGraph: MGraph) {let dist: number[] = [];					//从原点sourcePoint到其他的各定点当前的最短路径长度let path: number[] = [];					//path[i]表示从原点到定点i之间最短路径的前驱节点let selectList: number[] = [];  			//选定的顶点的集合let minDistance, point = 0;for (let i = 0; i < _MGraph.pointNumber; i++) {dist[i] = _MGraph.edgeMatrixList[sourcePoint][i];       		//距离初始化selectList[i] = 0;                        						//selectList[]置空  0 表示 i 不在selectList集合中if (_MGraph.edgeMatrixList[sourcePoint][i] < this.INFINITY) {   //路径初始化path[i] = sourcePoint;} else {path[i] = -1;}}selectList[sourcePoint] = 1;                  				//原点编号sourcePoint放入selectList中path[sourcePoint] = 0;for (let i = 0; i < _MGraph.pointNumber; i++) {             //循环直到所有顶点的最短路径都求出minDistance = this.INFINITY;                    		//minDistance置最小长度初值for (let j = 0; j < _MGraph.pointNumber; j++)        	//选取不在selectList中且具有最小距离的顶点pointif (selectList[j] == 0 && dist[j] < minDistance) {point = j;minDistance = dist[j];}selectList[point] = 1;                       		 	//顶点point加入selectList中for (let j = 0; j < _MGraph.pointNumber; j++)        	//修改不在selectList中的顶点的距离if (selectList[j] == 0)if (_MGraph.edgeMatrixList[point][j] < this.INFINITY && dist[point] + _MGraph.edgeMatrixList[point][j] < dist[j]) {dist[j] = dist[point] + _MGraph.edgeMatrixList[point][j];path[j] = point;}}this.putBothpath(_MGraph, dist, path, selectList, _MGraph.pointNumber, sourcePoint);//获取路径}private putBothpath(_MGraph: MGraph, dist: number[], path: number[], selectList: number[], pointNumber: number, sourcePoint: number) {for (let i = 0; i < pointNumber; i++) {if (selectList[i] == 1 && dist[i] < this.INFINITY) {/**路径点列表 */let pathVexsList: string[] = [];pathVexsList.push(_MGraph.mapDataList[sourcePoint].mapID);	//起点this.findPath(_MGraph, path, i, sourcePoint, pathVexsList);pathVexsList.push(_MGraph.mapDataList[i].mapID);			//终点/**测试 */let pathStr: string = "";for (let j: number = 0; j < pathVexsList.length; j++) {pathStr += pathVexsList[j];if (j != pathVexsList.length - 1) {						//不是结尾就加间隔符pathStr += "-";}}/**测试 */let _pathKey: string = _MGraph.mapDataList[sourcePoint].mapID + "-" + _MGraph.mapDataList[i].mapID;if (!this.allPathDic[_pathKey]) {							//不存在this.allPathDic[_pathKey] = pathVexsList;}console.log("从 " + _MGraph.mapDataList[sourcePoint].mapID + " 到 " + _MGraph.mapDataList[i].mapID + " 的最短路径长度为: " + dist[i] + "\t 路径为: " + pathStr);}else {console.log('从 ' + _MGraph.mapDataList[sourcePoint].mapID + ' 到 ' + _MGraph.mapDataList[i].mapID + ' 不存在路径      ');}}}private findPath(_MGraph: MGraph, path: number[], i: number, sourcePoint: number, pathVexsList: string[]) {  //前向递归查找路径上的顶点let point;point = path[i];if (point == sourcePoint) return;    								//找到了起点则返回this.findPath(_MGraph, path, point, sourcePoint, pathVexsList);    	//找顶点point的前一个顶点sourcePointpathVexsList.push(_MGraph.mapDataList[point].mapID);}

查找得到路径存如下:

从 A 到 A 不存在路径           
从 A 到 B 的最短路径长度为: 5	 路径为: A-C-B     
从 A 到 C 的最短路径长度为: 3	 路径为: A-C     
从 A 到 D 的最短路径长度为: 6	 路径为: A-C-D     
从 A 到 E 的最短路径长度为: 7	 路径为: A-C-E     
从 A 到 F 的最短路径长度为: 9	 路径为: A-C-D-F从 B 到 A 的最短路径长度为: 5	 路径为: B-C-A     
从 B 到 B 不存在路径           
从 B 到 C 的最短路径长度为: 2	 路径为: B-C     
从 B 到 D 的最短路径长度为: 5	 路径为: B-D     
从 B 到 E 的最短路径长度为: 6	 路径为: B-C-E     
从 B 到 F 的最短路径长度为: 8	 路径为: B-D-F 从 C 到 A 的最短路径长度为: 3	 路径为: C-A     
从 C 到 B 的最短路径长度为: 2	 路径为: C-B     
从 C 到 C 不存在路径           
从 C 到 D 的最短路径长度为: 3	 路径为: C-D     
从 C 到 E 的最短路径长度为: 4	 路径为: C-E     
从 C 到 F 的最短路径长度为: 6	 路径为: C-D-F 从 D 到 A 的最短路径长度为: 6	 路径为: D-C-A     
从 D 到 B 的最短路径长度为: 5	 路径为: D-B     
从 D 到 C 的最短路径长度为: 3	 路径为: D-C     
从 D 到 D 不存在路径           
从 D 到 E 的最短路径长度为: 2	 路径为: D-E     
从 D 到 F 的最短路径长度为: 3	 路径为: D-F  从 E 到 A 的最短路径长度为: 7	 路径为: E-C-A     
从 E 到 B 的最短路径长度为: 6	 路径为: E-C-B     
从 E 到 C 的最短路径长度为: 4	 路径为: E-C     
从 E 到 D 的最短路径长度为: 2	 路径为: E-D     
从 E 到 E 不存在路径           
从 E 到 F 的最短路径长度为: 5	 路径为: E-F  从 F 到 A 的最短路径长度为: 9	 路径为: F-D-C-A     
从 F 到 B 的最短路径长度为: 8	 路径为: F-D-B     
从 F 到 C 的最短路径长度为: 6	 路径为: F-D-C     
从 F 到 D 的最短路径长度为: 3	 路径为: F-D     
从 F 到 E 的最短路径长度为: 5	 路径为: F-E     
从 F 到 F 不存在路径

 

 

 

 

 

 

 

 

 

 

 

 

 

这篇关于【游戏跨场景寻路】基于egret(白鹭)的游戏地图跨场景寻路功能的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110566

相关文章

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Linux挂载linux/Windows共享目录实现方式

《Linux挂载linux/Windows共享目录实现方式》:本文主要介绍Linux挂载linux/Windows共享目录实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录文件共享协议linux环境作为服务端(NFS)在服务器端安装 NFS创建要共享的目录修改 NFS 配

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter