refactor + cleanup
This commit is contained in:
@@ -25,18 +25,9 @@ if (...) then
|
||||
{x = 0, y = 0, z = 1} --[[U]], {x = 0, y = -0, z = -1}, --[[D]]
|
||||
}
|
||||
|
||||
--- The `Grid` class.<br/>
|
||||
-- This class is callable.
|
||||
-- Therefore,_ <code>Grid(...)</code> _acts as a shortcut to_ <code>Grid:new(...)</code>.
|
||||
-- @type Grid
|
||||
local Grid = {}
|
||||
Grid.__index = Grid
|
||||
|
||||
--- Inits a new `grid`
|
||||
-- @class function
|
||||
-- @tparam table Map dimensions
|
||||
-- or a `string` with line-break chars (<code>\n</code> or <code>\r</code>) as row delimiters.
|
||||
-- @treturn grid a new `grid` instance
|
||||
function Grid:new(dim)
|
||||
local newGrid = { }
|
||||
newGrid._min_x, newGrid._max_x = dim.x, dim.ex
|
||||
@@ -49,73 +40,38 @@ if (...) then
|
||||
return setmetatable(newGrid,Grid)
|
||||
end
|
||||
|
||||
--- Checks if `node` at [x,y] is __walkable__.
|
||||
-- Will check if `node` at location [x,y] both *exists* on the collision map and *is walkable*
|
||||
-- @class function
|
||||
-- @tparam int x the x-location of the node
|
||||
-- @tparam int y the y-location of the node
|
||||
-- @tparam int z the z-location of the node
|
||||
--
|
||||
function Grid:isWalkableAt(x, y, z)
|
||||
local node = self:getNodeAt(x,y,z)
|
||||
return node and node.walkable ~= 1
|
||||
end
|
||||
|
||||
--- Returns the `grid` width.
|
||||
-- @class function
|
||||
-- @treturn int the `grid` width
|
||||
-- @usage print(myGrid:getWidth())
|
||||
function Grid:getWidth()
|
||||
return self._width
|
||||
end
|
||||
|
||||
--- Returns the `grid` height.
|
||||
-- @class function
|
||||
-- @treturn int the `grid` height
|
||||
-- @usage print(myGrid:getHeight())
|
||||
function Grid:getHeight()
|
||||
return self._height
|
||||
end
|
||||
|
||||
--- Returns the set of nodes.
|
||||
-- @class function
|
||||
-- @treturn {{node,...},...} an array of nodes
|
||||
-- @usage local nodes = myGrid:getNodes()
|
||||
function Grid:getNodes()
|
||||
return self._nodes
|
||||
end
|
||||
|
||||
--- Returns the `grid` bounds. Returned values corresponds to the upper-left
|
||||
-- and lower-right coordinates (in tile units) of the actual `grid` instance.
|
||||
-- @class function
|
||||
-- @treturn int the upper-left corner x-coordinate
|
||||
-- @treturn int the upper-left corner y-coordinate
|
||||
-- @treturn int the lower-right corner x-coordinate
|
||||
-- @treturn int the lower-right corner y-coordinate
|
||||
-- @usage local left_x, left_y, right_x, right_y = myGrid:getBounds()
|
||||
function Grid:getBounds()
|
||||
return self._min_x, self._min_y, self._min_z, self._max_x, self._max_y, self._max_z
|
||||
end
|
||||
|
||||
--- Returns neighbours. The returned value is an array of __walkable__ nodes neighbouring a given `node`.
|
||||
-- @class function
|
||||
-- @tparam node node a given `node`
|
||||
-- @tparam[opt] string|int|func walkable the value for walkable locations
|
||||
-- in the collision map array (see @{Grid:new}).
|
||||
-- Defaults to __false__ when omitted.
|
||||
-- @treturn {node,...} an array of nodes neighbouring a given node
|
||||
-- @usage
|
||||
-- local aNode = myGrid:getNodeAt(5,6)
|
||||
-- local neighbours = myGrid:getNeighbours(aNode, 0, true)
|
||||
function Grid:getNeighbours(node)
|
||||
local neighbours = {}
|
||||
for i = 1,#straightOffsets do
|
||||
local n = self:getNodeAt(
|
||||
node._x + straightOffsets[i].x,
|
||||
node._y + straightOffsets[i].y,
|
||||
node._z + straightOffsets[i].z
|
||||
node.x + straightOffsets[i].x,
|
||||
node.y + straightOffsets[i].y,
|
||||
node.z + straightOffsets[i].z
|
||||
)
|
||||
if n and self:isWalkableAt(n._x, n._y, n._z) then
|
||||
if n and self:isWalkableAt(n.x, n.y, n.z) then
|
||||
neighbours[#neighbours+1] = n
|
||||
end
|
||||
end
|
||||
@@ -123,17 +79,7 @@ if (...) then
|
||||
return neighbours
|
||||
end
|
||||
|
||||
--- Returns the `node` at location [x,y,z].
|
||||
-- @class function
|
||||
-- @name Grid:getNodeAt
|
||||
-- @tparam int x the x-coordinate coordinate
|
||||
-- @tparam int y the y-coordinate coordinate
|
||||
-- @tparam int z the z-coordinate coordinate
|
||||
-- @treturn node a `node`
|
||||
-- @usage local aNode = myGrid:getNodeAt(2,2)
|
||||
|
||||
-- Gets the node at location <x,y> on a preprocessed grid
|
||||
function Grid:getNodeAt(x,y,z)
|
||||
function Grid:getNodeAt(x,y,z)
|
||||
if not x or not y or not z then return end
|
||||
if Utils.outOfRange(x,self._min_x,self._max_x) then return end
|
||||
if Utils.outOfRange(y,self._min_y,self._max_y) then return end
|
||||
|
||||
Reference in New Issue
Block a user