class 'l_04_Guitar' (PlayField)

local ANCHOR = 'anchor_'
local SPLIT = 'Splitting_'
local ANIM_GREEN = 'compositionString_green_'
local ANIM_RED = 'compositionString_red_'

function l_04_Guitar:__init(game_scene, name, path)
	PlayField.__init(self, game_scene, name, path)

	self.is_outdoor_scene = false

	-- interactive help ------------------------------------
	self.tutorial = self:getGameScene():getTutorial()

	if ( not self.tutorial:isTipShown( 'MG_SKIP' ) )then
		self.tutorial:showTip('MG_SKIP')
	end
	--------------------------------------------------------
		
	AddUniqueGameState('04_Guitar_Return_from_mg', 'true')
	
	self.string_num = 6
	self.pos_num = 3
	self.right_combo = { 3,1,3,2,1,2 }
	self.current_combo = { 1,1,1,1,1,1 }
	self.anim_time = 2.2
	self.SPLIT = {}
	
	self.PLAY_BTTN = self:getEntityNamed('play')
	self.PLAY_BTTN.onPress:add(bind(self.onPlayPress, self))
	self.PLAY_BTTN:setCursorOverName('CursorActive')
	self.LIGHT = { self:getEntityNamed('Splitting_light_1'),self:getEntityNamed('Splitting_light_2'),self:getEntityNamed('Splitting_light_3') }
		
	for i=1, self.string_num do
		local anchor = self:getEntityNamed(ANCHOR..i)
		anchor.onClickBegin:add(bind(self.onClick, self))
		anchor.onFocusIn:add(bind(self.onFocusIn, self))
		anchor.onFocusOut:add(bind(self.onFocusOut, self))
		anchor:setCursorOverName('CursorActive')
		self.SPLIT[i] = {}
		for j=1, self.pos_num do
			self.SPLIT[i][j] = self:getEntityNamed(SPLIT..i..'_'..j)
			self.SPLIT[i][j]:setRespondable(false)
		end
		
		local anim_red = self:getEntityNamed(ANIM_RED..i)
		local anim_green = self:getEntityNamed(ANIM_GREEN..i)
		
		anim_red:hide()
		anim_green:hide()
		
		anim_green.onFinish:add(function() anim_green:hide() end)
		anim_red.onFinish:add(function() anim_red:hide()  end)
															
															
	end
	
	local final_anim = self:getEntityNamed('compositionfon')
	local mediator = self:getEntityNamed('compositionmediator')
		
	final_anim:hide()
		
	mediator:hide()
	mediator.onFinish:add(function() 
									mediator:hide() 
									if not HasGameState('l_04_Guitar', 'completeMG') then
										self:getEntityNamed('Mediator'):show() 
									end
							end)
		
	self:onFocusOut()
	self:Load()	
		
end

function l_04_Guitar:onClick(ent)
	local name = ent:getName()
	local num = tonumber(string.sub(name, -1))
	self.current_combo[num] = self.current_combo[num] + 1
	if self.current_combo[num] > self.pos_num then
		self.current_combo[num] = self.current_combo[num] - self.pos_num 
	end
	
	self:restartSound('change_mode')
		
	Helpers.SaveObjectToGameState('l_04_Guitar_current_combo', self.current_combo)
	self:onFocusOut()
		
	for i=1, self.pos_num do
		if i == self.current_combo[num] then
			self.SPLIT[num][i]:setOpacity(1)
		else
			self.SPLIT[num][i]:setOpacity(0)
		end
	end
			
end

function l_04_Guitar:onFocusIn(ent)
	local name = ent:getName()
	local ent_num = tonumber(string.sub(name, -1))
	local light_num = self.current_combo[ent_num]
	
	local ang = 0
	if ent_num > 3 then
		ang = 180
	end
	
	self.LIGHT[light_num]:setXY(ent:getX(), ent:getY())
	self.LIGHT[light_num]:setRotationDeg(ang)
	self.LIGHT[light_num]:setOpacity(1)
end

function l_04_Guitar:onFocusOut()	
	for i=1, self.pos_num do
		self.LIGHT[i]:setOpacity(0)
	end
end

function l_04_Guitar:splitRespond(bool,time)
	for i=1, self.string_num do
		self:getEntityNamed(ANCHOR..i):setRespondable(bool)
	end
	self.PLAY_BTTN:setRespondable(bool)
	if time then
		self.timer:add(function()
			if not HasGameState('l_04_Guitar', 'completeMG') then
				self:splitRespond(true)
			end
		end, time)
	end
end

function l_04_Guitar:completeMG(time)
	
	if not time then
		time = 0
	end
	
	for i=1, self.string_num do
		if self.current_combo[i] ~= self.right_combo[i] then
			return
		end
	end
	
	AddUniqueGameState('l_04_Guitar', 'completeMG')
	self:getPanel():setMode(EPanelMode.QUEST)
	
	self:playFinalAnim(time)
	
	self:completeMinigame(8+time)
	
end

function l_04_Guitar:playFinalAnim(time)

	if time then
		self.timer:add(function()
			self:playFinalAnim()
		end,time)
		return
	end
	
	local final_anim = self:getEntityNamed('compositionfon')
	local anim_time = final_anim:getDuration()
	final_anim:play()
	self:playSound('guitar_melody')
	
	self.timer:add(function()
	
		final_anim:play()
		
		self.timer:add(function()
			final_anim:play()
		end, anim_time)
		
	end, anim_time )
	
end

function l_04_Guitar:onPlayPress()	
	
	self:splitRespond(false,self.anim_time)
	
	self:getEntityNamed('Mediator'):hide()
	self:getEntityNamed('compositionmediator'):play()
	self.timer:add(function()
		self:playSound('guitar_bryn')
		for i=1, self.string_num do
			if self.current_combo[i] == self.right_combo[i] then
				self:getEntityNamed(ANIM_GREEN..i):play()
			else
				self:getEntityNamed(ANIM_RED..i):play()
			end
		end	
	end, 0.2)
	self:completeMG(self.anim_time)	
end	

function l_04_Guitar:setCurrentSplitPos()	
	for i=1, self.string_num do
		for j=1, self.pos_num do
			if j == self.current_combo[i] then
				self.SPLIT[i][j]:setOpacity(1)
			else
				self.SPLIT[i][j]:setOpacity(0)
			end
		end
	end
end

function l_04_Guitar:SkipMiniGame()	
	AddUniqueGameState('l_04_Guitar', 'completeMG')
	self:getEntityNamed('Mediator'):hide()
	self.current_combo = self.right_combo
	Helpers.SaveObjectToGameState('l_04_Guitar_current_combo', self.current_combo)
	self:setCurrentSplitPos()	
	self:splitRespond(false)
	self:playFinalAnim(1)
	self:completeMinigame(9)
end

function l_04_Guitar:Load()	
	if HasGameState('l_04_Guitar_current_combo') then
		self.current_combo = Helpers.RestoreObjectFromGameState('l_04_Guitar_current_combo')
	end
	self:setCurrentSplitPos()	
	if HasGameState('l_04_Guitar', 'completeMG') then
		self:getPanel():setMode(EPanelMode.QUEST)
		self:splitRespond(false)
		self:getEntityNamed('Mediator'):hide()
		self:completeMinigame(1)
	end
end


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

function l_04_Guitar:CreateLoaderTable()
	local objects =
	{
		{ name = 'Ph3_MG_04_Guitar_BG',  entityType = 'SpriteEntity',  x = 512,  y = 382,  order = -1,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Ph3_MG_04_Guitar_BG1.jpg',  width = 1366,  height = 768,  },
		{ name = 'Mediator',  entityType = 'SpriteEntity',  x = 321,  y = 528,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Mediator.png',  width = 130,  height = 100,  },
		{ name = 'play',  entityType = 'ButtonEntity',  parentName = 'Mediator',  x = 5,  y = 3,  order = 2,  isVisible = false,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Button.png',  text = '',  activeRectX = 97,  activeRectY = 88,  },
		{ name = 'anchor_1',  entityType = 'SpriteEntity',  x = 581,  y = 137,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
		{ name = 'Splitting_1_1',  entityType = 'SpriteEntity',  parentName = 'anchor_1',  x = 0.737,  y = 0.419006,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_1.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_1_2',  entityType = 'SpriteEntity',  parentName = 'anchor_1',  x = 0,  y = 1.52588e-005,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_2.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_1_3',  entityType = 'SpriteEntity',  parentName = 'anchor_1',  x = 0,  y = 1.52588e-005,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_3.png',  width = 118,  height = 92,  },
		{ name = 'anchor_2',  entityType = 'SpriteEntity',  x = 699,  y = 130,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
		{ name = 'Splitting_2_1',  entityType = 'SpriteEntity',  parentName = 'anchor_2',  x = 0.737,  y = 0.419006,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_1.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_2_2',  entityType = 'SpriteEntity',  parentName = 'anchor_2',  x = 0,  y = 1.52588e-005,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_2.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_2_3',  entityType = 'SpriteEntity',  parentName = 'anchor_2',  x = 0,  y = 0,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_3.png',  width = 118,  height = 92,  },
		{ name = 'anchor_3',  entityType = 'SpriteEntity',  x = 819,  y = 106,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
		{ name = 'Splitting_3_1',  entityType = 'SpriteEntity',  parentName = 'anchor_3',  x = 0.737,  y = 0.419006,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_1.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_3_2',  entityType = 'SpriteEntity',  parentName = 'anchor_3',  x = 0,  y = 1.52588e-005,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_2.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_3_3',  entityType = 'SpriteEntity',  parentName = 'anchor_3',  x = 0,  y = 1.52588e-005,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_3.png',  width = 118,  height = 92,  },
		{ name = 'anchor_4',  entityType = 'SpriteEntity',  x = 821,  y = 591,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
		{ name = 'Splitting_4_1',  entityType = 'SpriteEntity',  parentName = 'anchor_4',  x = 0.737366,  y = 0.419189,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_down_1.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_4_2',  entityType = 'SpriteEntity',  parentName = 'anchor_4',  x = 6.10352e-005,  y = 0,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_down_2.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_4_3',  entityType = 'SpriteEntity',  parentName = 'anchor_4',  x = 6.10352e-005,  y = 0,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_down_3.png',  width = 118,  height = 92,  },
		{ name = 'anchor_5',  entityType = 'SpriteEntity',  x = 702,  y = 566,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
		{ name = 'Splitting_5_1',  entityType = 'SpriteEntity',  parentName = 'anchor_5',  x = 0.737366,  y = 0.419189,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_down_1.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_5_2',  entityType = 'SpriteEntity',  parentName = 'anchor_5',  x = 6.10352e-005,  y = 0,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_down_2.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_5_3',  entityType = 'SpriteEntity',  parentName = 'anchor_5',  x = 6.10352e-005,  y = 0,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_down_3.png',  width = 118,  height = 92,  },
		{ name = 'anchor_6',  entityType = 'SpriteEntity',  x = 585,  y = 570,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
		{ name = 'Splitting_6_1',  entityType = 'SpriteEntity',  parentName = 'anchor_6',  x = 0.737366,  y = 0.419189,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_down_1.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_6_2',  entityType = 'SpriteEntity',  parentName = 'anchor_6',  x = 6.10352e-005,  y = 0,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_down_2.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_6_3',  entityType = 'SpriteEntity',  parentName = 'anchor_6',  x = 6.10352e-005,  y = 0,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_down_3.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_light_1',  entityType = 'SpriteEntity',  x = -78.0001,  y = 153.077,  order = 0,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_1_light.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_light_2',  entityType = 'SpriteEntity',  x = -79.5961,  y = 144.086,  order = 0,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_2_light.png',  width = 118,  height = 92,  },
		{ name = 'Splitting_light_3',  entityType = 'SpriteEntity',  x = -79.5961,  y = 144.086,  order = 0,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/Minigames/04_Guitar/Splitting_up_3_light.png',  width = 118,  height = 92,  },
		{ name = 'string_anchor',  entityType = 'SpriteEntity',  x = 424,  y = 699,  order = 0,  isRespondable = false,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
		{ name = 'String_1',  entityType = 'SpriteEntity',  parentName = 'string_anchor',  x = -213.882,  y = -389.025,  order = 0,  angle = -2,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/String_1.png',  width = 464,  height = 102,  },
		{ name = 'String_2',  entityType = 'SpriteEntity',  parentName = 'string_anchor',  x = -224.409,  y = -362.709,  order = 0,  angle = -1,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/String_1.png',  width = 464,  height = 102,  },
		{ name = 'String_3',  entityType = 'SpriteEntity',  parentName = 'string_anchor',  x = -228.62,  y = -334.288,  order = 0,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/String_1.png',  width = 464,  height = 102,  },
		{ name = 'String_4',  entityType = 'SpriteEntity',  parentName = 'string_anchor',  x = -230.726,  y = -305.867,  order = 0,  angle = 2,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/String_1.png',  width = 454,  height = 102,  },
		{ name = 'String_5',  entityType = 'SpriteEntity',  parentName = 'string_anchor',  x = -232.831,  y = -280.604,  order = 0,  angle = 2,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/String_1.png',  width = 445,  height = 102,  },
		{ name = 'String_6',  entityType = 'SpriteEntity',  parentName = 'string_anchor',  x = -228.621,  y = -255.341,  order = 0,  angle = 3,  editingLocked = true,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Minigames/04_Guitar/String_1.png',  width = 437,  height = 102,  },
		{ name = '04_Guitar_anim',  entityType = 'CompositionContainer',  x = -171,  y = 37,  order = 5,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  path = 'Data/Scripts/Minigames/04_Guitar/anim/04_Guitar_anim.lua',  },
		{ name = 'compositionString_green_1',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
		{ name = 'compositionString_green_2',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
		{ name = 'compositionString_green_3',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
		{ name = 'compositionString_green_4',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
		{ name = 'compositionString_green_5',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
		{ name = 'compositionString_green_6',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
		{ name = 'compositionString_red_1',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  speed = 1,  },
		{ name = 'compositionString_red_2',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isActive = false,  editingLocked = false,  treeItemIsExpanded = true,  speed = 1,  },
		{ name = 'compositionString_red_3',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
		{ name = 'compositionString_red_4',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
		{ name = 'compositionString_red_5',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
		{ name = 'compositionString_red_6',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  speed = 1,  },
		{ name = 'compositionfon',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isVisible = false,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
		{ name = 'compositionmediator',  entityType = 'CompositionSequence',  parentName = '04_Guitar_anim',  x = 0,  y = 0,  order = 0,  isActive = false,  editingLocked = false,  treeItemIsExpanded = false,  speed = 1,  },
	}
	return objects
end
