:NAME interstitial_sfx


:PARAM uint16 unused

:SOUND_TRIGGER_DEFINITION string							name
:SOUND_TRIGGER_DEFINITION float32							trigger_time

:SOUND_TRIGGER_STATE boolean								triggered
:SOUND_TRIGGER_STATE uint16									definition_index


:SOUND_CONTINUOUS_DEFINITION string							name
:SOUND_CONTINUOUS_DEFINITION s_function1d					volume_func

:SOUND_CONTINUOUS_STATE uint16								definition_index


:CONFIG sound_trigger_definition[]				trigger_definitions
:CONFIG sound_continuous_definition[]			continuous_definitions

:VAR sound_trigger_state[]						trigger_states
:VAR sound_continuous_state[]					continuous_states
:VAR boolean									started

-- ------------------- constants ------------------- 

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

-- ------------------- initialization ------------------- 

function param_initialize(p)
end

function config_initialize(c)
end

function var_initialize(v)
	v.started= false
end

-- ------------------- component ------------------- 

function attach(object)
	-- create a state for each triggered sound
	
	local ws= world_state_get()
	local sm= ws.sound_manager
	local iterator= config.trigger_definitions.iterator_get()
	local index= 0
	while (true) do
		local trigger_def= iterator.next()
		if (trigger_def) then

			local s= var.trigger_states.create()
			if (s) then
				s.triggered= false
				s.definition_index= index
			end

			index = index+1
		else
			break
		end
	end
	
	-- begin continuous sound
	local continuous_iterator= config.continuous_definitions.iterator_get()
	index= 0
	while (true) do
		local cdef= continuous_iterator.next()
		if (cdef) then
			local sound= sm.get_sound_by_name(cdef.name);
			if (sound) then
			
				local state= var.continuous_states.create()
				if (state) then
					state.definition_index= index
				end
			end
			
			index= index+1
		else
			break
		end
	end
end

function start_continuous_sounds()
	local c_iterator= var.continuous_states.iterator_get()
	local ws= world_state_get()
	local sm= ws.sound_manager

	while (true) do
		local cstate= c_iterator.next()
		if (cstate) then
			local definition= config.continuous_definitions[cstate.definition_index]
			local sound= sm.get_sound_by_name(definition.name)
			if (sound) then
				sound.set_loop(true)
				sound.play(k_game_time_mode)
				sound.set_volume(0)
			end
		else
			break
		end
	end
end

function update(object)	
	local ws= world_state_get()
	local time= ws.cinematic_time.time
	local sm= ws.sound_manager
	
	if (not var.started) then
		--start the continuous sounds
		start_continuous_sounds()
		var.started= true
	end

	-- loop through states
	local state_iterator= var.trigger_states.iterator_get()
	while (true) do
		local state= state_iterator.next()
		
		if (state) then
			if (not state.triggered) then
			
				local definition= config.trigger_definitions[state.definition_index]
				if (time>=definition.trigger_time) then
					-- play the sound
					local sound= sm.get_sound_by_name(definition.name)
					if (sound) then
						sound.play(k_game_time_mode)
						state.triggered= true
					end
				end
			end
		else
			break
		end
	end
	
	-- loop through continuous sounds
	local c_iterator= var.continuous_states.iterator_get()
	while (true) do
		local cstate= c_iterator.next()
		if (cstate) then
			local definition= config.continuous_definitions[cstate.definition_index]
			local func= definition.volume_func
			if (func.valid()) then
				local sound= sm.get_sound_by_name(definition.name)
				if (sound) then
					local val= definition.volume_func.evaluate(time)
					sound.set_volume(val)
				end
			end
		else
			break
		end
	end

end

component_register(attach, update, nil, nil)
