#include "script/toolanimation.lua"

function init()
	RegisterTool("dynamite_stick", "loc@TOOL_NAME_DYNAMITE", "MOD/prefabs/tools/dynamite_stick_in_hand.xml", 4)
--	SetBool("game.tool.dynamite_stick.enabled", true)
	--SetInt("game.tool.dynamite_stick.ammo", 4)
	toolAnimator = ToolAnimator()
	toolAnimator.armPitchScale = 0.2
	toolAnimator.toolPitchScale = 0.1
	toolAnimator.maxActionPoseTime = 0.1
end

function tick(dt)
	if GetString("game.player.tool") == "dynamite_stick" then
		if GetInt("game.tool.dynamite_stick.ammo") > 0 then
			SetToolTransform(Transform(Vec(0.3, -0.57, -0.55)))
			if InputPressed("usetool") and GetBool("game.player.canusetool") then
				local tool = GetToolBody()
				toolTrans = GetBodyTransform(tool)
				Spawn("MOD/prefabs/tools/dynamite_stick.xml", Transform(TransformToParentPoint(toolTrans, Vec(0, 0, -1))))
				SetInt("game.tool.dynamite_stick.ammo", GetInt("game.tool.dynamite_stick.ammo")-1)
			end
		else
			toolAnimator.offsetTransform = Transform(Vec(0, -100, 0))
		end
		tickToolAnimator(toolAnimator, dt)
	end
end

function render(dt)
	if GetString("game.player.tool") == "dynamite_stick" then
		local toolBody = GetToolBody()
		local toolShapes = GetBodyShapes(toolBody)
		for i = 1, #toolShapes, 1 do
			if toolAnimator.timeSinceFire < 0.1 then
				SetTag(toolShapes[i], "invisible")
			else
				RemoveTag(toolShapes[i], "invisible")
			end
		end
	end
end