#include "script/toolanimation.lua"

function init()
	RegisterTool("dynamite_bundle", "loc@TOOL_NAME_DYNAMITEBUNDLE", "MOD/prefabs/tools/dynamite_bundle_in_hand.xml", 4)
	--SetBool("game.tool.dynamite_bundle.enabled", true)
	--SetInt("game.tool.dynamite_bundle.ammo", 2)
	toolAnimator = ToolAnimator()
end

function tick(dt)
	if GetString("game.player.tool") == "dynamite_bundle" then
		if GetInt("game.tool.dynamite_bundle.ammo") > 0 then

			local playerT = GetPlayerCameraTransform()

			if InputPressed("usetool") and GetBool("game.player.canusetool") then
				local depth = 4.5
				if GetBool("game.thirdperson") then depth = 6 end
				local hit, dist, normal, shape = QueryRaycast(playerT.pos, QuatRotateVec(playerT.rot, Vec(0, 0, -1)), depth)
				canPlace = hit
				if  hit then
					if HasTag(shape, "horse") then return end
					local hitPos = TransformToParentPoint(playerT, Vec(0, 0, -dist))
					if normal[2] > 0.9 then
						local spawnTransform = Transform(hitPos)
						Spawn("MOD/prefabs/tools/dynamite_bundle.xml", spawnTransform, true)
					else
						local xAxis = Vec(1.0, 0, 0)
						normal = VecScale(normal, -1.0)
						local rotateAxis = VecCross(xAxis, normal)
						rotateAxis = VecNormalize(rotateAxis)
						local angle = (180.0 / math.pi) * math.acos(VecDot(xAxis, normal))
						local spawnTransform = Transform(hitPos, QuatAxisAngle(rotateAxis, angle))
						local entity = Spawn("MOD/prefabs/tools/dynamite_bundle_rope.xml", spawnTransform, false, false)
						local newbody = GetShapeBody(shape)
						local newtransform = Transform()
						local entityTransform = TransformToParentTransform(GetShapeWorldTransform(entity[3]), Transform(Vec(0.26, 0.12, 0)))
						if newbody~= 1 then
							newtransform = TransformToLocalTransform(GetBodyTransform(newbody), entityTransform)
							newtransform2 = TransformToLocalTransform(GetBodyTransform(newbody), GetShapeWorldTransform(entity[4]))
						else
							newtransform = TransformToParentTransform( GetBodyTransform(newbody), entityTransform)
							newtransform2 = TransformToParentTransform( GetBodyTransform(newbody), GetShapeWorldTransform(entity[4]))
						end
						SetShapeBody(entity[3], newbody, newtransform)
						SetShapeBody(entity[4], newbody, newtransform2)
					end
					SetInt("game.tool.dynamite_bundle.ammo", GetInt("game.tool.dynamite_bundle.ammo") - 1)
					return
				end
			end
		else
			toolAnimator.offsetTransform = Transform(Vec(0, -100, 0))
		end
		if getEyeHeight() < 0.9 then
			toolAnimator.armPitchScale = 0.2
			toolAnimator.toolPitchScale = 0.1
		else
			toolAnimator.armPitchScale = 0.4
			toolAnimator.toolPitchScale = 0.1
		end
		tickToolAnimator(toolAnimator, dt)
	end
end

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

