#include "script/toolanimation.lua"

toolAnimator = ToolAnimator()

function init()
	--x,y,z,rx,ry,rz = 5,-.55,-.7,0,0,0
	force = 30 
	
	--RegisterTool("papergun", "PAPER_GUN","MOD/prefab/papergun.xml")
	--SetBool("game.tool.papergun.enabled",true)
	--SetString("game.player.tool","papergun")

	testClearSound = LoadSound("valuable.ogg")
	
	pickupSnd = LoadSound("MOD/sound/papergrab0.ogg")
	shootSnd = LoadSound("MOD/script/paperlauncher/papergun0.ogg")

	triggers = FindTriggers("deliverytrigger", true)
	papers = {}

	shootHaptic = LoadHaptic("MOD/haptic/papergun_fire.xml")
	local toolHaptic = LoadHaptic("MOD/haptic/papergun_tool.xml")
	
	SetToolHaptic("papergun", toolHaptic);

	toolAnimator.armPitchScale = 0.8
end

function tick(dt)
	if GetString("game.player.tool") == "papergun" then

		tickToolAnimator(toolAnimator, dt)

		local currentAmmo = GetInt("game.tool.papergun.ammo")
		if InputPressed("usetool") and GetBool("game.player.canusetool") and currentAmmo > 0 then
			PlaySound(shootSnd)
			PlayHaptic(shootHaptic, 1)

			currentAmmo = currentAmmo - 1
			SetInt("game.tool.papergun.ammo", currentAmmo)
			
			local paper = Spawn("<script file='MOD/script/paper.lua'> <vox pos='0.0 0.0 0.0' file='MOD/vox/paper.vox' tags='unbreakable'/> </script>",bt)[2]		
			
			table.insert(papers, paper)

			local muzzle = GetToolLocationWorldTransform("muzzle")
			SetBodyTransform(paper, muzzle)
			
			if GetBool("level.run") then
				SetTag(paper,"paper")
				SetDescription(paper, "loc@A_NEWSPAPER")
			else
				SetTag(paper,"oldpaper")
				SetDescription(paper, "loc@AN_OLD")
			end

			SetBodyVelocity(paper,TransformToParentVec(GetPlayerCameraTransform(),Vec(0,1,-force)))

			local angVel = Vec(math.random(-5,5),0,0)
			SetBodyAngularVelocity(paper, angVel)

			spawnDust(muzzle.pos, TransformToParentVec(muzzle, Vec(0.0,0.0,-1.0)))
		end
	end
	for i=1, #papers do
		if GetPlayerInteractBody() == papers[i] and InputPressed("interact") then
			if GetBool("level.run") and HasTag(papers[i], "oldpaper") then
				SetString("hud.notification", "loc@UI_HUD_PICKED_UP_NEWSPAPER")
				Delete(papers[i])
				PlaySound(pickupSnd)
			else
				local currentAmmo = GetInt("game.tool.papergun.ammo")
				currentAmmo = currentAmmo + 1
				SetInt("game.tool.papergun.ammo", currentAmmo)
				Delete(papers[i])
				PlaySound(pickupSnd)
			end
		end
	end
	for i=1, #triggers do
		if not HasTag(triggers[i],"cleared") then
			for j=1, #papers do
				if IsBodyInTrigger(triggers[i], papers[j]) and HasTag(papers[j], "paper") then
					SetTag(triggers[i], "cleared")
					Delete(papers[j])
				end
				if IsBodyInTrigger(triggers[i], papers[j]) and not HasTag(papers[j], "testcleared") and not HasTag(papers[j], "paper") then
					SetTag(triggers[i], "testcleared")
					SetTag(papers[j], "testcleared")
					PlaySound(testClearSound)
				end
			end
		end
	end
end

function spawnDust(pos, dir)
	
	ParticleReset()
	ParticleDrag(0.2)
	ParticleTile(0)
	ParticleCollide(0)

	for i = 1, 30, 1 do
		local life = 0.4 + math.random() * 0.4
		
		local c0 = 0.6 + math.random() * 0.2
		local c1 = 0.8 + math.random() * 0.1
		ParticleColor(c0, c0, c0, c1, c1, c1)

		local a = 0.1 + math.random() * 0.2
		ParticleAlpha(a, a, "constant", 0.1, 0.7)

		local radius = 0.1 + math.random() * 0.3
		ParticleRadius(radius, radius)

		ParticleGravity((math.random() * 2.0 - 1.0) * 0.2)

		local angVel = (math.random() * 2.0 - 1.0) * 2.0 * math.pi * 3.0
		ParticleRotation(angVel, angVel*0.5)

		local direction = dir
		direction = VecAdd(direction, VecScale(Vec(math.random() * 2.0 - 1.0, math.random() * 2.0 - 1.0, math.random() * 2.0 - 1.0), 0.6))
		
		local velocity = VecScale(direction, 1.0 + math.random() * 3.0)
		local position = VecAdd(pos, VecScale(dir, math.floor(i/3) * 0.5))
		SpawnParticle(position, velocity, life)
	end
end