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

41 lines
1.5 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
2018-01-24 22:39:38 +00:00
local Node = {}
Node.__index = Node
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
function Node:new(x,y,z)
return setmetatable({x = x, y = y, z = z }, Node)
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
-- 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
2016-12-11 19:24:52 +00:00
2017-10-28 00:24:48 +00:00
function Node:getX() return self.x end
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
--- 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.
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
2018-01-24 22:39:38 +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