#include "script/common.lua"

------------------------------------------------------------------------
function init()
    merlin = {}
    merlin.emote = 0
    merlin.isRagdoll = false
	merlin.animator = FindAnimator("merlin_animator", true)
    SetAnimationClipFade(merlin.animator, "emote_1", 0.5, 0.5)

    platform = {}
    platform.timer = 0
	platform.motor = FindJoint("joint_platform", true)
	platform.lever = FindShape("lever", true)
	platform.leverjoint = FindJoint("lever_joint", true)
	platform.sound = LoadSound("robot/step-l0.ogg")

    emote_button = {}
    emote_button.shape = FindShape("button_01", true)
    emote_button.anim = 0
    emote_button.transform = GetShapeLocalTransform(emote_button.shape)

    ragdoll_button = {}
    ragdoll_button.shape = FindShape("button_02", true)
    ragdoll_button.anim = 0
    ragdoll_button.transform = GetShapeLocalTransform(ragdoll_button.shape)

	buttonClickSound = LoadSound("clickup.ogg")
    SetTag(emote_button.shape, "interact", "loc@UI_TEXT_WAVE")
    SetTag(ragdoll_button.shape, "interact", "loc@UI_TEXT_RAGDOLL")
end

function tick(dt)
    local ishape = GetPlayerInteractShape()

	if ishape == emote_button.shape and not merlin.isRagdoll and InputPressed("interact") then
        PlayAnimation(merlin.animator, "emote_1")
		PlaySound(buttonClickSound, GetShapeWorldTransform(ishape).pos)
        emote_button.anim = math.pi
    end
    if emote_button.anim > 0 then
        emote_button.anim = clamp(emote_button.anim - dt * 20, 0.0, math.pi)
        local t = TransformCopy(emote_button.transform)
        t.pos[2] = t.pos[2] - 0.1 * math.sin(emote_button.anim)
        SetShapeLocalTransform(emote_button.shape, t)
    end

	if ishape == ragdoll_button.shape and InputPressed("interact") then
		PlaySound(buttonClickSound, GetShapeWorldTransform(ishape).pos)
        ragdoll_button.anim = math.pi
        merlin.isRagdoll = not merlin.isRagdoll
        if merlin.isRagdoll then
            MakeRagdoll(merlin.animator)
            SetTag(platform.lever, "interact", "loc@THROW")
            RemoveTag(emote_button.shape, "interact")
            SetTag(ragdoll_button.shape, "interact", "loc@UI_BUTTON_RESET")
        else
            UnRagdoll(merlin.animator)
            RemoveTag(platform.lever, "interact")
            SetTag(emote_button.shape, "interact", "loc@UI_TEXT_WAVE")
            SetTag(ragdoll_button.shape, "interact", "loc@UI_TEXT_RAGDOLL")
        end
    end
    if ragdoll_button.anim > 0 then
        ragdoll_button.anim = clamp(ragdoll_button.anim - dt * 20, 0.0, math.pi)
        local t = TransformCopy(ragdoll_button.transform)
        t.pos[2] = t.pos[2] - 0.1 * math.sin(ragdoll_button.anim)
        SetShapeLocalTransform(ragdoll_button.shape, t)
    end

	if ishape == platform.lever and InputPressed("interact") then
	    SetJointMotorTarget(platform.leverjoint, 60, 6)
	    SetJointMotorTarget(platform.motor, 1, 15)
		PlaySound(platform.sound, GetShapeWorldTransform(ishape).pos)
        platform.timer = 1
    end
    if platform.timer > 0 then
        platform.timer = platform.timer - dt
        if platform.timer <= 0 then
            SetJointMotorTarget(platform.motor, 0, 2)
            SetJointMotorTarget(platform.leverjoint, 0, 6)
        end
    end

    PlayAnimationLoop(merlin.animator, "idle_menu")
end
