LoadFile("DATA:levels/utility/tutorial_finger.lua")

local TUT_TIME = 10
local EETS_SCARED_HASH = World_HashString("eets_scared")
local EETS_STUCK_MIN_THRESHOLD = fixed_float(245)
local EETS_STUCK_MAX_THRESHOLD = fixed_float(270)

local simulating = false
local scared_eets = nil
local eets_stuck = false
local whale_tossed_not_eets = false
local timer = 0
local timer_max = TUT_TIME

local whale_finger = nil

function OnInit()
	for object in World_GetObjects() do
		if object:GetBlueprintName() == "whale" then
			whale_finger = TapObjectTutorialFinger(object)
			break
		end
	end
end

function OnStartSimulation()
	simulating = true
	scared_eets = nil
	eets_stuck = false
	whale_tossed_not_eets = false
end

function OnStopSimulation()
	simulating = false
	scared_eets = nil
	whale_finger:Stop()
	if eets_stuck and not whale_tossed_not_eets and timer < timer_max then
		timer = 0
		if timer_max == TUT_TIME then
			timer_max = timer_max / 2
		else
			timer_max = 0
		end
	end
end

function OnWhaleTossNotEets()
	whale_tossed_not_eets = true
	whale_finger:Stop()
	timer = 0
	timer_max = TUT_TIME
end

function OnUpdateInModal()
	whale_finger:SetVisible(false)
end

function OnUpdatePlaying()
	whale_finger:SetVisible(true)
end

function OnUpdateGUI(dt)
	whale_finger:OnUpdate(dt)
	if simulating and not whale_tossed_not_eets then
		if eets_stuck then
			if timer < timer_max then
				timer = timer + dt
				if timer >= timer_max then
					whale_finger:Start()
				end
			elseif not whale_finger:IsPlaying() then
				whale_finger:Start()
			end
		elseif scared_eets then
			local ypos = scared_eets:GetPosition().y
			if	ypos > EETS_STUCK_MIN_THRESHOLD and
				ypos < EETS_STUCK_MAX_THRESHOLD and
				scared_eets:GetWalkingExtension():GetState() ~= WalkingExtension.WES_Falling then
				eets_stuck = true
			end
		else
			scared_eets = World_GetEets()
			if scared_eets:GetBlueprintHash() ~= EETS_SCARED_HASH then
				scared_eets = nil
			end
		end
	end
end