在Lua的Jumper寻路模块中添加不可走的endPos

时间:2018-04-09 01:13:12

标签: lua corona path-finding

我在Corona sdk中使用"Jumper"寻路模块。我遇到了一个问题,例如,我需要将一个单元移动到一个建筑物中。构建区块设置为不可步行,这意味着其可步行值= 1或不为0 因此,在这种情况下,探路者不会返回一个值,因为它不接受endPos是不可步行的。

以下是模块本身的getPath函数:

    --- Calculates a `path`. Returns the `path` from location __[startX, startY]__ to location __[endX, endY]__.
  -- Both locations must exist on the collision map. The starting location can be unwalkable.
  -- @class function
  -- @tparam int startX the x-coordinate for the starting location
  -- @tparam int startY the y-coordinate for the starting location
  -- @tparam int endX the x-coordinate for the goal location
  -- @tparam int endY the y-coordinate for the goal location
  -- @tparam int clearance the amount of clearance (i.e the pathing agent size) to consider
  -- @treturn path a path (array of nodes) when found, otherwise nil
    -- @usage local path = myFinder:getPath(1,1,5,5)

  function Pathfinder:getPath(startX, startY, endX, endY, clearance)
        self:reset()
    local startNode = self._grid:getNodeAt(startX, startY)
    local endNode = self._grid:getNodeAt(endX, endY)
    assert(startNode, ('Invalid location [%d, %d]'):format(startX, startY))
    assert(endNode and self._grid:isWalkableAt(endX, endY),
      ('Invalid or unreachable location [%d, %d]'):format(endX, endY))
    local _endNode = Finders[self._finder](self, startNode, endNode, clearance, toClear)
    if _endNode then
            return Utils.traceBackPath(self, _endNode, startNode)
    end
    return nil
  end

我试图删除断言部分,检查endPos是否可以步行,但它没有做任何事情。

我需要的是将endPos添加到路径中,即使它是非步行的

我不太擅长算法,所以如果有人知道如何实现这一点,我会很感激。

1 个答案:

答案 0 :(得分:0)

修改map您提供Grid功能而不是Pathfinder功能正文:

local board  = { 
    tiles = {}, 
    walkable = 0, 
    map = {
        {0,1,0,0,0},
        {0,1,0,1,0},
        {0,1,0,0,0},
        {0,0,0,0,0},
    } 
}

-- Make it walkable
board.map[1][2] = 0

local function createPath( startx, starty, endx, endy )

    -- Creates a grid object
    local grid = Grid( board.map ) 
    -- Creates a pathfinder object using Jump Point Search
    local myFinder = Pathfinder( grid, 'JPS', board.walkable ) 
    -- Calculates the path, and its length
    local path = myFinder:getPath( startx, starty, endx, endy )

    return path

end