-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

function onRunning(speed)
    if speed>0.01 then
        if Figure and Humanoid and Humanoid.WalkSpeed<17 then
            playAnimation("walk", 0.1, Humanoid);
        elseif Figure and Humanoid and Humanoid.WalkSpeed>17 then
            playAnimation("run", 0.1, Humanoid);
        end;
        if currentAnimInstance and currentAnimInstance.AnimationId == "http://www.roblox.com/asset/?id=180426354" then
            setAnimationSpeed(speed / 14.5)
        end
        pose = "Running"
    else
        playAnimation("idle", 0.1, Humanoid)
        pose = "Standing"
    end
end
function onDied()
    pose = "Dead"
end
function onJumping()
    playAnimation("jump", 0.1, Humanoid)
    jumpAnimTime = jumpAnimDuration
    pose = "Jumping"
end
function onClimbing(speed)
    playAnimation("climb", 0.1, Humanoid)
    setAnimationSpeed(speed / 12.0)
    pose = "Climbing"
end
function onGettingUp()
    pose = "GettingUp"
end
function onFreeFall()
    if (jumpAnimTime <= 0) then
        playAnimation("fall", fallTransitionTime, Humanoid)
    end
    pose = "FreeFall"
end
function onFallingDown()
    pose = "FallingDown"
end
function onSeated()
    pose = "Seated"
end
function onPlatformStanding()
    pose = "PlatformStanding"
end
function onSwimming(speed)
    if speed>0 then
        pose = "Running"
    else
        pose = "Standing"
    end
end

function getTool()
    for _, kid in ipairs(Figure:GetChildren()) do
        if kid.className == "Tool" then return kid end
    end
    return nil
end

function getToolAnim(tool)
    for _, c in ipairs(tool:GetChildren()) do
        if c.Name == "toolanim" and c.className == "StringValue" then
            return c
        end
    end
    return nil
end

function animateTool()

    if (toolAnim == "None") then
        playToolAnimation("toolnone", toolTransitionTime, Humanoid)
        return
    end

    if (toolAnim == "Slash") then
        playToolAnimation("toolslash", 0, Humanoid)
        return
    end

    if (toolAnim == "Lunge") then
        playToolAnimation("toollunge", 0, Humanoid)
        return
    end
end

function moveSit()
    RightShoulder.MaxVelocity = 0.15
    LeftShoulder.MaxVelocity = 0.15
    RightShoulder:SetDesiredAngle(3.14 /2)
    LeftShoulder:SetDesiredAngle(-3.14 /2)
    RightHip:SetDesiredAngle(3.14 /2)
    LeftHip:SetDesiredAngle(-3.14 /2)
end

local lastTick = 0

function move(time)
    local amplitude = 1
    local frequency = 1
    local deltaTime = time - lastTick
    lastTick = time

    local climbFudge = 0
    local setAngles = false

    if (jumpAnimTime > 0) then
        jumpAnimTime = jumpAnimTime - deltaTime
    end

    if (pose == "FreeFall" and jumpAnimTime <= 0) then
        playAnimation("fall", fallTransitionTime, Humanoid)
    elseif (pose == "Seated") then
        playAnimation("sit", 0.5, Humanoid)
        return
    elseif (pose == "Running") then
        if Figure and Humanoid and Humanoid.WalkSpeed<17 then
            playAnimation("walk", 0.1, Humanoid);
        elseif Figure and Humanoid and Humanoid.WalkSpeed>17 then
            playAnimation("run", 0.1, Humanoid);
        end;
    elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
        stopAllAnimations()
        amplitude = 0.1
        frequency = 1
        setAngles = true
    end
    if (setAngles) then
        local desiredAngle = amplitude * math.sin(time * frequency)
        RightShoulder:SetDesiredAngle(desiredAngle + climbFudge)
        LeftShoulder:SetDesiredAngle(desiredAngle - climbFudge)
        RightHip:SetDesiredAngle(-desiredAngle)
        LeftHip:SetDesiredAngle(-desiredAngle)
    end
    -- Tool Animation handling
    local tool = getTool()
    if tool and tool:FindFirstChild("Handle") then
        local animStringValueObject = getToolAnim(tool)
        if animStringValueObject then
            toolAnim = animStringValueObject.Value
            -- message recieved, delete StringValue
            animStringValueObject.Parent = nil
            toolAnimTime = time + .3
        end
        if time > toolAnimTime then
            toolAnimTime = 0
            toolAnim = "None"
        end
        animateTool()
    else
        stopToolAnimations()
        toolAnim = "None"
        toolAnimInstance = nil
        toolAnimTime = 0
    end
end