-- Animated escape vehicle forward when global state is win
#include "script/common.lua"
#include "script/wheels.lua"

pAcceleration = GetFloatParam("acceleration", 6)
pMaxSpeed = GetFloatParam("maxspeed", 20)
pDynamic = GetBoolParam("dynamic", false)
pMap = GetStringParam("map", "")
pArrowSize = 2


function init()
	body = FindBody("escapevehicle")
	transform = GetBodyTransform(body)

	local arrow = FindLocation("arrow")
	if arrow ~= 0 then
		arrowSprite = LoadSprite("gfx/arrowdown.png")
        local arrow_tr = GetLocationTransform(arrow)
		arrowPos = arrow_tr.pos
		arrowPos[2] = arrowPos[2] + pArrowSize/2
        arrowLTr = TransformToLocalTransform(transform, arrow_tr)
	end

	cameraTr = GetLocationTransform(FindLocation("escapecamera"))
    cameraLTr = TransformToLocalTransform(transform, cameraTr)
    gLerpPos = nil
    gLerpRot = nil
	shapes = GetBodyShapes(body)
	for i=1, #shapes do SetShapeEmissiveScale(shapes[i], 0.0) end
	-- SetTag(body, "interact", "loc@PMESSAGE_ESCAPE")
	if pMap ~= "" then
		SetTag(body, "escapevehicle", pMap)
	end
	SetTag(body, "nocull")
	endscreenTrigger = FindTrigger("endscreen")
	saveEndscreenScene(endscreenTrigger)
end


function tick(dt)
	local state = GetString("level.state")
	local complete = GetBool("level.complete")

	--Outline escape vehicle when mission is complete
	if state == "" then
		if complete then
			--Draw escape vehicle arrow
			if arrowSprite and not GetBool("game.map.enabled") then
				local c = GetCameraTransform().pos
                arrowPos = TransformToParentTransform(GetBodyTransform(body), arrowLTr).pos
				c[2] = arrowPos[2]
				local p = VecCopy(arrowPos)
				p[2] = p[2] + math.sin(GetTime()*5)*0.3+0.3
				local t = Transform(p, QuatLookAt(arrowPos, c))
				DrawSprite(arrowSprite, t, pArrowSize, pArrowSize, 1, 1, 1, 1, true, false)
			end
		end

		if GetPlayerInteractBody() == body then
			if complete then
				DrawBodyOutline(body, 1)
			end
			if InputPressed("interact") then
				if complete then
					SetString("level.state", "win")
                    -- SetTriggerTransform(endscreenTrigger, TransformToParentTransform(GetBodyTransform(body), endscreenTriggerLTr))
					cleanEndscreenScene(endscreenTrigger)
				elseif not GetBool("hud.hasnotification") then
					SetString("hud.notification", GetTranslatedStringByKey("UI_HUD_NOTE_YOU_NEED"))
				end
			end
		end

	end

	--Third person win/lose camera
	if state == "win" then
        if not gLerpPos then
            gLerpPos = 0
            gLerpRot = 0
            SetValue("gLerpPos", 1, "easeout", 3)
            SetValue("gLerpRot", 1, "cosine", 5)

            cameraTr = TransformToParentTransform(GetBodyTransform(body), cameraLTr)
        end
        if math.min(gLerpPos, gLerpRot) ~= 1 then
            local pc = GetPlayerCameraTransform()
            t = Transform(VecLerp(pc.pos, cameraTr.pos, gLerpPos), QuatSlerp(pc.rot, cameraTr.rot, gLerpRot))
        end
		SetBool("game.disableinput", true)
        pcall(SetPlayerHidden)
		SetCameraTransform(t, 90)
	end
end

function saveEndscreenScene(trigger)
	if not IsHandleValid(trigger) then return end

	endscreen_Shapes = {}
	local shapes = FindShapes(nil, true)
	for i = 1, #shapes do
		local shape = shapes[i]
		if IsShapeInTrigger(trigger, shape) then
			endscreen_Shapes[#endscreen_Shapes+1] = shape
		end
	end
end

function cleanEndscreenScene(trigger)
	if not IsHandleValid(trigger) then return end

	local shapes = FindShapes(nil, true)
	for i = 1, #shapes do
		local shape = shapes[i]
		if IsShapeInTrigger(trigger, shape) then
			local was_saved = false
			for j = 1, #endscreen_Shapes do
				if endscreen_Shapes[j] == shape then
					table.remove(endscreen_Shapes, j)
					was_saved = true
					break
				end
			end
			if not was_saved then
				Delete(shape)
			end
		end
	end
end
