• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

cocos 寻路 lua实现

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

cocos 寻路 lua实现

local RoadPoint = {}

function RoadPoint:CreateNode(p)
-- body
local t = {}
t.g = -1;
t.h = -1;
t.f = 0;
t.point = p;
t.parent = nil;
return t;
end

-- 判断p是否在open表中

function RoadPoint:exists(p)
-- body
for i =1,#self.open do
if self.open[i].point.x == p.x and self.open[i].point.y == p.y  then
return true;
end
end
return false;

end

function RoadPoint:FindPath(startp,endp,maskparent)
-- body

release_print(string.format("开始点:x--%d,y--%d",startp.x,startp.y));
release_print(string.format("结束点:x--%d,y--%d",endp.x,endp.y));

self.step = 40;

self.open = {};
self.close = {};
self.childs = maskparent:getChildren();
self.childCount = maskparent:getChildrenCount();
local point = self:CreateNode(startp);
table.insert(self.open,point);

while(#self.open ~= 0)
do

table.sort(self.open,function (a,b)
-- body
return a.f < b.f;
end)

local tempStart = self.open[1];
table.remove(self.open,1);

table.insert(self.close,tempStart);

local sur = self:GetSurPoints(tempStart.point);

for i=1,#sur do

if self:exists(sur[i].point) then

local pg = 0;
if sur[i].parent ~= nil then
pg = sur[i].parent.g;
end
local g = sur[i].g + pg;
if g < sur[i].g then
sur[i].g = g;
sur[i].f = sur[i].g + sur[i].h;
sur[i].parent = tempStart;
end
else

local pg = 0;
if sur[i].parent ~= nil then
pg = sur[i].parent.g;
end
local g = sur[i].g + pg;
sur[i].g = g;
sur[i].h = math.abs(endp.x - sur[i].point.x) + math.abs(endp.y,- sur[i].point.y);
sur[i].f = sur[i].g + sur[i].h;
sur[i].parent = tempStart;
table.insert(self.open,sur[i]);
end

end

for i = 1, #self.close do

local tempP = self.close[i].point;
local x = math.abs(tempP.x - endp.x);
local y = math.abs(tempP.y - endp.y);

if math.abs(tempP.x - endp.x) < self.step and math.abs(tempP.y - endp.y) < self.step then
return self.close[i];
end

end

end

return nil;

end

function RoadPoint:GetSurPoints( p )

local t = {};

local up = cc.p(p.x,p.y -  self.step);

if  self:judge(up) == true then

local point = self:CreateNode(up);

point.g = 10;

table.insert(t,point)

end

local down = cc.p(p.x,p.y+ self.step);
if self:judge(down) == true then
local point = self:CreateNode(down);
point.g = 10;
table.insert(t,point)
end

local left = cc.p(p.x -  self.step,p.y);
if self:judge(left) == true then
local point = self:CreateNode(left);
point.g = 10;
table.insert(t,point)
end

local right = cc.p(p.x +  self.step, p.y);
if self:judge(right) == true then
local point = self:CreateNode(right);
point.g = 10;
table.insert(t, point)
end

local leftup = cc.p(p.x -  self.step,p.y -  self.step);
if self:judge(leftup) == true then
local point = self:CreateNode(leftup);
point.g = 14;
table.insert(t,point);
end

local leftdown = cc.p(p.x -  self.step,p.y +  self.step);
if self:judge(leftdown) ==true then
local point = self:CreateNode(leftdown);
point.g = 14;
table.insert(t,point);
end

local rightup = cc.p(p.x +  self.step,p.y -  self.step);
if self:judge(rightup) == true then
local point = self:CreateNode(rightup);
point.g = 14;
table.insert(t,point);
end

local rightdown = cc.p(p.x +  self.step,p.y +  self.step);
if self:judge(rightdown) == true then
local point = self:CreateNode(rightdown);
point.g = 14;
table.insert(t,point);
end

return t;
-- body
end

function RoadPoint:judge(p)
-- body
for i = 1,#self.close do
if self.close[i].point.x == p.x and self.close[i].point.y == p.y then
return false;
end
end
for i=1,self.childCount do
if cc.rectContainsPoint(self.childs[i]:boundingBox(),p) then
return true;
end
end
return false;
end

return RoadPoint;

原文地址http://www.bieryun.com/2864.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Cocos如何绑定Lua自定义类发布时间:2022-07-22
下一篇:
iOS中Lua脚本应用笔记一:脚本概念相关发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap