1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-19 15:14:50 +00:00
opus/sys/apis/jumper/core/node.lua

74 lines
2.6 KiB
Lua
Raw Normal View History

2016-12-11 19:24:52 +00:00
--- The Node class.
-- The `node` represents a cell (or a tile) on a collision map. Basically, for each single cell (tile)
-- in the collision map passed-in upon initialization, a `node` object will be generated
-- and then cached within the `grid`.
--
-- In the following implementation, nodes can be compared using the `<` operator. The comparison is
2017-10-26 22:56:55 +00:00
-- made with regards of their `f` cost. From a given node being examined, the `pathfinder` will expand the search
2016-12-11 19:24:52 +00:00
-- to the next neighbouring node having the lowest `f` cost. See `core.bheap` for more details.
2017-10-26 22:56:55 +00:00
--
2016-12-11 19:24:52 +00:00
if (...) then
--- The `Node` class.<br/>
-- This class is callable.
-- Therefore,_ <code>Node(...)</code> _acts as a shortcut to_ <code>Node:new(...)</code>.
-- @type Node
local Node = {}
Node.__index = Node
--- Inits a new `node`
-- @class function
-- @tparam int x the x-coordinate of the node on the collision map
-- @tparam int y the y-coordinate of the node on the collision map
-- @treturn node a new `node`
-- @usage local node = Node(3,4)
function Node:new(x,y,z)
2017-10-26 22:56:55 +00:00
return setmetatable({_x = x, _y = y, _z = z }, Node)
2016-12-11 19:24:52 +00:00
end
-- Enables the use of operator '<' to compare nodes.
-- Will be used to sort a collection of nodes in a binary heap on the basis of their F-cost
function Node.__lt(A,B) return (A._f < B._f) end
--- Returns x-coordinate of a `node`
-- @class function
-- @treturn number the x-coordinate of the `node`
2017-10-26 22:56:55 +00:00
-- @usage local x = node:getX()
2016-12-11 19:24:52 +00:00
function Node:getX() return self._x end
2017-10-26 22:56:55 +00:00
2016-12-11 19:24:52 +00:00
--- Returns y-coordinate of a `node`
-- @class function
2017-10-26 22:56:55 +00:00
-- @treturn number the y-coordinate of the `node`
-- @usage local y = node:getY()
2016-12-11 19:24:52 +00:00
function Node:getY() return self._y end
function Node:getZ() return self._z end
2017-10-26 22:56:55 +00:00
2016-12-11 19:24:52 +00:00
--- Returns x and y coordinates of a `node`
-- @class function
-- @treturn number the x-coordinate of the `node`
-- @treturn number the y-coordinate of the `node`
2017-10-26 22:56:55 +00:00
-- @usage local x, y = node:getPos()
2016-12-11 19:24:52 +00:00
function Node:getPos() return self._x, self._y, self._z end
2017-10-26 22:56:55 +00:00
2016-12-11 19:24:52 +00:00
--- Clears temporary cached attributes of a `node`.
-- Deletes the attributes cached within a given node after a pathfinding call.
-- This function is internally used by the search algorithms, so you should not use it explicitely.
-- @class function
-- @treturn node self (the calling `node` itself, can be chained)
-- @usage
-- local thisNode = Node(1,2)
-- thisNode:reset()
function Node:reset()
self._g, self._h, self._f = nil, nil, nil
self._opened, self._closed, self._parent = nil, nil, nil
return self
end
2017-10-26 22:56:55 +00:00
2016-12-11 19:24:52 +00:00
return setmetatable(Node,
2017-10-26 22:56:55 +00:00
{__call = function(_,...)
return Node:new(...)
2016-12-11 19:24:52 +00:00
end}
)
end