#include "game.lua"


dieFade = 0

function init()
	local ammoScale = 1
	local s = GetFloat("options.game.campaign.ammo")
	if s == -1 then
		ammoScale = 0
	else
		ammoScale = ammoScale + s/100
	end

	checkSandbox = false
	
	particletimer = 0
	
	flashlightinit()
	
end

function flashlightinit()
	timer = 0
	starttimer = 0
	isinit =false

	torchbody = FindBody("torchbody")
	lightthing = FindLight("lightthing")
	OnSound = LoadSound("MOD/sound/lamp-on.ogg")
	OffSound = LoadSound("MOD/sound/lamp-off.ogg")
end

function giveTools()
	for id,tool in pairs(gTools) do
		SetBool("game.tool."..id..".enabled", true)
		for j=1, #tool.upgrades do
			local prop = tool.upgrades[j].id
			local value = tool.upgrades[j].max
			local saved = GetFloat("savegame.mod.tool."..id.."."..prop)
			if saved > value then
				value = saved 
			end
			if prop == "ammo" then
				SetInt("game.tool."..id..".ammo.max", value)
			end
			SetFloat("game.tool."..id.."."..prop, value)
		end
	end
	SetString("game.player.tool", "sledge")
end

function tick(dt)

	if GetBool("level.sandbox") and not checkSandbox then
		SetBool("game.mod.lamp", true)
		giveTools()
		checkSandbox = true
	end
		--Fade to black and respawn when dead
	if GetFloat("game.player.health") == 0 and (GetBool("level.sandbox") or GetBool("level.hub")) then
		if dieFade == 0 then
			SetValue("dieFade", 1, "linear", 4)
		end
		if dieFade == 1 then
			RespawnPlayer()
		end
	else
		if dieFade == 1 then
			SetValue("dieFade", 0, "linear", 1)
		end	
	end
	
	flashlighttick(dt)
end


function draw(dt)
	if dieFade > 0 then
		UiColor(0,0,0, dieFade)
		UiRect(UiWidth(), UiHeight())
	end
end

function flashlighttick(dt)
	if starttimer > 0.1 and not isinit then
		local lights = FindLights('', true)

		for i=1, #lights do
			local light = lights[i]
			local trans = GetLightTransform(light)
			local pos = trans.pos
			local rot = trans.rot
			local ptrans = GetPlayerPos()

			local dist = VecLength(VecSub(pos, ptrans))

		   if dist < 1 then
				SetLightIntensity(light, 0)
				break
			end
		end
		isinit = true
	else
		starttimer = starttimer+dt
	end

	if InputPressed("flashlight") then
		if GetBool("game.mod.lamp") then
			if torchon then
				PlaySound(OffSound)
				torchon = false
			else
				torchon = true
				PlaySound(OnSound, GetPlayerPos(), 1)
			end
		end
	end
	
	if torchon then
		local eyeTransform = GetPlayerEyeTransform()
		lightpos = eyeTransform.pos

		if GetBool("game.thirdperson") then
			-- move lantern in front of character
			local fwd = QuatRotateVec(eyeTransform.rot, Vec(0,0,-1))
			fwd[2] = 0.0
			fwd = VecNormalize(fwd)
			lightpos = VecAdd(lightpos, VecScale(fwd, 1.0))
			lightpos[2] = lightpos[2] - 0.5
		end

		
		shakex = math.random(-5, 5)/40
		shakey = math.random(-5, 5)/40
		shakez = math.random(-5, 5)/40
		
		lightpos2 = VecAdd(lightpos, Vec(shakex,shakey,shakez))
		SetLightColor(lightthing, 0.9, 0.35, 0)
		SetLightIntensity(lightthing, 2*math.random(4.5,5))
		SetBodyTransform(torchbody, Transform(lightpos2))

	else
		SetBodyTransform(torchbody, Transform(Vec(1000000,100000,10000)))
	end
end

