shooting_scene08_1 = {}

shooting_scene08_1.params = 
{
	-- возможное количество выстрелов
	shoot_limit = 5,
	-- необходимое число попаданий
	target_hits = 3,
	-- id-шник партиклевой системы для попадания
	effect_hit = "EffectOnHit",
	-- id-шник партиклевой системы для промаха
	effect_miss = "EffectOnMiss",
	-- смещение предмета-цели при попадании по нему
	offset_by_hit = sf.misc.FloatVector(15.0, -15.0),
	-- скорость уменьшения содержимого "градусника"
	progress_decrease_speed = 0.4, -- 0.2 содержимого "градусника" исчезнет за 1с
	
	-- настройки землетрясения
	shake_time = 1000,		-- продолжительность одной тряски
	shake_delay_min = 50,	-- минимальная пауза между трясками
	shake_delay_max = 300,	-- максимальная пауза между трясками
	shake_radius = 10,
	shake_period = 100,
	shake_interval_x = 4,
	shake_interval_y = 4
}

function shooting_scene08_1.CheckEnd(_owner)
	if shooting_scene08_1.result then
		if shooting_scene08_1.result == 1 then
			sminigames.ImageDropBuffer(shooting_scene08_1.target.GetImage())			
			_owner.SetGameResult(shooting_scene08_1.result)
			_owner.OnEndGame()
		elseif not shooting_scene08_1.progress_decrease_effect:IsActive() then
			sminigames.ImageDropBuffer(shooting_scene08_1.target.GetImage())			
			minigame_window.CloseMinigame(0)
		end
	end
end

function shooting_scene08_1.OnUpdate(_owner)	
	
	if not shooting_scene08_1.sight then
		shooting_scene08_1.sight = __cast(_owner.GetParentWidget().GetWidget(_T("sight")), sf.gui.CImageWidget)
		assert(shooting_scene08_1.sight)
		shooting_scene08_1.sight.AddFlags(sf.gui.CBaseWidget.FlagHidden)
	end
	if not shooting_scene08_1.progress then
		shooting_scene08_1.progress = __cast(_owner.GetParentWidget().GetWidget(_T("progress")), sf.gui.CImageWidget)
		assert(shooting_scene08_1.progress)
	end
	if not shooting_scene08_1.shake_inited then
		shooting_scene08_1.shake_inited = true
		scene_effects.shake.Init(
			shooting_scene08_1.params.shake_time, 
			shooting_scene08_1.params.shake_delay_min, 
			shooting_scene08_1.params.shake_delay_max, 
			shooting_scene08_1.params.shake_radius, 
			shooting_scene08_1.params.shake_period, 
			shooting_scene08_1.params.shake_interval_x, 
			shooting_scene08_1.params.shake_interval_y, 
			_owner, _owner, false, true)
	end
	if not shooting_scene08_1.progress_decrease_effect then
		shooting_scene08_1.progress_decrease_effect = shooting_scene08_1.CProgressDecreaseEffect(shooting_scene08_1.progress.GetImage())
	end
	
	shooting_scene08_1.progress_decrease_effect:Update(shooting_scene08_1.timer.Get().GetTime())

	shooting_scene08_1.CheckEnd(_owner)
end

function shooting_scene08_1.CProgressDecreaseEffect(_progress)
	
	return
	{		
		floor_value = 1.0,
		
		_progress = _progress,
		_size = sf.misc.FloatVector(_progress.GetRect().Width, _progress.GetRect().Height),
		_current_value = 1.0,
		
		IsActive = function(_self)
			return _self._current_value ~= _self.floor_value
		end,

		Update = function(_self, _time)			
			if _self._current_value <= 0.0 then return end
			
			if _self._current_value > _self.floor_value then
				if not _self._last_time then _self._last_time = _time end
				local t = _time - _self._last_time
				local v = shooting_scene08_1.params.progress_decrease_speed * (t / 1000.0)
				_self._current_value = _self._current_value - v
				_self._last_time = _time
			end
			
			if _self._current_value <= _self.floor_value then
				_self._current_value = _self.floor_value
				_self._last_time = nil
			end
			
			if _self._current_value <= 0.0 then return end
						
			local height = _self._size.Y * _self._current_value
			local rect = sf.misc.FloatRect(0.0, _self._size.Y-height, _self._size.X, height)
			_self._progress.SetSourceRect(rect)
			_self._progress.SetRect(rect)
		end
	}	
end

function shooting_scene08_1.OnMouseDown(_owner, _pos, _button, _state, _broadcast)
	local target_pos = shooting_scene08_1.target.GetOffset()
	local p = _pos - target_pos
	if 	shooting_scene08_1.target.GetPoly().IsContains(_pos.X, _pos.Y) and 
		sminigames.IsVisiblePoint(shooting_scene08_1.target.GetImage(), p.X, p.Y) then
			shooting_scene08_1.OnHit(_owner, _pos)
	else
			shooting_scene08_1.OnMiss(_owner, _pos)
	end
	return false
end

function shooting_scene08_1.OnMouseMove(_owner, _pos, _state, _broadcast)
	if shooting_scene08_1.sight then
		sf.gui.g_Cursor.Instance().SetCustomCursor(shooting_scene08_1.sight.GetImage().GetTexture())
	end
	return false
end

function shooting_scene08_1.OnMouseUp(_owner, _pos, _button, _state, _broadcast)
	return false
end

function shooting_scene08_1.OnPreferredSize(_owner)
	return sminigames.OnPreferredSize(_owner)
end

function shooting_scene08_1.Init(_owner)	
	sminigames.SetStandartScriptName(_owner, shooting_scene08_1)
	
	shooting_scene08_1.sight = nil
	shooting_scene08_1.progress = nil
	shooting_scene08_1.shake_inited = nil
	shooting_scene08_1.progress_decrease_effect = nil
	
	shooting_scene08_1.target = __cast(_owner.GetWidget(_T("target")), sf.gui.CImageWidget)
	assert(shooting_scene08_1.target)
	
	shooting_scene08_1.window = _owner
	shooting_scene08_1.rotation = sf.misc.MatrixIdentity()
	
	shooting_scene08_1.timer = sf.core.CTimerWrapper(sf.core.g_Application.GetTimeManager().AttachTimer(_T(""), nil))	
	
	shooting_scene08_1.result = nil
	shooting_scene08_1.shoots = 0
	shooting_scene08_1.hits = 0
	
	shooting_scene08_1.shake_inited = nil
	
	shooting_effects.effects_list = visual_effects.CreateEffectsList()
end

function shooting_scene08_1.OnShoot(_owner)
	if shooting_scene08_1.shoots < shooting_scene08_1.params.shoot_limit then
		shooting_scene08_1.shoots = shooting_scene08_1.shoots + 1
		shooting_scene08_1.progress_decrease_effect.floor_value = 1.0 - (shooting_scene08_1.shoots / shooting_scene08_1.params.shoot_limit)
		return true
	end
	return false
end

function shooting_scene08_1.OnHit(_owner, _pos)
	if shooting_scene08_1.OnShoot(_owner) then
		shooting_scene08_1.hits = shooting_scene08_1.hits + 1
		local offset = sf.misc.FloatVector(shooting_scene08_1.target.GetOffset())
		offset = offset + shooting_scene08_1.params.offset_by_hit
		shooting_scene08_1.target.SetOffset(offset.X, offset.Y)
		shooting_effects.CreateEffectHit(_pos)
		if shooting_scene08_1.hits >= shooting_scene08_1.params.target_hits then
			shooting_scene08_1.result = 1
		elseif shooting_scene08_1.shoots >= shooting_scene08_1.params.shoot_limit then
			shooting_scene08_1.result = 0
		end
	end
end

function shooting_scene08_1.OnMiss(_owner, _pos)
	if shooting_scene08_1.OnShoot(_owner) then	
		shooting_effects.CreateEffectMiss(_pos)	
		if shooting_scene08_1.shoots >= shooting_scene08_1.params.shoot_limit then
			shooting_scene08_1.result = 0
		end
	end
end

shooting_effects = shooting_effects or {}

__inherite(shooting_effects, null_lua_widget_handler()) --default_lua_widget_handler

function shooting_effects.__CreateInstance()
    return "shooting_effects"
end

function shooting_effects.DoUpdate(_this)
	if shooting_effects.effects_list then
		shooting_effects.effects_list:Update()
	end
    return true
end

function shooting_effects.DoDraw(_this, _renderer)
	if shooting_effects.effects_list then
		shooting_effects.effects_list:PreDraw(_renderer)
		shooting_effects.effects_list:PostDraw(_renderer)
	end
    return true
end

function shooting_effects.CreateEffectMiss(_pos)
	shooting_effects.effects_list:CreateEffect(shooting_scene08_1.params.effect_miss, _pos)
end

function shooting_effects.CreateEffectHit(_pos)
	shooting_effects.effects_list:CreateEffect(shooting_scene08_1.params.effect_hit, _pos)
end

