Added some built-in composite nodes
This commit is contained in:
parent
fa96a38a1d
commit
df9acb5841
70
init.lua
70
init.lua
@ -1,5 +1,5 @@
|
|||||||
local Tactree = {}
|
local Tactree = {}
|
||||||
Tactree.Nodes = {}
|
local Nodes = {}
|
||||||
|
|
||||||
local function NodeStart(node, args)
|
local function NodeStart(node, args)
|
||||||
-- abort if started
|
-- abort if started
|
||||||
@ -32,13 +32,13 @@ end
|
|||||||
local function default_impl(node) end
|
local function default_impl(node) end
|
||||||
function Tactree.Leaf(name)
|
function Tactree.Leaf(name)
|
||||||
return function(t)
|
return function(t)
|
||||||
if Tactree.Nodes[name] then
|
if Nodes[name] then
|
||||||
error('node with name \'' .. name .. '\' already exists.', 2)
|
error('node with name \'' .. name .. '\' already exists.', 2)
|
||||||
else
|
else
|
||||||
Tactree.Nodes[name] = {}
|
Nodes[name] = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
local node_type = Tactree.Nodes[name]
|
local node_type = Nodes[name]
|
||||||
|
|
||||||
local tick = t.tick or t[1]
|
local tick = t.tick or t[1]
|
||||||
if type(tick) ~= 'function' then error('no tick function supplied', 2) end
|
if type(tick) ~= 'function' then error('no tick function supplied', 2) end
|
||||||
@ -52,14 +52,14 @@ end
|
|||||||
function Tactree.Composite(name)
|
function Tactree.Composite(name)
|
||||||
return function(parent_type_name)
|
return function(parent_type_name)
|
||||||
return function(children)
|
return function(children)
|
||||||
if Tactree.Nodes[name] then
|
if Nodes[name] then
|
||||||
error('node with name \'' .. name .. '\' already exists.', 2)
|
error('node with name \'' .. name .. '\' already exists.', 2)
|
||||||
else
|
else
|
||||||
if not Tactree.Nodes[parent_type_name] then error('no such node \'' .. name .. '\'', 2) end
|
if not Nodes[parent_type_name] then error('no such node \'' .. name .. '\'', 2) end
|
||||||
Tactree.Nodes[name] = setmetatable({}, Tactree.Nodes[parent_type_name])
|
Nodes[name] = setmetatable({}, { __index = Nodes[parent_type_name] })
|
||||||
end
|
end
|
||||||
|
|
||||||
local node_type = Tactree.Nodes[name]
|
local node_type = Nodes[name]
|
||||||
|
|
||||||
node_type.children = {}
|
node_type.children = {}
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ function Tactree.Composite(name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Tactree.Tree(name)
|
function Tactree.Tree(name)
|
||||||
if not Tactree.Nodes[name] then error('no such node \'' .. name .. '\'', 2) end
|
if not Nodes[name] then error('no such node \'' .. name .. '\'', 2) end
|
||||||
|
|
||||||
local node = {}
|
local node = {}
|
||||||
node.children = {}
|
node.children = {}
|
||||||
@ -81,15 +81,59 @@ function Tactree.Tree(name)
|
|||||||
node.update = NodeUpdate
|
node.update = NodeUpdate
|
||||||
node.private = NodePrivateData
|
node.private = NodePrivateData
|
||||||
|
|
||||||
if Tactree.Nodes[name].children then
|
-- this will crash if there is a recursive structure
|
||||||
for _, child in ipairs(Tactree.Nodes[name].children) do
|
-- find a way to prevent this?
|
||||||
table.insert(node.children, Tactree.Tree(child))
|
if Nodes[name].children then
|
||||||
|
for _, child in ipairs(Nodes[name].children) do
|
||||||
|
local cn = Tactree.Tree(child)
|
||||||
|
cn.parent = node
|
||||||
|
table.insert(node.children, cn)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return setmetatable(node, { __index = Tactree.Nodes[name] })
|
return setmetatable(node, { __index = Nodes[name] })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Tactree.Leaf 'Sequence'
|
||||||
|
{
|
||||||
|
start = function(node)
|
||||||
|
node:private().current_child = 1
|
||||||
|
node.children[node:private().current_child]:start()
|
||||||
|
end,
|
||||||
|
tick = function(node)
|
||||||
|
local result = node.children[node:private().current_child]:update()
|
||||||
|
|
||||||
|
if type(result) == 'boolean' then
|
||||||
|
if result then
|
||||||
|
if node:private().current_child == #node.children then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
node:private().current_child = node:private().current_child + 1
|
||||||
|
node.children[node:private().current_child]:start()
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
Tactree.Leaf 'Repeat'
|
||||||
|
{
|
||||||
|
start = function(node)
|
||||||
|
node.children[1]:start()
|
||||||
|
end,
|
||||||
|
tick = function(node)
|
||||||
|
local result = node.children[1]:update()
|
||||||
|
|
||||||
|
if type(result) == 'boolean' then
|
||||||
|
if not result then return false end
|
||||||
|
|
||||||
|
node.children[1]:start()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
Example usage
|
Example usage
|
||||||
-------------
|
-------------
|
||||||
|
Loading…
Reference in New Issue
Block a user