:NAME shimmer

:PARAM uint16					unused

:CONFIG string					placement_name
:CONFIG string					animation_name
:CONFIG boolean					use_cinematic_time
:CONFIG float32					cinematic_offset_time
:CONFIG uint16					ring_count
:CONFIG float32					ring_delay

:VAR c_object_placement*		placement
:VAR float32					dt
:VAR int16						ring_index



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

function param_initialize(p)
end

function config_initialize(c)
	c.placement_name=""
	c.use_cinematic_time= false
	c.cinematic_offset_time= 0
end

function var_initialize(v)
	v.placement= nil
	v.dt= 0
	v.ring_index= -1
end


function attach(object)

	local world= world_get()
	local ws= world_state_get()
	local placement= world.placement_get(config.placement_name)
	
	var.placement= placement
	
	if (placement) then
		if (config.use_cinematic_time) then
			for i=0, config.ring_count-1 do
				local ring= ws.place(placement)
				if (ring) then
					local tween= ring.tween_get()
					if (tween) then
						tween.runtime_cinematic_offset = config.cinematic_offset_time + i*config.ring_delay
						if (config.animation_name) then
							tween.play_animation(config.animation_name)
						end
					end
				end
			end
			
			-- and we're done
			sleep()
		else
		
			-- place the first guy
			local ring= ws.place(placement)
			if (ring and config.animation_name) then
				ring.tween_animation_play(config.animation_name)
			end
			
			var.ring_index=0
			
			--if (config.ring_count<=1) then
			--	sleep()
			--end
		end
	else
		print("Shimmer: no placement found named "..config.placement_name)
		sleep()
	end
end

function update(object)

	local ws= world_state_get()
	var.dt = var.dt + object.object_dt_get()
	
	local desired_ring_index= math.floor(var.dt/config.ring_delay)
	--print("desired ring: "..desired_ring_index)
	if (desired_ring_index ~= var.ring_index) then
		if (var.placement) then
			local ring= ws.place(var.placement)
			if (ring and config.animation_name) then
				--print("play animation")
				ring.tween_animation_play(config.animation_name)
			end
		end
		var.ring_index= desired_ring_index;
		--if ((desired_ring_index+1) >= config.ring_count) then
			-- the end!
		--	sleep()
		--end
	end

end


component_register(attach, update, nil, nil)
