:NAME start

:GLOBAL boolean			options_are_up

:GLOBAL c_object*		touched_object
:GLOBAL boolean			dragging_enabled
:GLOBAL boolean			dragging
:GLOBAL boolean			initial_splash_done
:GLOBAL int32			drag_touch_index
:GLOBAL int32			anchor_index
:GLOBAL s_vector2d		start_drag_point
:GLOBAL s_vector2d		last_drag_point
:GLOBAL float32			last_camera_x
:GLOBAL float32			anchor_x
:GLOBAL float32			velocity_x
:GLOBAL float32			acceleration_x
:GLOBAL float32			last_world_offset_x
:GLOBAL c_object_placement* blind_placement
:GLOBAL int32			load_pending_blind_index
:GLOBAL int32			load_pending_type
:GLOBAL int16			load_chapter_index
:GLOBAL int16			load_level_index
:GLOBAL float32			load_pending_time
:GLOBAL boolean			load_pending
:GLOBAL int32			top_anchor
:GLOBAL float32			max_x
:GLOBAL int32			purchase_act

:GLOBAL boolean			paused
:GLOBAL float32			start_time


k_start= "start"
k_continue= "continue"
k_level_select= "level_select"
k_options= "options"
k_credits= "credits"
k_quit= "quit"
k_purchase_act= "purchase_act"
k_purchase_all= "purchase_all"
k_game_center= "game_center"

k_main_theme_filename= "audio/streaming/music/Third_Eye_Crime_Main_Theme.m4a"

k_escape_key= e_value("e_key", "_key_escape")


-- START Mike Interested
k_main_menu_x= -40
k_splash_x= -80
k_level_anchors= { k_splash_x, k_main_menu_x, 0, 50, 100, 150, 200, 250, 300, 350, 400 }
k_level_anchor_delta= 50
k_min_x= k_splash_x
--k_max_x= 150	-- pax / chapters 1-3 version
k_max_x= 400
k_splash_delay= 3				-- how long we stay on the splash screen when we start
k_splash_transition_time= 4		-- how long it takes to transition from the splash to the main menu
k_first_level_anchor_index= 3
-- END Mike Interested

k_campaign_anchor_offset= 3

k_depth= 0

k_splash_anchor_index= 1
k_main_menu_anchor_index= 2
k_level_select_anchor_index= 3
--k_top_anchor= 6			-- pax / chapters 1-3 version
k_top_anchor= 11


k_drag_acceleration= 100
k_anchor_acceleration= 80
k_damping= 15
k_max_velocity= 100

k_threshold_offset= 1.5

k_load_pending_start= 0
k_load_pending_continue= 1
k_load_pending_level=2
k_load_pending_credits= 3

k_load_pending_time= 1.5
k_load_delay_time= 0.6				-- the amount of time we just hang out after finishing the blinds
k_total_finish_time= k_load_pending_time + k_load_delay_time

k_load_pending_blind_count= 21
k_load_pending_blind_animation_duration= 0.6
k_load_pending_blind_spacing= 0.2
k_load_pending_blind_delay= (k_load_pending_time-k_load_pending_blind_animation_duration)/(k_load_pending_blind_count-1)
k_blind_rotation= -30*math.pi/180
k_blind_offset= {}
k_blind_offset.x= k_load_pending_blind_spacing*math.cos(k_blind_rotation)
k_blind_offset.y= k_load_pending_blind_spacing*math.sin(k_blind_rotation)

k_game_time= e_value("e_time_mode", "_time_mode_game")


function global_initialize(g)
	g.options_are_up= false
	g.dragging_enabled= false
	g.initial_splash_done= false
	
	g.touched_object= nil
	g.dragging= false
	g.drag_touch_index= -1
	g.start_drag_point.zero()
	g.anchor_x= 0
	g.velocity_x= 0
	g.acceleration_x= 0
	g.anchor_index= 2
	g.last_world_offset_x= 0
	
	g.load_pending_type= -1
	g.blind_placement= nil
	g.load_pending= false
	g.load_pending_blind_index= -1
	g.load_pending_time= 0
	g.load_chapter_index= -1
	g.load_level_index= -1
	
	g.paused= true			-- start paused
	g.start_time= -100
	
	g.top_anchor= k_first_level_anchor_index
	
	g.purchase_act= -1
end

function find_campaign()
	local campaign= campaign_get()
	if (campaign.loaded_campaigns.count == 1) then
		campaign.campaign_set(campaign.loaded_campaigns[0].campaign)
	end
end


function music_fadeout()
	-- if the main theme is playing, fade it out
	local ws= world_state_get()
	local sound= ws.sound_manager
	local mid= sound.get_playing_music_id(k_main_theme_filename)
	if (mid ~= -1) then
		sound.fadeout_music(mid, 5, true)
	end		
end



function level_select()
	global.anchor_index= k_level_select_anchor_index
	global.anchor_x= k_level_anchors[k_level_select_anchor_index]
end


function options_start()

	local w= world_get()
	if (w.zone_load("options")) then
		global.options_are_up= true
	end
end

function options_update()
	if (not world_get().zone_is_loaded("options")) then
		global.options_are_up= false
	end
end




-- ------------------- load pending ------------------- 

function load_pending_start(type, chapter_index, level_index, suppress_loading_message)

	global.load_pending_type= type
	global.load_pending= true
	global.load_pending_time= world_state_get().time.time
	global.load_chapter_index= chapter_index
	global.load_level_index= level_index
	
	object_place("bloom")
	
	if (not suppress_loading_message) then
		local loading_obj= object_place("loading")
		if (loading_obj) then
			local tween= loading_obj.tween_get()
			if (tween) then
				tween.play_animation_after_delay("loading", 0.65)
			end
		end
	end
	
	music_fadeout()

end

function load_pending_update()

	local time= world_state_get().time.time
	local delta_t= time - global.load_pending_time
	
	if (delta_t > k_total_finish_time) then

		-- load me
		local campaign= campaign_get()
		if (campaign and campaign.current_campaign) then
			local type= global.load_pending_type
			if (type == k_load_pending_start) then
				campaign.level_load(0,0)
			elseif (type == k_load_pending_continue) then
				local profile= player_profile_get()
				local success= false
				if (profile and profile.last_level_valid()) then
					local last_campaign= profile.last_campaign_get()
					if (last_campaign and campaign.campaign_set(last_campaign)) then
						campaign.level_load(profile.last_chapter_index, profile.last_level_index);
						success= true						
					end
				end
				
				if (not success) then
					-- just start
					campaign.level_load(0,0)
				end
			elseif (type == k_load_pending_credits) then
				campaign.credits_world_load()
			else
				campaign.level_load(global.load_chapter_index, global.load_level_index)
			end
		end
	elseif (global.load_pending_blind_index < (k_load_pending_blind_count-1)) then	
		local desired_index= math.floor(delta_t / k_load_pending_blind_delay)
		if (desired_index > global.load_pending_blind_index) then
		
			global.load_pending_blind_index= desired_index
			
			if (global.blind_placement) then
				
				local blind= world_state_get().place(global.blind_placement)
				if (blind) then
				
					local position= blind.world_position_get()
		
					-- offset to the correct place
					local x= desired_index*k_blind_offset.x + position.x
					local y= desired_index*k_blind_offset.y + position.y
					blind.position_set(x, y)
				end
				
			end
		end
	end
end


-- ------------------------------ dragging ------------------------------


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 touch= touch_interface.touch[global.drag_touch_index]
		local screen_point= touch.screen_position
		
		global.last_camera_x= camera.transform.offset.x
		global.dragging= true
		
		global.start_drag_point.x= screen_point.x
		global.start_drag_point.y= screen_point.y
		global.last_drag_point.x= screen_point.x
		global.last_drag_point.y= screen_point.y
		
		global.acceleration_x= 0
	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.start_drag_point.x
	
	local world_offset= camera.screen_vector_to_world(screen_offset_x, 0, k_depth)
	global.last_drag_point.x= screen_point.x
	global.last_drag_point.y= screen_point.y
	
	local desired_camera_x= global.last_camera_x - world_offset.x
	
	global.acceleration_x = k_drag_acceleration*(desired_camera_x - camera.transform.offset.x)
	global.last_world_offset_x= -world_offset.x
	
end

function drag_end()

	global.dragging= false

	if ((global.anchor_index < global.top_anchor) and (global.last_world_offset_x > k_threshold_offset) and (global.velocity_x >= -0.05)) then
		global.anchor_index = global.anchor_index+1
	elseif ((global.anchor_index > 1) and (global.last_world_offset_x < -k_threshold_offset) and (global.velocity_x <= 0.05)) then
		global.anchor_index = global.anchor_index-1
	end
	
	global.anchor_x = k_level_anchors[global.anchor_index]

end

-- ------------------------------ scrolling system ------------------------------

function spring_update()
	
	local ws= world_state_get()
	local dt= ws.time.delta_t()
	local camera_pos= ws.camera[0].transform.offset
	
	local new_v= global.velocity_x
	local accel= global.acceleration_x - k_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 > global.max_x) then
		camera_pos.x= global.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


-- ------------------------------ BASE SYSTEM ------------------------------

function set_counter(plate_state, object_name, count)
	local object= plate_state.object_get(object_name)
	if (object) then
		local text= object.text_get()
		if (text) then
			local count_string= ""..count
			text.text_set(count_string)
		end
	end
end

function append_to_msg(object, text_string)
	local text= object.text_get()
	if (text) then
		text.append(text_string)
	end
end


function chapter_blocking_refresh()
	local app_settings= app_settings_get()
	local profile= player_profile_get()
	local campaign= campaign_get()
	local w= world_get()
	local ws= world_state_get()
	
	-- clean up, in case we've done this before
	-- note, we delete_all_immediate so that we don't have old copies of object lying around
	-- when we get in there and start grabbing objects and doing stuff to them below
	
	local ui_plate= w.get_plate("block_ui")
	local ui_plate_state= nil
	if (ui_plate) then
		ui_plate_state= ui_plate.plate_state_get()
		ui_plate_state.delete_all_immediate()
	end
	
	local purchase_plate= w.get_plate("purchase_ui")
	local purchase_plate_state= nil
	if (purchase_plate) then
		purchase_plate_state= purchase_plate.plate_state_get()
		purchase_plate_state.delete_all_immediate()
	end
	
	global.purchase_act= -1

	-- figure out the top anchor
	global.top_anchor= k_top_anchor
	if (app_settings and profile) then
		local campaign_record= app_settings.current_campaign_record_get()
		local player_campaign_record= profile.current_campaign_record_get()
		local current_campaign= campaign.current_campaign
		if (campaign_record and player_campaign_record and current_campaign) then
		
			local locked_due_to_purchase= true
		
			-- figure out the last chapter that the PROFILE has unlocked
			-- note, we might have to figure out: does the profile disallow it because of 
			-- PURCHASE state or level progress
			-- right now, the profile ONLY reflects purchase state
			-- and the app-settings reflects level progress
			local purchase_first_locked_chapter= -1
			local purchase_last_unlocked_chapter= -1
			local chapter_count= current_campaign.chapters.count
			while (true) do
				local chapter_index= purchase_last_unlocked_chapter+1
				-- note, if chapter doesn't exist, this returns "locked"
				if (profile.chapter_is_unlocked(current_campaign, chapter_index)) then
					purchase_last_unlocked_chapter= chapter_index
				elseif (chapter_index < chapter_count) then
					local chapter= current_campaign.chapters[chapter_index]
					purchase_first_locked_chapter= chapter_index
					global.purchase_act= chapter.act_index
					break
				else
					break
				end
			end

			-- always let them browse one past how much as they've paid for
			-- NOTE: this lets them look at what they have purchased so far. But that does mean that we shouldn't
			-- still put up the notice that they need to solve more levels to advance to the next chapter!

			global.top_anchor= math.min(purchase_last_unlocked_chapter + k_first_level_anchor_index + 1, k_top_anchor)
			
			local last_unlocked_chapter= 0
			
			if (purchase_last_unlocked_chapter > campaign_record.last_unlocked_chapter) then
				last_unlocked_chapter= campaign_record.last_unlocked_chapter
				locked_due_to_purchase= false
			else
				last_unlocked_chapter= purchase_last_unlocked_chapter
				locked_due_to_purchase= true
			end
		
			-- Are we ACTUALLY blocking anything, or is this just the end of the campaign?
			if (last_unlocked_chapter < current_campaign.chapters.count) then
			
				-- if we are blocked due to level progress (as opposed to purchase state)
				--if (locked_due_to_purchase) then
				-- note that we place the purchase UI whether or not it is actually our purchases that are blocking us
				-- this way the player can always swipe forward, see a few levels, and then get the chance to buy 
				if (purchase_last_unlocked_chapter < current_campaign.chapters.count) then
					-- place everything!
					if (purchase_plate) then
						local all_bundle_exists= true

						ws.place_plate(purchase_plate)
						
						local bundle_id_all= ""..current_campaign.name..".act"..(global.purchase_act+1).."_all"
						local store= store_get()
						if (store) then
							local bundle= store.test_purchase(bundle_id_all, profile)
							if (not bundle) then
								all_bundle_exists= false
							end
						end
						
						-- append the act index to both these messages
						--append_to_msg(act_msg, ""..(global.purchase_act+1))
						--append_to_msg(purchase_act_msg, ""..(global.purchase_act+1))
						
						-- FINALLY ... offset every object on the plate by a fixed amount so it appears in the right place
						local offset= k_level_anchor_delta*(purchase_last_unlocked_chapter+1)
						local obj= purchase_plate_state.objects
						while (obj) do
							local pos= obj.world_position_get()
							local name= obj.placement.name

							-- oof ... weird hacky thing
							-- we can't handle these objects directly, because we are deleting and then recreating the plate
							-- within a single tick. SO, 2 instances of each object exist briefly
							-- so doing object_get(<name>) will probably return the wrong original one

							if ((not all_bundle_exists) and ((name=="purchase_all") or (name=="enticement"))) then
								object_delete(obj)
							else
								if (obj.placement.name=="act_msg" or obj.placement.name=="purchase_act") then
									append_to_msg(obj, ""..(global.purchase_act+1))
								end
								obj.position_set(pos.x+offset, pos.y)
							end
							obj= obj.plate_object_next
						end
						
					end
				end
				
				-- place a the "N completed, M required to advance" ui
				if (not locked_due_to_purchase) then
					local chapter= current_campaign.chapters.count
					if (ui_plate_state) then

						-- place everything!
						ws.place_plate(ui_plate)
						
						local chapter= current_campaign.chapters[campaign_record.last_unlocked_chapter]
						local level_complete_count= 20
						--local badges_complete_count= 20
						local min_points= 20
						
						-- NOTE NOTE NOTE
						-- It used to be that the chapter "points" were levels-beaten+badges aquired.
						-- But this was too complicated. So now it's just levels beaten.

						level_complete_count= player_campaign_record.get_chapter_level_count(campaign_record.last_unlocked_chapter)
						min_points= chapter.min_point_count
						
						set_counter(ui_plate_state, "levels_complete", level_complete_count)
						set_counter(ui_plate_state, "points_required", min_points)
						
						-- FINALLY ... offset every object on the plate by a fixed amount so it appears in the right place
						local offset= k_level_anchor_delta*campaign_record.last_unlocked_chapter
						local obj= ui_plate_state.objects
						while (obj) do
							local pos= obj.world_position_get()
							obj.position_set(pos.x+offset, pos.y)
							obj= obj.plate_object_next
						end
					end
				end
			end
		end
	end
	global.max_x= k_level_anchors[global.top_anchor]
end

function make_purchase(act_index, include_rest)

	local profile= player_profile_get()
	local store= store_get()
	local current_campaign= campaign_get().current_campaign
	
	print("make purchase!")
	
	--[[
	print("purchase "..act_index.."  include all? ")
	if (include_rest) then
		print("yes!")
	else
		print("no")
	end
	--]]
	if (profile and store and current_campaign and (act_index >= 0)) then
	
		-- find the appropriate bundle
		local bundle_id= ""..current_campaign.name..".act"..(act_index+1)
		if (include_rest) then
			bundle_id = bundle_id.."_all"
		end
		--print("buying bundle: "..bundle_id)
		if (store.test_purchase(bundle_id, profile)) then
			store.submit_purchase(bundle_id, profile)
		end
	end
end

function purchase_complete(bundle, successful)
	if (successful) then
		local ws= world_state_get()
		refresh_nubbins()
		chapter_blocking_refresh()
		ws.sound_manager.play_sound_by_name("act_unlock", k_game_time);

	else
		local notification= world_state_get().place_by_plate("loading", "purchase_fail_notification")
		if (notification) then
			local text= notification.text_get()
			if (text) then
				text.append(bundle.name)
			end
		end
	end
end

function start()
	-- figure out which to display, the start or the continue
	local app_settings= app_settings_get()
	local show_continue= false
	find_campaign()
	
	local profile= player_profile_get()
	if (profile and profile.last_level_valid()) then
		show_continue= true
	end
	
	if (show_continue) then
		object_place(k_continue)
	else
		object_place(k_start)
	end
	
	-- FOR CONVENTIONS (Pax, BostonFIG, etc.)
	--if (app_settings) then
	--	app_settings.unlock_all()
	--end

	local ws= world_state_get()
	local sound= ws.sound_manager
	local mid= sound.get_playing_music_id(k_main_theme_filename)
	if (mid == -1) then
		sound.play_music(k_main_theme_filename, true)
	end
	
	local camera= ws.camera[0]
	
	-- figure out where to start the camera
	
	local campaign= campaign_get()
	local start_location_found= false
	
	if (campaign.game_level_played_this_session()) then
		-- figure out the anchor/index
		if (profile.last_campaign == campaign.current_campaign.name) then
			global.anchor_index= profile.last_chapter_index + k_campaign_anchor_offset
			global.anchor_x= k_level_anchors[global.anchor_index]
			camera.transform.offset.x= global.anchor_x
			camera.transform.offset.y= 0
			start_location_found= true
			global.initial_splash_done= true
		end
	end
	
	if (not start_location_found) then
		--move_to_main_menu()

		global.anchor_x= k_splash_x
		global.anchor_index= k_splash_anchor_index
		camera.transform.offset.x= k_splash_x
		camera.transform.offset.y= 0
	end
	
	chapter_blocking_refresh()
	
	global.blind_placement= world_get().placement_get("blind")
	
	-- finally, start the search lights going
	local searchlight1= object_get("searchlight1")
	local searchlight2= object_get("searchlight2")
	if (searchlight1) then
		searchlight1.tween_animation_play("sweep1")
	end
	if (searchlight2) then
		local tween= searchlight2.tween_get()
		if (tween) then
			-- we set the time to offset the phase of the sweep animation a bit
			tween.play_animation("sweep2");
			tween.set_time("sweep2", 4);
		end
	end
	
end

function move_to_main_menu()
	global.anchor_x= k_main_menu_x
	global.anchor_index= k_main_menu_anchor_index
end

function distance(v0, v1)
	local dx= v0.x-v1.x
	local dy= v0.y-v1.y
	return math.sqrt(dx*dx + dy*dy)
end

function update()

	if (global.paused) then
		-- wait until someone unpauses me
	elseif (global.options_are_up) then
		options_update()
	elseif (global.load_pending) then
		load_pending_update()
	else
		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
				
					local touched_object= global.touched_object
					if (touched_object) then
					
						local play_sound= false
					
						-- let's see if we're close enough to be considered a selection
						if (distance(global.start_drag_point, global.last_drag_point) < 0.075) then
							local name= touched_object.placement.name
							
							if (name == k_start) then
								load_pending_start(k_load_pending_start, -1, -1, false)
								--touched_object.tween_animation_play("pressed")
								touched_object.button_press()
								play_sound= true
							elseif (name == k_continue) then
								load_pending_start(k_load_pending_continue, -1, -1, false)
								--touched_object.tween_animation_play("pressed")
								touched_object.button_press()
								play_sound= true
							elseif (name == k_options) then
								options_start()
								--touched_object.tween_animation_play("pressed")
								touched_object.button_press()
								-- note, the options menu plays its own sounds
							--[[
							elseif (name == k_game_center) then
								game_center_invoke()
								touched_object.tween_animation_play("pressed")
								play_sound= true
							--]]
							elseif (name == k_credits) then
								--touched_object.tween_animation_play("pressed")
								touched_object.button_press()
								load_pending_start(k_load_pending_credits, -1, -1, true)
								play_sound= true
							elseif (name == k_quit) then
								quit_game()
								touched_object.button_press()
							--[[
							elseif (name == k_purchase_act) then
								make_purchase(global.purchase_act, false)
								global.touched_object= nil
							elseif (name == k_purchase_all) then
								make_purchase(global.purchase_act, true)
								global.touched_object= nil
							--]]
							else

								-- might be a nubbin

								local campaign= campaign_get()
								if (campaign and campaign.current_campaign) then
									local nubbin_config= touched_object._config.level_selection_nubbin
									if (nubbin_config) then
										local nubbin_var= touched_object._c.level_selection_nubbin
										if (nubbin_var.enabled) then
											
											load_pending_start(k_load_pending_level, nubbin_config.chapter_index, nubbin_config.level_index, false)

											-- the button is an auto-press button
											local button= touched_object.button_get()
											if (button) then
												button.button_enabled_set(false)
												local spring= button.spring_state
												spring.value= button.definition.configuration.spring.max_value
												spring.velocity= 0
											end
											
											play_sound= true
										end
									end
								end
							end
						else
							
							-- touch ended but was not close enough to be an actual selection
							-- play an animation if its a nubbin
							
							--[[
							local nubbin_var= touched_object._c.level_selection_nubbin
							if (nubbin_var and nubbin_var.enabled) then
								local tween= touched_object.tween_get()
								local tween_state= tween.tween_state_get("touched")
								tween_state.reverse(true)

								-- if it's already finished, we may need to restart it								
								if (not tween_state.is_playing()) then
									tween.play_animation("touched")
								end
							end
							--]]

						end
						
						if (play_sound) then
							local ws= world_state_get()
							ws.sound_manager.play_sound_by_name("forward", k_game_time);
						end
					end
				
					drag_end()
					global.touched_object= nil
				end

			elseif (touch_interface.touch_started()) then
			
				if (not global.initial_splash_done) then
					move_to_main_menu()
					global.initial_splash_done= true
				else
					local touch_index= touch_interface.touch_start_index()
					local touch= touch_interface.touch[touch_index]
					local touched_object= touch.touch_handler_object

					global.touched_object= touched_object
					
					-- play an animation if its a nubbin
					--[[
					if (touched_object) then
						local nubbin_var= touched_object._c.level_selection_nubbin
						if (nubbin_var and nubbin_var.enabled) then
							local tween= touched_object.tween_get()
							tween.stop_all()
							tween.reverse(false)
							tween.play_animation("touched");
						end
					end
					--]]
					
					drag_start(touch_interface)
				end
			else
				local keyboard= keyboard_get()
				if (keyboard) then
					if (keyboard.pressed[k_escape_key]) then
						move_to_main_menu()
					end
				end
			end
		end
	end
	
	if (not global.paused) then
		local ws= world_state_get()
		local camera= ws.camera[0]
		
		if (global.start_time < 0) then
			global.start_time= ws.time.time
		end
		
		if (not global.initial_splash_done) then
			local time= ws.time.time - global.start_time - k_splash_delay
			
			if (time > k_splash_transition_time) then
				camera.transform.offset.x= k_main_menu_x
				move_to_main_menu()
				global.initial_splash_done= true
			elseif (time > 0) then
				-- s-curve that mo-fo
				--k_main_menu_x= -40
				--k_splash_x= -80

				local t= time/k_splash_transition_time
				local delta_x= k_main_menu_x - k_splash_x
				camera.transform.offset.x= k_splash_x + 0.5*(1-math.cos(t*math.pi))*delta_x
				
			end
		else
			if (not global.dragging) then
				global.acceleration_x= k_anchor_acceleration*(global.anchor_x - camera.transform.offset.x)
			end

			spring_update()
		end
	end
end


-- ------------------------------ EVENTS ------------------------------

function refresh_nubbins()
	local w= world_get()
	
	-- iterate through the objects on the "NUBBINS" plate and if they are nubbins, invoke a refresh
	
	local plate= w.get_plate("NUBBINS")
	if (plate) then
		local plate_state= plate.plate_state_get()
		if (plate_state) then
			local object= plate_state.objects
			while object do
				local nubbin_script= object._c.level_selection_nubbin
				if (nubbin_script) then
					nubbin_script.refresh= true
				end
				object= object.plate_object_next
			end
		end
	end

end

function player_changed()
	if (global.load_pending) then
		world_state_get().reset()
	else
		refresh_nubbins()
		
		-- reset chapter blocking ui		
		chapter_blocking_refresh()
		
		-- reset player act purchasing
		
		-- other refresh?
		
	end
end

system_register(start, update, nil, nil)

event_register(player_changed, e_value("e_script_event", "_script_event_player_changed"))
event_register(purchase_complete, e_value("e_script_event", "_script_event_purchase_complete"))
