class 'RavenFireMg' (RavenMg)

function RavenFireMg:__init(name)
    RavenMg.__init(self, name) 
    AddUniqueGameState('RavenElementFound', 'FIRE')
    
    self:playSound('fire_fon', true)
    
 
    
    --						6		7
    --					5				8
    --				4						9
    --			3								10
    --		2										11
    --	1												12
    
    self.numCurrentRow = 0
    self.countCells = 28
    self.counterDT = 0
    self.counterRemoveItems = 0
    self.countRows = 12
    self.countMaxItem = 0
    self.timeMoveRow = 0.3
    self.canShooting = true
    self.cursorIsVisibleMG = true
    self.positionPlaces = {}
    for i = 1, self.countCells do
			self.positionPlaces[i] = {self:getEntityNamed('Item_'..i):getX(), self:getEntityNamed('Item_'..i):getY()}
			self:getEntityNamed('Item_'..i):setRespondable(false)
    end
    self.structureOfRows = {
								{16, 23}, -- 1 row
								{11, 17, 24}, -- 2 row
								{7, 12, 18, 25}, -- 3 row
								{4, 8, 13, 19, 26}, -- 4 row
								{2, 5, 9, 14, 20, 27}, -- 5 row
								{1, 3, 6, 10, 15, 21, 28}, -- 6 row
								{1, 2, 4, 7, 11, 16, 22}, -- 7 row
								{3, 5, 8, 12, 17, 23}, -- 8 row
								{6, 9, 13, 18, 24}, -- 9 row
								{10, 14, 19, 25}, -- 10 row
								{15, 20, 26}, -- 11 row
								{21, 27} -- 12 row								
							}
							
	self.horizontalRows = {
								{{6,1}}, -- 1 row
								{{5,1}, {6,2}}, -- 2 row
								{{4,1}, {5,2}, {6,3}}, -- 3 row
								{{3,1}, {4,2}, {5,3}, {6,4}}, -- 4 row
								{{2,1}, {3,2}, {4,3}, {5,4}, {6,5}}, -- 5 row
								{{1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}} -- 6 row														
							}
	
							
	self.dependenceOfRows = {
								{{7,6}}, -- 1 row -- if changed 1 row then its first element changed in 7 row in 6 place
								{{7,5}, {8,5}}, -- 2 row
								{{7,4}, {8,4}, {9,4}}, -- 3 row
								{{7,3}, {8,3}, {9,3}, {10,3}}, -- 4 row
								{{7,2}, {8,2}, {9,2}, {10,2}, {11,2}}, -- 5 row
								{{7,1}, {8,1}, {9,1}, {10,1}, {11,1}, {12,1}}, -- 6 row
								{{6,1}, {5,1}, {4,1}, {3,1}, {2,1}, {1,1}}, -- 7 row
								{{6,2}, {5,2}, {4,2}, {3,2}, {2,2}}, -- 8 row
								{{6,3}, {5,3}, {4,3}, {3,3}}, -- 9 row
								{{6,4}, {5,4}, {4,4}}, -- 10 row
								{{6,5}, {5,5}}, -- 11 row
								{{6,6}} -- 12 row								
							}
	
	
	-- save 
	self.save_state_mg = 'RavenFireMg_SaveLoad'
    self.arrayForSaveMG = {}	
    self.arrayForSaveMG.countCurrentItems = 21
    self.arrayForSaveMG.OrderCurrentItems = 10
    self.arrayForSaveMG.TypeItem = { -- 1 - num item, 2 - num type
										{1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, 
										{7, 2}, {8, 2}, {9, 2}, {10, 2}, {11, 2}, {12, 2}, {13, 2}, {14, 2}, {15, 2},
										{16, 3}, {17, 3}, {18, 3}, {19, 3}, {20, 3}, {21, 3}									
									}
    self.arrayForSaveMG.PlaceItem = {
										{5, 0}, -- 1 row
										{11, 20, 0}, -- 2 row
										{9, 4, 21, 0}, -- 3 row
										{16, 18, 12, 6, 0}, -- 4 row
										{7, 17, 10, 19, 14, 0}, -- 5 row
										{1, 8, 2, 3, 13, 15, 0}, -- 6 row
										{1, 7, 16, 9, 11, 5, 0}, -- 7 row
										{8, 17, 18, 4, 20, 0}, -- 8 row
										{2, 10, 12, 21, 0}, -- 9 row
										{3, 19, 6, 0}, -- 10 row
										{13, 14, 0}, -- 11 row
										{15, 0} -- 12 row
									}	
									
	 -- restore
	if HasGameState('RAVEN_FIRE_COMPLETED') then
        RemoveGameState('RAVEN_FIRE_COMPLETED','RAVEN_FIRE_COMPLETED')        
    else
        if HasGameState(self.save_state_mg) then
			self.arrayForSaveMG = Helpers.RestoreObjectFromGameState(self.save_state_mg)
			if #self.arrayForSaveMG.TypeItem == 0 then
			    self:SkipMiniGame()
			    return
			end
		end
    end
									
	self.objects = {}
    for i = 1, #self.arrayForSaveMG.TypeItem do 			
			if  not self:hasEntityNamed('item_'..self.arrayForSaveMG.TypeItem[i][1]) then					
					 local entity = CreateSpriteEntity('Data/Textures/Screens/RavenFireMg/Item_'..self.arrayForSaveMG.TypeItem[i][2]..'.png')
					 entity:setAnimationSize(84, 84)
					 entity:setHotSpot(42, 42)
					 entity:attachTo( self:getMainEntity() )
					 entity:setName('item_' .. self.arrayForSaveMG.TypeItem[i][1])
					 self:getEntityNamed('item_' .. self.arrayForSaveMG.TypeItem[i][1]):setRespondable(false)
				 
					self.objects[self.arrayForSaveMG.TypeItem[i][1]] = {}
					self.objects[self.arrayForSaveMG.TypeItem[i][1]].entity = self:getEntityNamed('item_'..self.arrayForSaveMG.TypeItem[i][1])
					self.objects[self.arrayForSaveMG.TypeItem[i][1]].entity:setOrder(self.arrayForSaveMG.OrderCurrentItems)
					self.objects[self.arrayForSaveMG.TypeItem[i][1]].type = self.arrayForSaveMG.TypeItem[i][2]
					self.arrayForSaveMG.OrderCurrentItems = self.arrayForSaveMG.OrderCurrentItems + 1
			end
    end		

	for i = 1, #self.arrayForSaveMG.PlaceItem do 
			for j = 1, #self.arrayForSaveMG.PlaceItem[i] do 
					if self.arrayForSaveMG.PlaceItem[i][j] ~= 0 then
							self.objects[self.arrayForSaveMG.PlaceItem[i][j]].entity:setXY(self.positionPlaces[self.structureOfRows[i][j]][1], self.positionPlaces[self.structureOfRows[i][j]][2])
					end
			end	
	end	
	
	self.currentObject = self.arrayForSaveMG.TypeItem[math.random(1, #self.arrayForSaveMG.TypeItem)][2]
	self:getEntityNamed('Item_light'):setVisible(false)
	
	 self.arrayForSaveMG.countCurrentItems = self.arrayForSaveMG.countCurrentItems+1
	 local entity = CreateSpriteEntity('Data/Textures/Screens/RavenFireMg/Item_'..self.currentObject..'.png')
	 entity:setAnimationSize(84, 84)
	 entity:setHotSpot(42, 42)
	 entity:attachTo( self:getMainEntity() )
	 entity:setName('item_' .. self.arrayForSaveMG.countCurrentItems)
	 self:getEntityNamed('item_' .. self.arrayForSaveMG.countCurrentItems):setRespondable(false)
	 self.objects[self.arrayForSaveMG.countCurrentItems] = {}
	 self.objects[self.arrayForSaveMG.countCurrentItems].entity = self:getEntityNamed('item_'..self.arrayForSaveMG.countCurrentItems)
	 self.objects[self.arrayForSaveMG.countCurrentItems].type = self.currentObject
	 self.objects[self.arrayForSaveMG.countCurrentItems].entity:setXY(self:getEntityNamed('Shooting'):getX(), self:getEntityNamed('Shooting'):getY())
	 self.objects[self.arrayForSaveMG.countCurrentItems].entity:setOrder(self.arrayForSaveMG.OrderCurrentItems)
   
    self.timer:add(function()
        self:getEntityNamed('area_return').onClickBegin:add(function()
                                                                self:entityHide('area_return')
                                                                self:onScreenDestroy()
                                                            end)
        self:getEntityNamed('area_return'):setCursorOverName('CursorDown') 
    end, 1)
    
    self:getEntityNamed('Shooting').onUpdate:add(bind(self.shooterOnUpdate, self))
    
    self.timer:add(function()
        self:getEntityNamed('area_hide_cursor_mg'):setCursorOverName('CursorEmpty')   
	    self:getEntityNamed('area_hide_cursor_mg').onClickBegin:add(bind(self.arrowOnClick, self));
	end, 1)
   
end

---------------------------------------------------- extra functions

function RavenFireMg:CheckTheSameItemsInRows(time)

	for i = 3, #self.horizontalRows do 
		local num_the_same_items_final = {}
		local num_the_same_items_place = {}
		local num_the_same_items_final_place = {}
		local num_the_same_items = {}
		local curr_type = 0
		for j = 1, #self.horizontalRows[i] do
					if self.arrayForSaveMG.PlaceItem[self.horizontalRows[i][j][1]][self.horizontalRows[i][j][2]] ~= 0 then -- если есть итем, заполненная ячейка
							if curr_type == 0 then -- если это первый из проверяемых типов итемов сначала или после пустой ячейки
								curr_type = self.objects[self.arrayForSaveMG.PlaceItem[self.horizontalRows[i][j][1]][self.horizontalRows[i][j][2]]].type
								table.insert(num_the_same_items, self.arrayForSaveMG.PlaceItem[self.horizontalRows[i][j][1]][self.horizontalRows[i][j][2]])
								table.insert(num_the_same_items_place, {self.horizontalRows[i][j][1], self.horizontalRows[i][j][2]})
							else	-- проверяем тип текущего итема с предыдущим
								if self.objects[self.arrayForSaveMG.PlaceItem[self.horizontalRows[i][j][1]][self.horizontalRows[i][j][2]]].type == curr_type then -- типы совпали
									table.insert(num_the_same_items, self.arrayForSaveMG.PlaceItem[self.horizontalRows[i][j][1]][self.horizontalRows[i][j][2]])
									table.insert(num_the_same_items_place, {self.horizontalRows[i][j][1], self.horizontalRows[i][j][2]})
									-- проверяем, сколько уже есть в таблице num_the_same_items рядом стоящих итемов с одинаковыми типами
									if #num_the_same_items >= 3 then
											num_the_same_items_final = {}
											num_the_same_items_final_place = {}
											for i = 1, #num_the_same_items do 
												num_the_same_items_final[i] = num_the_same_items[i]												
												num_the_same_items_final_place[i] = {}							
												num_the_same_items_final_place[i][1] = num_the_same_items_place[i][1]												
												num_the_same_items_final_place[i][2] = num_the_same_items_place[i][2]												
											end
									end
								else	-- типы не совпали
										curr_type = 0
										num_the_same_items = {}
										num_the_same_items_place = {}
										curr_type = self.objects[self.arrayForSaveMG.PlaceItem[self.horizontalRows[i][j][1]][self.horizontalRows[i][j][2]]].type
										table.insert(num_the_same_items, self.arrayForSaveMG.PlaceItem[self.horizontalRows[i][j][1]][self.horizontalRows[i][j][2]])
										table.insert(num_the_same_items_place, {self.horizontalRows[i][j][1], self.horizontalRows[i][j][2]})
								end
								
							end
					else	-- если нет итема, пустая ячейка
							curr_type = 0
							num_the_same_items = {}
							num_the_same_items_place = {}
					end
					
		end
				
		if #num_the_same_items_final ~= 0 then
			-- create sequence for disappear items
			local z = 200
			for i = 1, #num_the_same_items_final do 
					if not self:hasEntityNamed('disappear_item_'..i) then
							local entity = CreateSpriteEntity('Data/Textures/Screens/RavenFireMg/Disappear_item.png')
							entity:setAnimationSize(78, 78)
							entity:setHotSpot(39, 39)
							entity:setFramesCount(29)
							entity:setFPS(24)
							entity:setBlend(3)
							entity:setOrder(z)
							entity:setMode(_ANIM_NOLOOP)        
							entity:attachTo( self:getMainEntity() )
							entity:setName('disappear_item_' .. i)
							self:getEntityNamed('disappear_item_' .. i):setRespondable(false)
							z = z + 1
					end						
					
					
					for j = 1, #self.arrayForSaveMG.TypeItem do 
						if self.arrayForSaveMG.TypeItem[j][1] == num_the_same_items_final[i] then
							table.remove(self.arrayForSaveMG.TypeItem, j)
							break
						end
					end
					
					for j = 1, #self.arrayForSaveMG.PlaceItem do 
							for k = 1, #self.arrayForSaveMG.PlaceItem[j] do 
										if self.arrayForSaveMG.PlaceItem[j][k] == num_the_same_items_final[i] then
											self.arrayForSaveMG.PlaceItem[j][k] = 0												
										end
							end
					end
					
					-- disappear item
					self:getEntityNamed('item_'..num_the_same_items_final[i]):ChangeOpacity(1, 0, 1, time)
					
					self.timer:add(function()	
										self:getEntityNamed('disappear_item_'..i):setXY(self.positionPlaces[self.structureOfRows[num_the_same_items_final_place[i][1]][num_the_same_items_final_place[i][2]]][1],
																						self.positionPlaces[self.structureOfRows[num_the_same_items_final_place[i][1]][num_the_same_items_final_place[i][2]]][2])
										self:getEntityNamed('disappear_item_'..i):setFrame(0)
										self:getEntityNamed('disappear_item_'..i):play()
										self:stopSound('fire_disappearance_balls')
										self:playSound('fire_disappearance_balls')
									end, time)
			end
 
			self.counterRemoveItems = self.counterRemoveItems + 1
			self:CheckTheSameItemsInRows(1.2)
			
			break
		end				
		
				
	end
	
end

function RavenFireMg:CheckTheSameItems(time)

	for i = 3, #self.arrayForSaveMG.PlaceItem - 2 do 
			local num_the_same_items_final = {}
			local num_the_same_items_place = {}
			local num_the_same_items_final_place = {}
			local num_the_same_items = {}
			local curr_type = 0
			for j = 1, #self.arrayForSaveMG.PlaceItem[i] do 
					if self.arrayForSaveMG.PlaceItem[i][j] ~= 0 then -- если есть итем, заполненная ячейка
							if curr_type == 0 then -- если это первый из проверяемых типов итемов сначала или после пустой ячейки
								curr_type = self.objects[self.arrayForSaveMG.PlaceItem[i][j]].type
								table.insert(num_the_same_items, self.arrayForSaveMG.PlaceItem[i][j])
								table.insert(num_the_same_items_place, {i,j})
							else	-- проверяем тип текущего итема с предыдущим
								if self.objects[self.arrayForSaveMG.PlaceItem[i][j]].type == curr_type then -- типы совпали
									table.insert(num_the_same_items, self.arrayForSaveMG.PlaceItem[i][j])
									table.insert(num_the_same_items_place, {i,j})
									-- проверяем, сколько уже есть в таблице num_the_same_items рядом стоящих итемов с одинаковыми типами
									if #num_the_same_items >= 3 then
											num_the_same_items_final = {}
											num_the_same_items_final_place = {}
											for i = 1, #num_the_same_items do 
												num_the_same_items_final[i] = num_the_same_items[i]												
												num_the_same_items_final_place[i] = {}							
												num_the_same_items_final_place[i][1] = num_the_same_items_place[i][1]												
												num_the_same_items_final_place[i][2] = num_the_same_items_place[i][2]												
											end
									end
								else	-- типы не совпали
										curr_type = 0
										num_the_same_items = {}
										num_the_same_items_place = {}
										curr_type = self.objects[self.arrayForSaveMG.PlaceItem[i][j]].type
										table.insert(num_the_same_items, self.arrayForSaveMG.PlaceItem[i][j])
										table.insert(num_the_same_items_place, {i,j})
								end
								
							end
					else	-- если нет итема, пустая ячейка
							curr_type = 0
							num_the_same_items = {}
							num_the_same_items_place = {}
					end
			end
			
						
			if #num_the_same_items_final ~= 0 then
				-- create sequence for disappear items
				local z = 200
				for i = 1, #num_the_same_items_final do 
						if not self:hasEntityNamed('disappear_item_'..i) then
								local entity = CreateSpriteEntity('Data/Textures/Screens/RavenFireMg/Disappear_item.png')
								entity:setAnimationSize(78, 78)
								entity:setHotSpot(39, 39)
								entity:setFramesCount(29)
								entity:setFPS(24)
								entity:setBlend(3)
								entity:setOrder(z)
								entity:setMode(_ANIM_NOLOOP)        
								entity:attachTo( self:getMainEntity() )
								entity:setName('disappear_item_' .. i)
								self:getEntityNamed('disappear_item_' .. i):setRespondable(false)
								z = z + 1
						end						
						
						
						for j = 1, #self.arrayForSaveMG.TypeItem do 
							if self.arrayForSaveMG.TypeItem[j][1] == num_the_same_items_final[i] then
								table.remove(self.arrayForSaveMG.TypeItem, j)
								break
							end
						end
						
						for j = 1, #self.arrayForSaveMG.PlaceItem do 
								for k = 1, #self.arrayForSaveMG.PlaceItem[j] do 
											if self.arrayForSaveMG.PlaceItem[j][k] == num_the_same_items_final[i] then
												self.arrayForSaveMG.PlaceItem[j][k] = 0												
											end
								end
						end
						
						-- disappear item
						self:getEntityNamed('item_'..num_the_same_items_final[i]):ChangeOpacity(1, 0, 1, time)
						
						self.timer:add(function()	
											self:getEntityNamed('disappear_item_'..i):setXY(self.positionPlaces[self.structureOfRows[num_the_same_items_final_place[i][1]][num_the_same_items_final_place[i][2]]][1],
																							self.positionPlaces[self.structureOfRows[num_the_same_items_final_place[i][1]][num_the_same_items_final_place[i][2]]][2])
											self:getEntityNamed('disappear_item_'..i):setFrame(0)
											self:getEntityNamed('disappear_item_'..i):play()
											self:stopSound('fire_disappearance_balls')
											self:playSound('fire_disappearance_balls')
										end, time)
				end
	 
				self.counterRemoveItems = self.counterRemoveItems + 1
				self:CheckTheSameItems(1.2)
				
				break
			end
	end
	
	
end

function RavenFireMg:SkipMiniGame()   
	self.skip_logic:getEntityNamed('bttn_skip'):setRespondable(false)
	
	self:entityHide('area_return')
    AddGameState('RAVEN_FIRE_COMPLETED', 'RAVEN_FIRE_COMPLETED')
    if HasGameState('RAVEN_FEATURE_PERCENTAGE_BATTERY_FIRE') then
        local percent = GetGameState('RAVEN_FEATURE_PERCENTAGE_BATTERY_FIRE'):get(0) or '0' 
        RemoveGameState('RAVEN_FEATURE_PERCENTAGE_BATTERY_FIRE', percent)
    end
    --
    AddGameState('RAVEN_FEATURE_PERCENTAGE_BATTERY_FIRE', '100')
    if HasGameState('RAVEN_FEATURE_COUNT_FIRE') then
        local count = GetGameState('RAVEN_FEATURE_COUNT_FIRE'):get(0) or '0' 
        RemoveGameState('RAVEN_FEATURE_COUNT_FIRE', count)
        count = string.upper(0)
        AddGameState('RAVEN_FEATURE_COUNT_FIRE', count)
    else
        count = string.upper(0)
        AddGameState('RAVEN_FEATURE_COUNT_FIRE', count)
    end
   
    for i = 1, #self.arrayForSaveMG.TypeItem do 
        self:getEntityNamed("item_"..self.arrayForSaveMG.TypeItem[i][1]):ChangeOpacity(self:getEntityNamed("item_"..self.arrayForSaveMG.TypeItem[i][1]):getOpacity(), 0, 0.5)
    end
   
	self:getEntityNamed('Item_light'):setVisible(false)
	
	self.arrayForSaveMG.TypeItem = {}
   
    self.timer:add(function()
							self:playSound('fire_final')
					end, 0.5)
	self.timer:add(function()
							self:onScreenDestroy()
					end,1.5)
   
end

---------------------------------------------------- base functions
function RavenFireMg:arrowOnClick(entity, state)

	if getHandledCursor():getPosition().y < 615  and self.canShooting and (not HasGameState('RAVEN_FIRE_COMPLETED')) then
			self.canShooting = false
			self:getEntityNamed('Item_light'):setVisible(false)
			self:stopSound('fire_move_ball')
			self:playSound('fire_move_ball')
			-- look for empty cell in current row
			local num_empty_cell
			for i = 1, #self.arrayForSaveMG.PlaceItem[self.numCurrentRow] do 
					if self.arrayForSaveMG.PlaceItem[self.numCurrentRow][i] == 0 then
							num_empty_cell = i
							break
					end
			end
			
			-- move all items from 1 cell to num_empty_cell-1 cell on step down
			-- var
			for i = num_empty_cell - 1, 1, -1 do 
					self.arrayForSaveMG.PlaceItem[self.numCurrentRow][i+1] = self.arrayForSaveMG.PlaceItem[self.numCurrentRow][i]
			end	
			self.arrayForSaveMG.PlaceItem[self.numCurrentRow][1] = self.arrayForSaveMG.countCurrentItems
			table.insert(self.arrayForSaveMG.TypeItem, {self.arrayForSaveMG.countCurrentItems, self.currentObject})
			-- move items
			for i = num_empty_cell, 1, -1 do 
				self.objects[self.arrayForSaveMG.PlaceItem[self.numCurrentRow][i]].entity:MoveTo(self.positionPlaces[self.structureOfRows[self.numCurrentRow][i]][1], self.positionPlaces[self.structureOfRows[self.numCurrentRow][i]][2], self.timeMoveRow)
			end		
			
			
			-- set 0 on last place
			if num_empty_cell == #self.arrayForSaveMG.PlaceItem[self.numCurrentRow] then
				local num = self.arrayForSaveMG.PlaceItem[self.numCurrentRow][num_empty_cell]
				self.objects[num].entity:ChangeOpacity(1, 0, self.timeMoveRow)
				self.objects[num].entity:ChangeScale(1, 0, self.timeMoveRow)
				self.arrayForSaveMG.PlaceItem[self.numCurrentRow][num_empty_cell] = 0
				-- del num item in table self.arrayForSaveMG.TypeItem
				local index_del
				for i = 1, #self.arrayForSaveMG.TypeItem do 
					if self.arrayForSaveMG.TypeItem[i][1] == num then
						index_del = i
						break
					end
				end
				table.remove(self.arrayForSaveMG.TypeItem, index_del)
				
				self.timer:add(function()
									self.objects[num].entity:destroy()									
								end, self.timeMoveRow)
			end
			 
								
			-- change other rows
			for i = 1, #self.dependenceOfRows[self.numCurrentRow] do 
				self.arrayForSaveMG.PlaceItem[self.dependenceOfRows[self.numCurrentRow][i][1]][self.dependenceOfRows[self.numCurrentRow][i][2]] = self.arrayForSaveMG.PlaceItem[self.numCurrentRow][i]
			end
			
			-- check the same items
			self:CheckTheSameItems(0.5)
			--
			local time_2 = 0.6
			if  self.counterRemoveItems > 0 then
				time_2 = self.counterRemoveItems*1.2 + 0.6				
			end
			self:CheckTheSameItemsInRows(time_2)
			if  self.counterRemoveItems > 0 then
				time_2 = time_2 + self.counterRemoveItems*1.2	
				self.counterRemoveItems = 0		
			end
			--
			
			-- next item
			 if #self.arrayForSaveMG.TypeItem ~= 0 then
					 self.arrayForSaveMG.countCurrentItems = self.arrayForSaveMG.countCurrentItems + 1 
					 self.arrayForSaveMG.OrderCurrentItems = self.arrayForSaveMG.OrderCurrentItems + 1
					 if self.arrayForSaveMG.OrderCurrentItems > 80 then
						self.arrayForSaveMG.OrderCurrentItems = 20
					 end
					 self.currentObject = self.arrayForSaveMG.TypeItem[math.random(1, #self.arrayForSaveMG.TypeItem)][2]
					 local entity = CreateSpriteEntity('Data/Textures/Screens/RavenFireMg/Item_'..self.currentObject..'.png')
					 entity:setAnimationSize(84, 84)
					 entity:setHotSpot(42, 42)
					 entity:attachTo( self:getMainEntity() )
					 entity:setName('item_' .. self.arrayForSaveMG.countCurrentItems)
					 self:getEntityNamed('item_' .. self.arrayForSaveMG.countCurrentItems):setRespondable(false)
					 
					 self.objects[self.arrayForSaveMG.countCurrentItems] = {}
					 self.objects[self.arrayForSaveMG.countCurrentItems].entity = self:getEntityNamed('item_'..self.arrayForSaveMG.countCurrentItems)
					 self.objects[self.arrayForSaveMG.countCurrentItems].type = self.currentObject
					 self.objects[self.arrayForSaveMG.countCurrentItems].entity:setXY(self:getEntityNamed('Shooting'):getX(), self:getEntityNamed('Shooting'):getY())
					 self.objects[self.arrayForSaveMG.countCurrentItems].entity:setOrder(self.arrayForSaveMG.OrderCurrentItems)
					 self.objects[self.arrayForSaveMG.countCurrentItems].entity:setOpacity(0)
					 self.objects[self.arrayForSaveMG.countCurrentItems].entity:ChangeOpacity(0, 1, self.timeMoveRow)
			else
			        self:entityHide('area_return')
					AddGameState('RAVEN_FIRE_COMPLETED', 'RAVEN_FIRE_COMPLETED')
                    if HasGameState('RAVEN_FEATURE_PERCENTAGE_BATTERY_FIRE') then
                        local percent = GetGameState('RAVEN_FEATURE_PERCENTAGE_BATTERY_FIRE'):get(0) or '0' 
                        RemoveGameState('RAVEN_FEATURE_PERCENTAGE_BATTERY_FIRE', percent)
                    end
                    --
                    AddGameState('RAVEN_FEATURE_PERCENTAGE_BATTERY_FIRE', '100')
                    if HasGameState('RAVEN_FEATURE_COUNT_FIRE') then
                        local count = GetGameState('RAVEN_FEATURE_COUNT_FIRE'):get(0) or '0' 
                        RemoveGameState('RAVEN_FEATURE_COUNT_FIRE', count)
                        count = string.upper(0)
                        AddGameState('RAVEN_FEATURE_COUNT_FIRE', count)
                    else
                        count = string.upper(0)
                        AddGameState('RAVEN_FEATURE_COUNT_FIRE', count)
                    end
										
					self.timer:add(function()
											self:playSound('fire_final')
									end, 0.5)
					self.timer:add(function()
											self:onScreenDestroy()
									end, 1.8)
			end
			
			--[[local time_2 = 0.6
			if  self.counterRemoveItems > 0 then
				time_2 = self.counterRemoveItems*1.2 + 0.6				
			end
			self:CheckTheSameItemsInRows(time_2)
			if  self.counterRemoveItems > 0 then
				time_2 = time_2 + self.counterRemoveItems*1.2	
				self.counterRemoveItems = 0		
			end--]]
			
			
			self.timer:add(function()											
								if #self.arrayForSaveMG.TypeItem ~= 0 then
										self:getEntityNamed('Item_light'):setVisible(true)
										self.canShooting = true
								end
							end, time_2)
								
			Helpers.SaveObjectToGameState(self.save_state_mg, self.arrayForSaveMG)
	end

end

function RavenFireMg:shooterOnUpdate(dt)

	if self.counterDT > 5 then
		if #self.arrayForSaveMG.TypeItem ~= 0 then				
				self.counterDT = 0
				-- look for nearest row for shooter
				local d_table = {}
				local x, y = getHandledCursor():getPosition().x, getHandledCursor():getPosition().y
				for i = 1, self.countRows do
					local d = math.sqrt( math.pow(x - self:getEntityNamed('Area_place_shooter_'..i):getX(), 2) + math.pow(y - self:getEntityNamed('Area_place_shooter_'..i):getY(), 2) )			
					d_table[i] = d
				end

				local min
				for i,v in pairs(d_table) do
					if not min then
						min = i
					elseif v < d_table[min] then
						min = i
					end
				end  
				
				local sign = 1
				if min < 7 then
					sign = -1
				end
				
				self:getEntityNamed('Shooting'):setXY(self:getEntityNamed('Area_place_shooter_'..min):getX(), self:getEntityNamed('Area_place_shooter_'..min):getY())
				self:getEntityNamed('Shooting_nakladka'):setXY(self:getEntityNamed('Area_place_shooter_'..min):getX(), self:getEntityNamed('Area_place_shooter_'..min):getY())
				self:getEntityNamed('button_pressed_mg'):setXY(self:getEntityNamed('Area_place_shooter_'..min):getX() + sign*48, self:getEntityNamed('Area_place_shooter_'..min):getY())
				self:getEntityNamed('button_default_mg'):setXY(self:getEntityNamed('Area_place_shooter_'..min):getX() + sign*48, self:getEntityNamed('Area_place_shooter_'..min):getY())
				self:getEntityNamed('button_light_mg'):setXY(self:getEntityNamed('Area_place_shooter_'..min):getX() + sign*50, self:getEntityNamed('Area_place_shooter_'..min):getY())
				self.objects[self.arrayForSaveMG.countCurrentItems].entity:setXY(self:getEntityNamed('Area_place_shooter_'..min):getX(), self:getEntityNamed('Area_place_shooter_'..min):getY())
				self:getEntityNamed('Shooting'):setScaleX(sign*1)
				self:getEntityNamed('Shooting_nakladka'):setScaleX(sign*1)
				self:getEntityNamed('button_pressed_mg'):setScaleX(sign*1)
				self:getEntityNamed('button_default_mg'):setScaleX(sign*1)
				self:getEntityNamed('button_light_mg'):setScaleX(sign*1)
				
				self:getEntityNamed('Item_light'):setXY(self.positionPlaces[self.structureOfRows[min][1]][1], self.positionPlaces[self.structureOfRows[min][1]][2])
				if #self.arrayForSaveMG.TypeItem ~= 0 and self.canShooting then
					self:getEntityNamed('Item_light'):setVisible(true)
				end
				
				self.numCurrentRow = min
		end
	else
		self.counterDT = self.counterDT + 1
	end
	

end

function RavenFireMg:onUpdate(dt)

	if self.canShooting then
			if getHandledCursor():getPosition().y < 615 and not HasGameState('RAVEN_FIRE_COMPLETED') then				
					self:getEntityNamed('button_light_mg'):setVisible(true)
					self:getEntityNamed('button_default_mg'):setVisible(false)
					self:getEntityNamed('button_pressed_mg'):setVisible(false)
			else
					self:getEntityNamed('button_light_mg'):setVisible(false)
					self:getEntityNamed('button_default_mg'):setVisible(true)
					self:getEntityNamed('button_pressed_mg'):setVisible(false)
					self:getEntityNamed('Item_light'):setVisible(false)
			end
	else
			self:getEntityNamed('button_light_mg'):setVisible(false)
			self:getEntityNamed('button_default_mg'):setVisible(false)
			self:getEntityNamed('button_pressed_mg'):setVisible(true)
	end
	
    
end

function RavenFireMg:onObjectMouseEnter(entity, state)
    
end

function RavenFireMg:onObjectMouseLeave(entity, state)
   
end

function RavenFireMg:onObjectMouseDown(entity, state)
   
end


--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function RavenFireMg:CreateLoaderTable()
	local objects =
	{
		{ name = 'Area_place_shooter_1',  entityType = 'SpriteEntity',  x = 231,  y = 389,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Area_place_shooter_2',  entityType = 'SpriteEntity',  x = 264,  y = 321,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Area_place_shooter_3',  entityType = 'SpriteEntity',  x = 298,  y = 251,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Area_place_shooter_4',  entityType = 'SpriteEntity',  x = 332,  y = 181,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Area_place_shooter_5',  entityType = 'SpriteEntity',  x = 372,  y = 112,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Area_place_shooter_6',  entityType = 'SpriteEntity',  x = 416,  y = 45,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Area_place_shooter_7',  entityType = 'SpriteEntity',  x = 600,  y = 40,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Area_place_shooter_8',  entityType = 'SpriteEntity',  x = 644,  y = 103,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Area_place_shooter_9',  entityType = 'SpriteEntity',  x = 688,  y = 168,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Area_place_shooter_10',  entityType = 'SpriteEntity',  x = 732,  y = 238,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Area_place_shooter_11',  entityType = 'SpriteEntity',  x = 763,  y = 305,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Area_place_shooter_12',  entityType = 'SpriteEntity',  x = 796,  y = 372,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Raven_fire_movie',  entityType = 'VideoEntity',  x = 511,  y = 307,  order = 0, scaleX = 1.342,  scaleY = 1.32, isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  path = 'Data/Video/RavenMgScreens/RavenFireMg/Raven_fire_movie.webm',  hasAlpha = true,  isLooped = true,  },
		{ name = 'area_return',  entityType = 'SpriteEntity',  x = 211,  y = 694,  order = 0,  scaleX = 170,  scaleY = 20,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Item_1',  entityType = 'SpriteEntity',  x = 512,  y = 166,  order = 51,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_2',  entityType = 'SpriteEntity',  x = 474,  y = 232,  order = 52,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_3',  entityType = 'SpriteEntity',  x = 550,  y = 231,  order = 53,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_4',  entityType = 'SpriteEntity',  x = 437,  y = 298,  order = 54,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_5',  entityType = 'SpriteEntity',  x = 513,  y = 298,  order = 55,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_6',  entityType = 'SpriteEntity',  x = 590,  y = 297,  order = 56,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_7',  entityType = 'SpriteEntity',  x = 398,  y = 364,  order = 57,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_8',  entityType = 'SpriteEntity',  x = 475,  y = 363,  order = 58,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_9',  entityType = 'SpriteEntity',  x = 550,  y = 363,  order = 59,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_10',  entityType = 'SpriteEntity',  x = 626,  y = 364,  order = 60,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_11',  entityType = 'SpriteEntity',  x = 360,  y = 431,  order = 61,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_12',  entityType = 'SpriteEntity',  x = 437,  y = 430,  order = 62,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_13',  entityType = 'SpriteEntity',  x = 512,  y = 430,  order = 63,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_14',  entityType = 'SpriteEntity',  x = 588,  y = 428,  order = 64,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_15',  entityType = 'SpriteEntity',  x = 666,  y = 430,  order = 65,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_16',  entityType = 'SpriteEntity',  x = 321,  y = 496,  order = 66,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_17',  entityType = 'SpriteEntity',  x = 397,  y = 497,  order = 67,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_18',  entityType = 'SpriteEntity',  x = 474,  y = 496,  order = 68,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_19',  entityType = 'SpriteEntity',  x = 551,  y = 495,  order = 69,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_20',  entityType = 'SpriteEntity',  x = 627,  y = 497,  order = 70,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_21',  entityType = 'SpriteEntity',  x = 703,  y = 495,  order = 71,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_22',  entityType = 'SpriteEntity',  x = 284,  y = 562,  order = 72,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_23',  entityType = 'SpriteEntity',  x = 360,  y = 562,  order = 73,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_24',  entityType = 'SpriteEntity',  x = 436,  y = 562,  order = 74,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_25',  entityType = 'SpriteEntity',  x = 512,  y = 562,  order = 75,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_26',  entityType = 'SpriteEntity',  x = 588,  y = 562,  order = 76,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_27',  entityType = 'SpriteEntity',  x = 664,  y = 563,  order = 77,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'Item_28',  entityType = 'SpriteEntity',  x = 741,  y = 562,  order = 78,  isVisible = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_1.png',  width = 84,  height = 84,  },
		{ name = 'area_hide_cursor_mg',  entityType = 'SpriteEntity',  x = 489,  y = 310,  order = 220,  scaleX = 200,  scaleY = 80,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Common/Area.png',  width = 8,  height = 8,  },
		{ name = 'Shooting',  entityType = 'SpriteEntity',  x = 111,  y = 124,  order = 15,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 82, y = 93},  texturePath = 'Data/Textures/Screens/RavenFireMg/Shooting.png',  width = 220,  height = 200,  },
		{ name = 'Shooting_nakladka',  entityType = 'SpriteEntity',  x = 111,  y = 124,  order = 98,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  hotSpot = {x = 11, y = 45},  texturePath = 'Data/Textures/Screens/RavenFireMg/Shooting_nakladka.png',  width = 98,  height = 94,  },
		{ name = 'button_pressed_mg',  entityType = 'SpriteEntity',  x = 159,  y = 128,  order = 100,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Screens/RavenFireMg/button_pressed_mg.png',  width = 80,  height = 80,  },
		{ name = 'button_default_mg',  entityType = 'SpriteEntity',  x = 159,  y = 128,  order = 101,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Screens/RavenFireMg/button_default_mg.png',  width = 80,  height = 80,  },
		{ name = 'button_light_mg',  entityType = 'SpriteEntity',  x = 161,  y = 128,  order = 102,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/button_light_mg.png',  width = 80,  height = 80,  },
		{ name = 'Shooting_light',  entityType = 'SpriteEntity',  parentName = 'button_light_mg',  x = -21,  y = 6,  order = 1,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Shooting_light.png',  width = 220,  height = 200,  },
		{ name = 'Item_light',  entityType = 'SpriteEntity',  x = 196,  y = 299,  order = 102,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/Item_light.png',  width = 84,  height = 84,  },
		{ name = 'bg',  entityType = 'SpriteEntity',  x = 512,  y = 349,  order = 200,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/RavenFireMg/bg.png',  width = 728,  height = 686,  },
		{ name = 'text_fire',  entityType = 'TextEntity',  x = 513,  y = 621,  order = 5205,  editingLocked = false,  treeItemIsExpanded = false,  localizationKey = 'BBT_FIRE_POWER',  font = 'Data/Fonts/cyrillic.fnt',  height = 23,  },
	}
	return objects
end
