-- =============================================================================
-- Animation Tree Registry Lua API Examples
-- =============================================================================
-- Animation trees are stored in the Animation Tree Registry
-- (animation_tree.json).  They are referenced from entities by name and edited
-- in the registry editor (Tools -> Animation Tree Registry).
--
-- The Lua API lets you create, query and remove registry entries.
-- =============================================================================

-- Register a simple humanoid locomotion tree.
-- skeletonSource is optional; when set, the registry editor loads that mesh
-- from the Characters resource group to populate animation-name dropdowns.
ecs.animation_trees.register("humanoid", {
    skeletonSource = "male_Face.mesh",
    root = {
        type = "output",
        speed = 1.0,
        children = {
            {
                type = "stateMachine",
                name = "main",
                fadeSpeed = 7.5,
                children = {
                    {
                        type = "state",
                        name = "idle",
                        children = {
                            { type = "animation", animationName = "idle" }
                        }
                    },
                    {
                        type = "state",
                        name = "walk",
                        children = {
                            { type = "animation", animationName = "walk" }
                        }
                    },
                    {
                        type = "state",
                        name = "run",
                        children = {
                            { type = "animation", animationName = "run" }
                        }
                    }
                },
                endTransitions = {
                    -- auto-transition when the source state's animation ends
                    idle = "walk"
                }
            }
        }
    }
})

-- List all registered trees
local trees = ecs.animation_trees.list()
for _, name in ipairs(trees) do
    print("Registered animation tree: " .. name)
end

-- Query a tree definition
local def = ecs.animation_trees.find("humanoid")
if def then
    print("Tree: " .. def.name)
    print("Skeleton source: " .. def.skeletonSource)
    print("Root type: " .. def.root.type)
end

-- Attach the tree to an entity
local entity = ecs.create_entity()
ecs.set_component(entity, "AnimationTree", {
    treeName = "humanoid",
    enabled = true,
    useRootMotion = false
})

-- Persist the registry to disk
ecs.animation_trees.save()
