local last_angle = {}
local sneak_head = 0.5
-- se skin db estiver instalado, o valor deve ser 0

btmt.player_animation = core.settings:get_bool("player_animation", true)

if btmt.player_animation then 

local function set_fun_anim (player)
    for _, p in pairs(minetest.get_connected_players()) do
        local n = p:get_player_name()
        local controls = p:get_player_control()
        local vert = -p:get_look_vertical()
        local pos = p:get_pos()
        local node = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z})
        local is_water = node.name == "default:water_source" or node.name == "default:water_flowing"
        local is_air = node.name == "air" 
        
        -- Tive como referencia headanim mod :)
        if not last_angle[n] or last_angle[n].v ~= vert or last_angle[n].h ~= rel_h then
            last_angle[n] = {v = vert, h = rel_h}
            
            p:set_bone_override("Head", {
                position = {vec = vector.new(0, 0, 0)},
                rotation = {vec = {x = vert, y = 0, z = 0}, interpolation = 0.1}
            })
        end                   
        ---------------------------------------------------------------------------------------                    
            if controls.aux1 then 
                     
               p:set_bone_override("Body", {
                    position = {vec = vector.new(0, -0.35, 0)},
                    rotation = {vec = {x = -0.2, y = 0, z = 0}, interpolation = 0.1}
                })
                p:set_bone_override("RightLeg", {
                    rotation = {vec = {x = 1.0, y = 0, z = 0}, interpolation = 0.1}
                })
                p:set_bone_override("LeftLeg", {
                    rotation = {vec = {x = 1.0, y = 0, z = 0}, interpolation = 0.1}
                })
                p:set_bone_override("Arm_Right", {
                    rotation = {vec = {x = -0.4, y = 0, z = 0}, interpolation = 0.1}
                })
                p:set_bone_override("Arm_Left", {
                    rotation = {vec = {x = -0.4, y = 0, z = 0}, interpolation = 0.1}
                })                                                                                         
	------------------------------------------------------------------------------------------------
             elseif controls.sneak then	           	          
                p:set_bone_override("Head", {
                position = {vec = vector.new(0, 0, 0)},
                rotation = {vec = {x = vert + sneak_head, y = 0, z = 0}, interpolation = 0.1}
                })           
                p:set_bone_override("Body", {
                    position = {vec = vector.new(0,-1,0)}, -- position = {vec = vector.new(0, -0.35, 0)},
                    rotation = {vec = {x = -0.4, y = 0, z = 0}, interpolation = 0.1}
                })
                p:set_bone_override("Leg_Right", {
                    position = {vec = vector.new(0, 1, -0.5)},
                    rotation = {vec = {x=0.4, y=0, z=0}, interpolation = 0.1}
                })
                p:set_bone_override("Leg_Left", {
                    position = {vec = vector.new(0, 1, -0.5)},
                    rotation = {vec = {x=0.4, y=0, z=0}, interpolation = 0.1}
                })  
            
            elseif controls.jump  or is_air then               	
              p:set_bone_override("Arm_Right", {
                    rotation = {vec = {x = 0, y = 0, z = 0.4}, interpolation = 0.1}
                })
                p:set_bone_override("Arm_Left", {
                    rotation = {vec = {x = 0, y = 0, z = -0.4}, interpolation = 0.1}
                })                     
            
            else                      
                p:set_bone_override("Head", {
                  position = {vec = vector.new(0, 0, 0)},
                  rotation = {vec = {x = vert, y = 0, z = 0}, interpolation = 0.1}
                })
                p:set_bone_override("Body", {
                    position = {vec = vector.new(0, 0, 0)},
                    rotation = {vec = {x = 0, y = 0, z = 0}, interpolation = 0.1}
                })
                p:set_bone_override("Leg_Right", {
                    rotation = {vec = {x = 0, y = 0, z = 0}, interpolation = 0.1}
                })
                p:set_bone_override("Leg_Left", {
                    rotation = {vec = {x = 0, y = 0, z = 0}, interpolation = 0.1}
                })
                
                p:set_bone_override("Arm_Right", {
                    rotation = {vec = {x = 0, y = 0, z = 0}, interpolation = 0.1}
                })
                p:set_bone_override("Arm_Left", {
                    rotation = {vec = {x = 0, y = 0, z = 0}, interpolation = 0.1}
                }) 
                
                --core.log("teste")               
            end     
    end   
end
    
minetest.register_globalstep(function()
  set_fun_anim (player)
end)
	
minetest.register_on_leaveplayer(function(p)
    last_angle[p:get_player_name()] = nil
end)

end 
