--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Эффект конца игры
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
local finaleffect_config = {}

-- НАСТРОЙКИ
finaleffect_config.lifetime = 1500	-- время существования звездочки
finaleffect_config.emit_timeout = 120	-- скорость вылетания звездочек
finaleffect_config.max_emits = 15		-- максимальное количество звездочек

finaleffect_config.image = "image_game_bonuses_sparkle_small"	-- картинка
finaleffect_config.size = CFloatVector(25,25)	-- размер
finaleffect_config.speed = 6			-- скорость движения
finaleffect_config.speed_delta = 1		-- отклонения скорости движения
finaleffect_config.rotation_speed = 360		-- скорость вращения (в градусах)
finaleffect_config.color = CColor(255, 255, 255)	-- цвет

finaleffect_config.base_color = CColor(128, 128, 128)
finaleffect_config.delta_color = CColor(127, 127, 127)

finaleffect_config.min_scale = 2.5 -- уменьшение частицы
finaleffect_config.paused = false -- пауза

finaleffect_config.gravity = true		-- гравитация?
finaleffect_config.gravity_acc = 100	-- ускорение гравитации

finaleffect_config.coords = -- список координат из которых вылетают искры
		{ {x=290, y=202},  {x=304, y=216}, {x=313, y=232}, {x=346, y=246}, {x=374, y=255},
		  {x=391, y=262},  {x=414, y=261}, {x=438, y=267}, {x=465, y=258}, {x=492, y=249},
		  {x=522, y=231},  {x=530, y=213}, {x=549, y=196}, {x=557, y=183} }

local x_coord, y_coord = 420,115
finaleffect_config.sun_coord = CFloatVector(x_coord, y_coord)
finaleffect_config.sun_image = "image_game_scenes_sun"

finaleffect_config.sun_glow_coord = CFloatVector(x_coord, y_coord)
finaleffect_config.sun_glow_image = "image_game_scenes_sun_glow"

finaleffect_config.ankh_coord = CFloatVector(x_coord, y_coord)
finaleffect_config.ankh_image = "image_game_scenes_ankh"

finaleffect_config.sun_min_brightness = 90	-- мин. яркость свечения солнца
finaleffect_config.sun_max_brightness = 250	-- макс. яркость свечения солнца
finaleffect_config.sun_brightness_period=4000	-- период изменения яркости свечения солнца

finaleffect_config.ankh_min_brightness = 10	-- мин. яркость анкха
finaleffect_config.ankh_max_brightness = 100	-- макс. яркость анкха
finaleffect_config.ankh_brightness_period=2000	-- период изменения яркости	анкха

finaleffect_config.image_min_brightness = 0	-- мин. яркость картинки
finaleffect_config.image_max_brightness = 45	-- макс. яркость картинки
finaleffect_config.image_brightness_period=4000	-- период изменения яркости картинки

finaleffect_config.big_image = "image_game_scenes_10_good01"

-- РЕАЛИЗАЦИЯ

function CreateFinalGameEffect(window)
	finaleffect_config.paused = false

	local count = table.getn(finaleffect_config.coords)
	while count>0 do
		ArmRainEffect(window, "arm"..count, 
			CFloatVector(finaleffect_config.coords[count].x, finaleffect_config.coords[count].y), finaleffect_config.color)
		count = count - 1
	end
	
	FinalSunEffect(window)
end

function FinalSunEffect(window)
	local sun_glow = CParticleNode()
	sun_glow:SetEternal(true)
	sun_glow:SetImage(finaleffect_config.sun_glow_image)

	sun_glow:SetColor(CColor(0, 0, 0))

	sun_glow:SetPosition(finaleffect_config.sun_coord)
	
	local color = CColorAction(
		CColor(finaleffect_config.sun_max_brightness, 
			finaleffect_config.sun_max_brightness, finaleffect_config.sun_max_brightness),
		CColor(finaleffect_config.sun_min_brightness, 
			finaleffect_config.sun_min_brightness, finaleffect_config.sun_min_brightness),  
		finaleffect_config.sun_brightness_period)
	color:StartActionTime(finaleffect_config.sun_brightness_period)
	sun_glow:AddAction(color)

	local color = CColorAction( 
			CColor(0, 0, 0),
			CColor(finaleffect_config.sun_max_brightness,
				finaleffect_config.sun_max_brightness, finaleffect_config.sun_max_brightness), 0)
	color:StartActionTime(0)
	color:EndActionTime(finaleffect_config.sun_brightness_period)
	sun_glow:AddAction(color)


	local big_image = CParticleNode()
	big_image:SetEternal(true)
	big_image:SetImage(finaleffect_config.big_image)
	big_image:SetPosition(CFloatVector(421, 281))
	big_image:SetColor(CColor(finaleffect_config.image_min_brightness, finaleffect_config.image_min_brightness, finaleffect_config.image_min_brightness))
	
	local color_big = CColorAction(CColor(finaleffect_config.image_min_brightness, finaleffect_config.image_min_brightness, finaleffect_config.image_min_brightness), 
		CColor(finaleffect_config.image_max_brightness, finaleffect_config.image_max_brightness, finaleffect_config.image_max_brightness), 
		finaleffect_config.image_brightness_period)
	big_image:AddAction(color_big)

	local ankh = CParticleNode()
	ankh:SetEternal(true)
	ankh:SetImage(finaleffect_config.ankh_image)

	ankh:SetColor(CColor(finaleffect_config.ankh_min_brightness, finaleffect_config.ankh_min_brightness, finaleffect_config.ankh_min_brightness))

	ankh:SetPosition(finaleffect_config.ankh_coord)
	
	local color2 = CColorAction(CColor(finaleffect_config.ankh_min_brightness, finaleffect_config.ankh_min_brightness, finaleffect_config.ankh_min_brightness), 
		CColor(finaleffect_config.ankh_max_brightness, finaleffect_config.ankh_max_brightness, finaleffect_config.ankh_max_brightness), 
		finaleffect_config.ankh_brightness_period)
	ankh:AddAction(color2)


	local sun = CParticleNode()
	sun:SetEternal(true)
	sun:SetImage(finaleffect_config.sun_image)
	sun:SetPosition(finaleffect_config.sun_coord)
	sun:SetDrawMode(false)
	
	local w = CParticleWidget("sun_widget", 0)
	w:AddParticleNode(big_image)
	w:AddParticleNode(sun)
	w:AddParticleNode(sun_glow)
	w:AddParticleNode(ankh)
	w:Start()
	window:AddWidget(w)
end

function ArmRainEffect(hostwidget, effectid, coord, color)
	local nodeemitter = CLuaEmitter(g_Lua, "armrain_emitter", finaleffect_config.emit_timeout)
	local pnode = CParticleNode(nodeemitter, true)
	
	log("effect")
	log(effectid)
	
	pnode:SetEternal(true)
	pnode:SetPosition(coord)
	pnode:SetColor(CColor(color.mRed, color.mGreen, color.mBlue, 0))
	
	local widget = CParticleWidget(effectid, hostwidget:GetLayer()+6)
	widget:AddParticleNode(pnode)
	widget:Start()
	hostwidget:AddWidget(widget)
end

function armrain_emitter(pnode)
	log(finaleffect_config.paused)

	if finaleffect_config.paused or pnode:GetParticleNumber() > finaleffect_config.max_emits then return end
	
	local color = CColor(
						finaleffect_config.base_color.mRed - finaleffect_config.delta_color.mRed/2 + math.random(finaleffect_config.delta_color.mRed),
						finaleffect_config.base_color.mGreen - finaleffect_config.delta_color.mGreen/2 + math.random(finaleffect_config.delta_color.mGreen),
						finaleffect_config.base_color.mBlue - finaleffect_config.delta_color.mBlue/2 + math.random(finaleffect_config.delta_color.mBlue)
						)	
	
	local p = CParticleNode()
	p:SetLifetime(finaleffect_config.lifetime)
	p:SetPosition(pnode:GetPosition())
	p:SetColor(color)
	p:SetImage(finaleffect_config.image)
	p:SetSize(finaleffect_config.size)
	
	angle = (6.283)*(math.random())

	local uf = CFadeAction(0, 255)
	uf:StartActionTime(0)
	uf:EndActionTime(50)
	p:AddAction(uf)
	
	local df = CFadeAction(255, 0)
	df:StartActionTime(50)
	df:EndActionTime(finaleffect_config.lifetime)
	p:AddAction(df)

	local rot = finaleffect_config.rotation_speed * 3.1415 / 180
	if math.random(2)==1 then rot = -rot end
	local r = CRotateAction(rot)
	p:AddAction(r)
		
	local move = CMoveAction()
	move:StartActionTime(0)
	move:EndActionTime(finaleffect_config.lifetime)
	p:AddAction(move)		

	local s = CScaleAction(1, finaleffect_config.min_scale)
	s:StartActionTime(0)
	s:EndActionTime(finaleffect_config.lifetime)
	p:AddAction(s)

	local dx = math.cos(angle)-math.sin(angle)
	local dy = math.sin(angle)+math.cos(angle)

	local speed = finaleffect_config.speed + math.random(finaleffect_config.speed_delta*2) - finaleffect_config.speed_delta
		
	p:SetSpeed(CFloatVector(speed * dx, speed * dy))
	if finaleffect_config.gravity then p:SetAccel(CFloatVector(0, finaleffect_config.gravity_acc)) end
	
	pnode:InsertParticle(p)
end
