#include "script/common.lua"

function init()
	joints = FindJoints("anim", true)
end

function tick(dt)
	for i=1, #joints do
		local joint = joints[i]
		local t = GetTagValue(joint, "anim")
		if t == "sine" then
			local period = tonumber(GetTagValue(joint, "period")) or 4
			local offset = tonumber(GetTagValue(joint, "offset")) or 0
			local strength = tonumber(GetTagValue(joint, "strength")) or 1000
			local mi, ma = GetJointLimits(joint)
			mi = mi + 0.2
			ma = ma - 0.2
			local target = (math.sin(math.pi * 2 * (GetTime()+offset)/period) * 0.5 + 0.5) * (ma-mi) + mi
			SetJointMotorTarget(joint, target, 10.0, strength)
		elseif t == "rot" then
			local speed = tonumber(GetTagValue(joint, "speed")) or 1
			local strength = tonumber(GetTagValue(joint, "strength")) or 1000
			SetJointMotor(joint, speed, strength)
		else
			SetJointMotor(joint, 0, 0)
		end
	end
end

