basePos[nextType])

for i = 2, #path1 do
table.insert(path2, 1, path1[i])
end

return path2
end

function AStar:margePath(paths)
local path = {}
for i,v in ipairs(paths) do
for j,k in ipairs(v) do
if not (i ~= 1 and j == 1) then
table.insert(path, k)
end
end
end
return path
end

function AStar:findAround(type, pos, index, dir)
local x = pos.x
local y = pos.y
if dir == 1 then
x = x - step
elseif dir == 2 then
y = y + step
elseif dir == 3 then
x = x + step
elseif dir == 4 then
y = y - step

elseif dir == 5 then
x = x - step
y = y + step
elseif dir == 6 then
x = x - step
y = y - step
elseif dir == 7 then
x = x + step
y = y + step
elseif dir == 8 then
x = x + step
y = y - step
end

if x < 1 or x > max or y < 1 or y > max then
return
end

local nowPos = p(x, y)
local bool, idx = self:isPosInTb(self.open[type], nowPos)
if bool then
local real = self:getReal(nowPos, index)
if real < self.dots[idx].real then
self.dots[idx].real = real
self.dots[idx].pDot = index
end
return
end

bool, idx = self:isPosInTb(self.close[type], nowPos)
if bool then
local real = self:getReal(nowPos, index)
if real < self.dots[idx].real then
self.dots[idx].pDot = index
self.dots[idx].real = real
self:addPos(type, idx)
end
return
end

if self.unDots[x] and self.unDots[x][y] then
return
end

if self.checkFunc then
if self.checkFunc(nowPos, dir) then
return nowPos
else
self:addUnDot(nowPos)
end
elseif self.testCheck then
if self:testCheck(nowPos) then
return nowPos
else
self:addUnDot(nowPos)
end
end
end

-----------------cocos test
function AStar:initPos()
self.config = {}
self.unDots = {}
for i = 1, max do
for j = 1, max do
if math.random(1, 4) == 1 then
if not self.config[i] then
self.config[i] = {}
end
self.config[i][j] = true
end
end
end
return {p( math.floor(math.random(1, max / step) * step) , math.floor(math.random(1, max / step) * step)), p(math.floor(math.random(1, max / step) * step), math.floor(math.random(1, max / step) * step))}
end

function AStar:testCheck(pos)
if self.config and self.config[pos.x] and self.config[pos.x][pos.y] then
return false
end
return true
end

function AStar:drawPath(parent, path)
if isEmptyTable(path) then
return
end

local function isInPath(tb, x, y)
for i,v in ipairs(tb) do
if v.x == x and v.y == y then
return true
end
end
end

local function isInTb(tb, x, y)
for i,v in pairs(tb) do
for j,k in pairs(v) do
if self.dots[j].pos.x == x and self.dots[j].pos.y == y then
return true
end
end
end
end

local node = parent:getChildByTag(666)
if not node then
node = cc.Node:create()
node:setPosition(100, 100)
parent:addChild(node, 1, 666)
end
local width = 20

for x = 1, max do
for y = 1, max do
local drawNode = node:getChildByTag(x * max + y)
if not drawNode then
drawNode = cc.DrawNode:create()
drawNode:setPosition(x * width, y * width)
node:addChild(drawNode, 1, x * max + y)
end
drawNode:clear()
if isInPath(self.basePos, x, y) then
drawNode:drawSolidRect(cc.p(0, 0), cc.p(width, width), cc.c4f(0, 1, 0, 1))
elseif isInPath(path, x, y) then
drawNode:drawSolidRect(cc.p(0, 0), cc.p(width, width), cc.c4f(1, 1, 0, 1))
elseif not self:testCheck(p(x, y)) then
drawNode:drawSolidRect(cc.p(0, 0), cc.p(width, width), cc.c4f(0.2, 0.2, 0.2, 1))
elseif isInTb(self.open, x, y) then
drawNode:drawSolidRect(cc.p(0, 0), cc.p(width, width), cc.c4f(0.5, 0.7, 0.5, 1))
elseif isInTb(self.close, x, y) then
drawNode:drawSolidRect(cc.p(0, 0), cc.p(width, width), cc.c4f(0.5, 0.5, 0.7, 1))
else
drawNode:drawSolidRect(cc.p(0, 0), cc.p(width, width), cc.c4f(1, 1, 1, 1))
end
end
end
end


Prev | Next
Pg.: 1 2 3


Back to home | File page

Subscribe | Register | Login | N