cocos2dx for lua A*寻路算法实现2
关于A*算法的实现过程,简单来说就是一个计算权限的过程。
首先,创建一个地图节点类,"MapNode.lua"
local MapNode = class("MapNode") function MapNode:ctor()
self._row = --行
self._col = --列
self._parent = nil--父节点
self._f = --当前节点的总开销
self._g = --当前节点的累计开销
self._h = --启发因子
end return MapNode
"AStar.lua"逻辑实现
local Direction = {}--方向
Direction.Right =
Direction.Right_Down =
Direction.Down =
Direction.Left_Down =
Direction.Left =
Direction.Left_Up =
Direction.Up =
Direction.Right_Up = local MapNode = require "app.views.MapNode" AStar = {}
AStar.__index = AStar function AStar.create()
local temp = {}
setmetatable(temp, AStar)
return temp
end --mapData:二维数组,存放底图数据,0是可行走格子,1是有障碍格子
function AStar:init(mapData)
self._mapData = mapData--地图数据
self._map = {}
self._lPath = {}--收集的路径
self._bFind = false local mapRow = #mapData
local mapCol = #mapData[] for i = , mapRow - do
self._map[i] = {}
for j = , mapCol - do
local mapNode = MapNode.new()
mapNode._row = i
mapNode._col = j
self._map[i][j] = mapNode
end
end
end --开始寻路
--from:开始格子位置(非坐标)
--to:目标格子位置
function AStar:getSearchPath(from, to)
self:processAStar(from, to)--收集路径到_lPath
return self._lPath
end function AStar:canPass(row,col)--判断是否能够通过
if self._mapData[col][row] == then
return true
end
return false
end
function AStar:getAdjacent(currentPos,dir)--根据方向获取相邻格子的位置
if dir == Direction.Right then
return cc.p(currentPos.x + ,currentPos.y)
elseif dir == Direction.Right_Down then
return cc.p(currentPos.x + ,currentPos.y - )
elseif dir == Direction.Down then
return cc.p(currentPos.x,currentPos.y - )
elseif dir == Direction.Left_Down then
return cc.p(currentPos.x - ,currentPos.y - )
elseif dir == Direction.Left then
return cc.p(currentPos.x - ,currentPos.y)
elseif dir == Direction.Left_Up then
return cc.p(currentPos.x - ,currentPos.y + )
elseif dir == Direction.Up then
return cc.p(currentPos.x,currentPos.y + )
elseif dir == Direction.Right_Up then
return cc.p(currentPos.x + ,currentPos.y + )
end
end
function AStar:AStarCore(fromPos,toPos)--算法核心代码
local open = {}
local close = {}
local targetNode = self._map[toPos.y][toPos.x]
local fromNode = self._map[fromPos.y][fromPos.x]
local f,g,h;
fromNode._g =
fromNode._h = math.abs(fromPos.x - toPos.x) + math.abs(fromPos.y - toPos.y)
fromNode._f = fromNode._h
open[#open + ] = fromNode
while #open > do
local mapNode = open[]
table.remove(open,)
if mapNode._row == toPos.x and mapNode._col == toPos.y then
return true
end
close[#close + ] = mapNode
local parentPos = cc.p(mapNode._col,mapNode._row)
local adjacentPos = nil
local adjacentNode = nil
for i = , do
adjacentPos = self:getAdjacent(parentPos,i)
if adjacentPos.x >= and adjacentPos.x < mapCol and adjacentPos.y >= and adjacentPos.y < mapRow then
adjacentNode = self._map[adjacentPos.y][adjacentPos.x]
if self:canPass(adjacentPos.y,adjacentPos.x) == true and self:isContain(close,adjacentNode) == false and
self:isContain(open,adjacentNode) == false then
if i % == then
g = adjacentNode._g +
else
g = adjacentNode._g + 1.4
end
h = math.abs(adjacentPos.x -toPos.x) + math.abs(adjacentPos.y - toPos.y)
f = g + h
adjacentNode._parent = mapNode
adjacentNode._f = f
adjacentNode._g = g
adjacentNode._h = h
open[#open + ] = adjacentNode
end
end
end
table.sort(open,function(a,b)
return a._f < b._f
end)
end
return false
end
function AStar:processAStar(from,to)--执行A*算法
local f = self:AStarCore(from,to)
if f == true then
self:collectRoute(from,to)
return true
end
return false
end
function AStar:collectRoute(from, to)--收集路线
self._lPath = {}
local mapNode = self._map[to.y][to.x]
self._lPath[#self._lPath + ] = mapNode
while mapNode._col ~= from.x or mapNode._row ~= from.y do
mapNode = mapNode._parent
self._lPath[#self._lPath + ] = mapNode
end
end
function AStar:isContain(tbl,cell) --在table中是否包含某个单元
for k,v in pairs(tbl) do
if tbl[k] == cell then
return true
end
end
return false
end
测试一下:
TestScene.lua
require "AStar.lua"
local TestScene = class("TestScene",cc.load("mvc").ViewBase) function TestScene:ctor()
local mapData = {
{, , , , , , , }
{, , , , , , , }
{, , , , , , , }
{, , , , , , , }
{, , , , , , , }
{, , , , , , , }
{, , , , , , , }
{, , , , , , , }
}--地图数据
self.mapData = mapData
for i = , #mapData do--创建地图快图片
for j = , #mapData[] do
local tileSp = cc.Sprite:create("tilemap.png")
self:addChild(tileSp)
tileSp:setPosition(i * tileWidth-(tileWidth * 0.5), j * tileHeight - (tileWidth * 0.5))
end
end
local from = cc.p(,)--起点
local to = cc.p(,)--目标点
local sp1 = display.newSprite("qizi.png",to.x *tileWidth+(tileWidth*0.5),to.y *tileHeight+(tileWidth*0.5))--目标点图片
self:addChild(sp1)
local sp2 = display.newSprite("qizi.png",from.x *tileWidth+(tileWidth*0.5),from.y *tileHeight+(tileWidth*0.5))--起点图片
self:addChild(sp2)
self:setCollisionSp()
self._sprite = sp.SkeletonAnimation:create("spine/116_m_fks/116_m_fks.json","spine/116_m_fks/116_m_fks.atlas",0.08)
self._sprite:setAnimation(,"stanby_1",true)--创建角色
self._sprite:setScale(-,)
self._sprite:setPosition(from.x *tileWidth+(tileWidth*0.5),from.y *tileHeight)
self:addChild(self._sprite)
local astar_logic = AStar.create()
astar_logic:init(mapData)
self._lPath = astar_logic:getSearchPath(from, to)
self:drawPathNode()
self._posIndex = #self._lPath
self:moveSprite()
end function TestScene:setCollisionSp()--设置障碍物
for k, v in pairs(self.mapData) do
for m, n in pairs(v) do
if n == then--障碍物
local sp = display.newSprite("collision.png",i*tileWidth+(tileWidth*0.5),j*tileHeight+(tileWidth*0.5))
self:addChild(sp)
end
end
end
end function TestScene:moveSprite()--移动精灵
local x = self._lPath[self._posIndex]._col *tileWidth+(tileWidth*0.5)
local y = self._lPath[self._posIndex]._row *tileWidth+(tileWidth*0.5)
transition.moveTo(self._sprite,{x = x,y = y,time = ,onComplete = function()
self._posIndex = self._posIndex -
if self._posIndex == then
return
end
self:moveSprite()
end})
end function TestScene:drawPathNode()--画路径图片
for k,v in pairs(self._lPath) do
local sp3 = display.newSprite("redPoint.png",v._col *tileWidth+(tileWidth*0.5),v._row *tileWidth+(tileWidth*0.5))
self:addChild(sp3)
end
end
转载请注明出处,from 博客园HemJohn
cocos2dx for lua A*寻路算法实现2的更多相关文章
- cocos2d-x学习日志(13) --A星寻路算法demo
你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢?如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! A星算法简介: A*搜寻算法俗称A星 ...
- A*寻路算法lua实现
前言:并在相当长的时间没有写blog该,我觉得有点"颓废"该,最近认识到各种同行,也刚刚大学毕业,我认为他们是优秀的.认识到与自己的间隙,有点自愧不如.我没有写blog当然,部分原 ...
- 《C++游戏开发》十六 游戏中的寻路算法(二):迷宫&A*算法基础
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/10289253 作者:七十一雾央 新浪微博:http: ...
- A*寻路算法详细解读
文章目录 A*算法描述 简化搜索区域 概述算法步骤 进一步解释 具体寻路过程 模拟需要更新F值的情况 Lua代码实现 在学习A*算法之前,很好奇的是A*为什么叫做A*.在知乎上找到一个回答,大致意思是 ...
- A*寻路算法的个人理解
A*寻路算法是一个求两点之间的最短路径的方法 算法详情如下: 准备工作: 两个容器: open容器和close容器 价值估算公式: F = G + H G:从起点移动到指定方格的移动代价: ...
- A星寻路算法介绍
你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢? 如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! 在网上已经有很多篇关于A星寻路算法 ...
- A*寻路算法探究
A*寻路算法探究 A*算法常用在游戏的寻路,是一种静态网路中求解最短路径的搜索方法,也是解决很多搜索问题的算法.相对于Dijkstra,BFS这些算法在复杂的搜索更有效率.本文在U3D中进行代码的测试 ...
- A*寻路算法
对于初学者而言,A*寻路已经是个比较复杂的算法了,为了便于理解,本文降低了A*算法的难度,规定只能横竖(四方向)寻路,而无法直接走对角线,使得整个算法更好理解. 简而言之,A*寻路就是计算从起点经过该 ...
- 算法:Astar寻路算法改进,双向A*寻路算法
早前写了一篇关于A*算法的文章:<算法:Astar寻路算法改进> 最近在写个js的UI框架,顺便实现了一个js版本的A*算法,与之前不同的是,该A*算法是个双向A*. 双向A*有什么好处呢 ...
随机推荐
- Java 为程序创建日志系统
使用JAVA创建日志系统有两种方法 1.使用log4j操作日志文件 2.使用系统重定向输出日志信息 方法1:使用log4j操作日志文件(可使用jar或者xml) 步骤1:下载log4j.jar 下载地 ...
- Luogu P1438无聊的序列【线段树/差分】By cellur925
题目传送门 题目大意:维护一个序列,维护区间加等差数列,单点查询的操作. 首先我们肯定是要用线段树来维护了,按照一般的思维局限,我选择了维护序列中的值,但是区间修改的时候由于公差的存在,所以区间修改有 ...
- Git 深度学习填坑之旅二(文件三种状态、打标签)
0x01 三种状态 Git 有三种状态,你的文件可能处于其中之一: 已提交(committed).已修改(modified)和已暂存(staged). 已提交表示数据已经安全的保存在本地数据库中. 已 ...
- jquery jtemplates.js模板渲染引擎的详细用法第三篇
jquery jtemplates.js模板渲染引擎的详细用法第三篇 <span style="font-family:Microsoft YaHei;font-size:14px;& ...
- CF 700E
构建后缀自动机,求出后缀树 比较明显的dp 设 \(f[i]\) 表示从上而下到达当前点能够满足条件的最优值 只需要检查父亲节点是否在当前串中出现过两次就行了 这个判断用 \(endpos\) 来判断 ...
- NET?.NET Framework?.NET Core?
什么是.NET?什么是.NET Framework?什么是.NET Core? https://www.cnblogs.com/1996V/p/9037603.html 什么是.NET?什么是.NET ...
- sql like 多条件
select * from student where name like 'mike%' or name like 'rose%';
- 《深入理解java虚拟机》笔记(4)对象已死吗
一.垃圾回收器回收的对象 虚拟机内存区域中程序计数器.虚拟机栈.本地方法栈随线程而生,随线程而灭.这3个区域内存分配和回收都具备确定性.因此不需要过多考虑回收问题. 而Java堆和方法区不一样,这部分 ...
- java 多线程死锁
死锁案例: package com.test; public class DealThread implements Runnable { public String username; public ...
- Error occurred while loading plugins. CLI functionality may be limited.
npm install --save-dev --save-exact @ionic/cli-plugin-ionic-angular@latest @ionic/cli-plugin-cordova ...