:NAME interstitial_camera

:GLOBAL boolean			dragging
:GLOBAL boolean			loading_next
:GLOBAL int32			drag_touch_index
:GLOBAL s_vector2d		last_drag_point
:GLOBAL float32			last_camera_x
:GLOBAL float32			velocity_x
:GLOBAL float32			acceleration_x
:GLOBAL int32			music_id


k_depth= -5
k_drag_acceleration= 100
k_drag_damping= 15
k_default_damping= 5
k_max_velocity= 100
k_min_x= 3
k_max_x= 169

local interstitiallib = require "campaigns/main/interstitials/common/interstitial_utils"



function global_initialize(g)
	g.dragging= false
	g.drag_touch_index= -1
	g.last_camera_x= 0
	g.velocity_x= 0
	g.acceleration_x= 0
	g.loading_next= false
	g.music_id= -1

end



function start()
	local ws= world_state_get()
	if (ws) then
		local camera= ws.camera[0]
		camera.transform.offset.x= k_min_x
		camera.transform.offset.y= 0
		camera.z= 10
		camera.fovy= 90
	end

	-- unlike most levels, we IMMEDIATELY unlock the next level
	local app_settings= app_settings_get()
	if (app_settings) then
		app_settings.unlock_next()
	end

	-- start music	
	interstitiallib.music_start()
	
end

function advance()
	local campaign= campaign_get()
	if (campaign) then
		campaign.load_next_level()
	end
end

function start_blinds()
	local blinds_script= world_state_get().global_script("blinds")
	if (blinds_script) then
		blinds_script.started= true
	end
	object_place("chapter")
	global.loading_next= true
	interstitiallib.music_fadeout()	
end

function drag_start(touch_interface)

	global.drag_touch_index= touch_interface.touch_start_index()
	
	if (global.drag_touch_index >= 0 and global.drag_touch_index < 4) then
		local camera= world_state_get().camera[0]
		local screen_point= touch_interface.touch[global.drag_touch_index].screen_position

		global.last_camera_x= camera.transform.offset.x
		global.last_drag_point.set(screen_point.x, screen_point.y)
		global.acceleration_x= 0
		global.dragging= true
	end
end

function drag_update(touch_interface)

	local camera= world_state_get().camera[0]
	local touch= touch_interface.touch[global.drag_touch_index]
	local screen_point= touch.screen_position
	local screen_offset_x= screen_point.x - global.last_drag_point.x
	local world_offset= camera.screen_vector_to_world(screen_offset_x, 0, k_depth)
	local desired_camera_x= global.last_camera_x - world_offset.x
	
	global.acceleration_x = k_drag_acceleration*(desired_camera_x - camera.transform.offset.x)
	
end

function drag_end()

	global.dragging= false

end

-- ------------------ spring system update ------------------

function spring_update()
	
	local ws= world_state_get()
	local dt= ws.time.delta_t()
	local camera_pos= ws.camera[0].transform.offset
	
	local damping= k_default_damping
	if (global.dragging) then
		damping= k_drag_damping
	end
	
	local new_v= global.velocity_x
	local accel= global.acceleration_x - damping*new_v
	
	new_v = new_v + accel*dt
	
	if (new_v > k_max_velocity) then
		new_v= k_max_velocity
	elseif (new_v < -k_max_velocity) then
		new_v= -k_max_velocity
	end
	
	camera_pos.x = camera_pos.x + new_v*dt
	
	if (camera_pos.x > k_max_x) then
		camera_pos.x= k_max_x
		new_v= 0
	elseif (camera_pos.x < k_min_x) then
		camera_pos.x= k_min_x
		new_v= 0
	end
	
	global.acceleration_x= 0
	global.velocity_x= new_v
end


function update()

	if (global.loading_next) then
		local blinds_script= world_state_get().global_script("blinds")
		local success= false
		if (blinds_script) then
			if (blinds_script.finished) then
				success= true
			end
		else
			success= true
		end
		
		if (success) then
			advance()
		end
	
	else
	
		local camera= world_state_get().camera[0]
		local touch_interface= touch_get()
		if (touch_interface) then
			if (global.dragging) then
			
				local touch_end= true

				if (global.drag_touch_index >= 0 and global.drag_touch_index < 4) then
					local touch= touch_interface.touch[global.drag_touch_index]
					if (touch.is_active()) then
						drag_update(touch_interface)
						touch_end= false
					end
				end

				if (touch_end) then
					drag_end()
				end
			
			elseif (touch_interface.touch_started()) then
				
				local touch_index= touch_interface.touch_start_index()
				local touch= touch_interface.touch[touch_index]
				local handled= false
				
				local touched_object= touch.touch_handler_object
				if (touched_object and touched_object.placement.name == "advance") then
					local button= touched_object.button_get()
					if (button) then
						button.button_enabled_set(false)
					end
					start_blinds()
					handled= true
				end
				
				if (not handled) then
					drag_start(touch_interface)
				end
			end
		end
		
		--if (world_state_get().time.time > 5) then
			--global.velocity_x = world_state_get().time.time-1
			--if (global.velocity_x > 5) then
				--global.velocity_x= 5
			--end
			
		--	global.velocity_x = 3*world_state_get().time.time-15
		--	if (global.velocity_x > 12) then
		--		global.velocity_x= 12
		--	end
		--end
		
		spring_update()
			
		world_state_get().cinematic_time_set(camera.transform.offset.x)
	end

end

system_register(start, update, nil, nil)
