:NAME brain_ping

:PARAM float32	radius
:PARAM float32	speed
:PARAM float32	delay
:PARAM s_ping_effect@	ping_effect
:PARAM s_ping_effect@	bounce_effect


:VAR float32	last_ping_time
:VAR boolean	played_bounce
:VAR s_vector2d ping_point
:VAR c_sound*	ping_sound

-- -------------------- CONSTANTS --------------------

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

k_ping_color= {}
--k_ping_color.red= 1
--k_ping_color.green= 0.8
--k_ping_color.blue= 0.3
k_ping_color.red= 1
k_ping_color.green= 0.80
k_ping_color.blue= 0.0

local vectorlib = require "scripts/vector_math"


-- -------------------- INITIALIZATION --------------------

function param_initialize(p)
	p.radius= 5
	p.speed= 5
	p.delay= 4
	p.ping_effect= nil
	p.bounce_effect= nil
end

function var_initialize(v)
	v.last_ping_time= -4
	v.played_bounce= false
	v.ping_point.x= 0
	v.ping_point.y= 0
	v.ping_sound= nil
end


-- -------------------- COMPONENT --------------------

function attach(object)
	local sound= object.sound_get()	
	if (sound) then
		var.ping_sound= sound.get_sound_by_name("ping")
	end
end


function update(object)

	local ws= world_state_get()
	local time= ws.time.time
	
	local ping_duration= param.radius/param.speed
	local elapsed= time - var.last_ping_time
	
	if (elapsed < ping_duration) then
		local current_radius= elapsed*param.speed
		--local point= object.world_position_get()
		local point= var.ping_point
		
		
		-- debugging
		--render_circle(object.plate, point, current_radius, k_ping_color, false)
		
		local iterator= ws.component_cache.component_target.iterator_get()
		while (true) do
			local target_component= iterator.next()
			if (target_component) then
				local target= target_component.target_handle
				if (target and target.is_targetable()) then
					local omap= target.occupancy_map
					if (omap) then
						omap.clear_p_in_radius(point.x, point.y, current_radius);
					end
					
					local target_object= target.target_object
					if (target_object and not target_object.is_hidden()) then
						local real_location= target.target_object.world_position_get()
						if (vectorlib.distance2d(real_location, point) <= (current_radius+0.1)) then
							target.make_sound(1, true)
							
							if (not var.played_bounce and param.bounce_effect) then

								local distance_map= object.distance_map_get()
								if (distance_map) then
									local point= target_object.world_position_get()
									distance_map.compute_at_point(point.x, point.y)
									distance_map.distance_map.ping(param.bounce_effect)
									var.played_bounce= true
								end
							end
						end
					end
				end
			else
				break
			end
		end
		
	elseif (elapsed > (ping_duration + param.delay)) then
		local point= object.world_position_get()
		var.last_ping_time= time
		var.played_bounce= false
		
		var.ping_point.x= point.x
		var.ping_point.y= point.y
		
		if (var.ping_sound) then
			var.ping_sound.play(k_game_time_mode)
		end
				
		-- do the ping effect
		if (param.ping_effect) then
			local distance_map= object.distance_map_get()
			if (distance_map) then
				distance_map.compute_at_point(point.x, point.y)
				distance_map.distance_map.ping(param.ping_effect)
			end
		end
	end
end



component_register(attach, update, nil, nil)
