class 'l_06_Town_Z_Gates' (ZoomZone)

function l_06_Town_Z_Gates:__init(name, mode, PlayField)
	ZoomZone.__init(self, name, mode, PlayField)
	
	-- code on gate
	local correct_code = {0, 3, 2, 1}
	local typed_code = {2, 0, 0, 0}
	
	--------------------------------------------------------------------------------
	if HasGameState(self.name..'_Typed_Code') then
	    typed_code = Helpers.RestoreObjectFromGameState(self.name..'_Typed_Code')
	else
	    Helpers.SaveObjectToGameState(self.name..'_Typed_Code', typed_code)
	end
	--------------------------------------------------------------------------------

	--------------------------------------------------------------------------------
	local function load_buttons_state()
	    for i = 1, 4 do
	        for j = 0, 3 do
	            if typed_code[i] == j then
	                self:entityShow('zg_'..i..'_'..j)
	            end
	        end
	    end
	end
	--------------------------------------------------------------------------------

	-- apply cube ------------------------------------------------------------------
	do
		self:getEntityNamed('compositiontake'):show()
		self:getEntityNamed('compositiontake'):getElementByName('lock2'):setOpacity(0)

		local function on_apply_cube_bttn(event) 
			self.playfield:entityHide(event.target)
			--self:getEntityNamed('compositiontake'):getElementByName('lock'):setOpacity(0)
			
			self:getEntityNamed('compositiontake'):getElementByName('lock2'):ChangeOpacity(0, 1, 0.25)			

			self.playfield:getHint():setAdditionalHintObject( 'area_zoom_Gates' )

			for i = 1, 4 do
				if not event.save then
					self.playfield:entityShow('zg_' .. i .. '_' .. typed_code[i])
				end	
			end		
			
			if event.save then
				load_buttons_state()

				self.playfield:removeNotebookHint()
			else
				self.playfield:setNotebookHint()
			end
		end

		local info = self:registerToolApplyInfo('09_remote_control_button', 'area_apply_zg_cude', on_apply_cube_bttn, 'BBT_06_LOCK')
		info.on_target_click = function() self:onClickBegin() end		
	end
	--------------------------------------------------------------------------------
	
	--------------------------------------------------------------------------------
	local function check_lock_open(save)
	    for i = 1, #correct_code do
	        if correct_code[i] ~= typed_code[i] then
	            return
	        end
	    end
	    
	    AddUniqueGameState(self.name..'_Lock_Open', 'true')

	    self.playfield:getHint():removeAdditionalHintObject( 'area_zoom_Gates' )
	    self.playfield:removeNotebookHint()
	
	    for i = 1, 4 do
	        self:entityHide('area_click_zg_button_'..i)
	    end



	    self.playfield:entityShow('area_link_10')
	    if not save then
	    	if not HasGameState('GateOpened_06') then
		    	SetGameState('GateOpened_06','true')
		    end

		    self.timer:add( function()
	    					self:closeAndDisable()
	    				end, 0.5)
	        
	        self.playfield:entityFadeShow('Layer_Gate_opened')
	        self.playfield:entityFadeHide('Layer_Gate_closed')
	        self:playSound('06_open_gates')
	    else	        
	        self.playfield:entityShow('Layer_Gate_opened')
	        self.playfield:entityHide('Layer_Gate_closed')
	        self:closeAndDisable()
	    end
	end
	
	check_lock_open()
	--------------------------------------------------------------------------------

	--------------------------------------------------------------------------------
	local function on_click_zt_button(e)
	    local i = tonumber(string.sub(e.target, -1))
	    
	    if not e.save then
	    	self:playSound('06_click_button')
	        self:entityHide('zg_'..i..'_'..typed_code[i])
	        
	        typed_code[i] = typed_code[i] + 1
	        typed_code[i] = typed_code[i] < 4 and typed_code[i] or 0
	        
	        Helpers.SaveObjectToGameState(self.name..'_Typed_Code', typed_code)
	        
	        self:entityShow('zg_'..i..'_'..typed_code[i])
	        
	        check_lock_open()
	    end
	end	
	for i = 1, 4 do
	    self:registerActiveObject('area_click_zg_button_'..i, on_click_zt_button, true)
	end
	--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
function l_06_Town_Z_Gates:onOpen()
	if IsToolApplied('09_remote_control_button') then
		self.playfield:setNotebookHint()
	end

	if self.playfield.zg_furry then
		self.playfield.zg_furry:play()
	end
end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
function l_06_Town_Z_Gates:onClose()
	if IsToolApplied('09_remote_control_button') then
		self.playfield:removeNotebookHint()
	end
end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
function l_06_Town_Z_Gates:onCloseFinished()
	if self.playfield.zg_furry then
		self.playfield.zg_furry:pause()
	end
end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
function l_06_Town_Z_Gates:onClickBegin()		
		self:playSound('06_click_on_lock')
	    self:getEntityNamed('compositiontake'):play()
	   
	    self.mouseBlocker:push()

	    self.timer:add(function()
	   		 self.mouseBlocker:pop()
	   		 self:getEntityNamed('compositiontake'):rewind(true)
	  	end, 1)		
end
--------------------------------------------------------------------------------

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

function l_06_Town_Z_Gates:CreateLoaderTable()
	local objects =
	{
		{ name = 'zg_bg',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_bg.jpg',  width = 640,  height = 480,  },
		{ name = 'zg_lock',  entityType = 'SpriteEntity',  x = 500,  y = 459,  order = 12,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_lock.png',  width = 252,  height = 128,  },
		{ name = 'anchor_zg_bttn_1',  entityType = 'SpriteEntity',  parentName = 'zg_lock',  x = -63,  y = 21,  order = 2,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 1,  height = 1,  },
		{ name = 'zg_1_0',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_1',  x = 0,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_1_0.png',  width = 37,  height = 44,  },
		{ name = 'zg_1_1',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_1',  x = 1,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_1_1.png',  width = 34,  height = 46,  },
		{ name = 'zg_1_2',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_1',  x = 0,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_1_2.png',  width = 39,  height = 43,  },
		{ name = 'zg_1_3',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_1',  x = 3,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_1_3.png',  width = 43,  height = 45,  },
		{ name = 'anchor_zg_bttn_2',  entityType = 'SpriteEntity',  parentName = 'zg_lock',  x = -20,  y = 22,  order = 2,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  width = 1,  height = 1,  },
		{ name = 'zg_2_0',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_2',  x = -2,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_2_0.png',  width = 36,  height = 43,  },
		{ name = 'zg_2_1',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_2',  x = 0,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_2_1.png',  width = 32,  height = 45,  },
		{ name = 'zg_2_2',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_2',  x = -1,  y = -1,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_2_2.png',  width = 37,  height = 45,  },
		{ name = 'zg_2_3',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_2',  x = 0,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_2_3.png',  width = 37,  height = 47,  },
		{ name = 'anchor_zg_bttn_3',  entityType = 'SpriteEntity',  parentName = 'zg_lock',  x = 26,  y = 20,  order = 2,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  width = 1,  height = 1,  },
		{ name = 'zg_3_0',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_3',  x = 0,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_3_0.png',  width = 40,  height = 45,  },
		{ name = 'zg_3_1',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_3',  x = 0,  y = 2,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_3_1.png',  width = 35,  height = 46,  },
		{ name = 'zg_3_2',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_3',  x = -16,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_3_2.png',  width = 71,  height = 45,  },
		{ name = 'zg_3_3',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_3',  x = 0,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_3_3.png',  width = 33,  height = 45,  },
		{ name = 'anchor_zg_bttn_4',  entityType = 'SpriteEntity',  parentName = 'zg_lock',  x = 53,  y = 21,  order = 2,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  width = 1,  height = 1,  },
		{ name = 'zg_4_0',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_4',  x = 16,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_4_0.png',  width = 34,  height = 46,  },
		{ name = 'zg_4_1',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_4',  x = 16,  y = -2,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_4_1.png',  width = 40,  height = 45,  },
		{ name = 'zg_4_2',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_4',  x = 15,  y = -1,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_4_2.png',  width = 38,  height = 44,  },
		{ name = 'zg_4_3',  entityType = 'SpriteEntity',  parentName = 'anchor_zg_bttn_4',  x = 15,  y = 0,  order = 0,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Scenes/06_Town/Z_Gates/zg_4_3.png',  width = 39,  height = 43,  },
		{ name = 'area_click_zg_button_1',  entityType = 'SpriteEntity',  parentName = 'zg_lock',  x = -71,  y = 26,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 50,  height = 60,  },
		{ name = 'area_click_zg_button_2',  entityType = 'SpriteEntity',  parentName = 'zg_lock',  x = -21,  y = 26,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 45,  height = 60,  },
		{ name = 'area_click_zg_button_3',  entityType = 'SpriteEntity',  parentName = 'zg_lock',  x = 26,  y = 26,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 43,  height = 60,  },
		{ name = 'area_click_zg_button_4',  entityType = 'SpriteEntity',  parentName = 'zg_lock',  x = 75,  y = 26,  order = 100,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 50,  height = 60,  },
		{ name = 'area_apply_zg_cude',  entityType = 'SpriteEntity',  x = 510.5,  y = 458,  order = 50,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  width = 255,  height = 160,  },
		{ name = '06_Town_Anim_Z_Gates',  entityType = 'CompositionContainer',  x = 192,  y = 144,  order = 8,  editingLocked = false,  treeItemIsExpanded = true,  path = 'Data/Scripts/Scenes/06_Town/Anim/06_Town_Anim_Z_Gates.lua',  },
		{ name = 'compositionfon',  entityType = 'CompositionSequence',  parentName = '06_Town_Anim_Z_Gates',  x = 0,  y = 0,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  isCyclic = true,  speed = 1,  },
		{ name = 'compositiontake',  entityType = 'CompositionSequence',  parentName = '06_Town_Anim_Z_Gates',  x = 0,  y = 0,  order = 0,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
	}
	return objects
end
