class 'p03_l11_m01_game' (Script)
function p03_l11_m01_game:__init()super()
 self.arrElementChanges={{1,3,4},{3,5,10},{2,5},{7,9,10},{3,4,7},{6,8,11,12} }
 self.arrButton={}
 self.arrTree={}
 self.arrTween={}

self.positionSave={}
 self.load=false
 self.ititetionOne=true
end

function p03_l11_m01_game:on_attach()
 local button=p03_l11_m01_button()
 local scene=self:scene()
 if self.ititetionOne then
     
  for i=1,6 do
    button=p03_l11_m01_button()
    button.object=scene:object('minigame_button0'..i)
   -- button.posUp=button.object:position()
   -- button.posDown=Position(button.object:position():x()+1,button.object:position():y()+30)
    button.state=1
    button.arrChenges=self.arrElementChanges[i]
    self.arrTween[i]=tweener()
    button.tween=tweener()
   self.arrButton[i]=button
  end
  if self.load then
    for i=1,6 do
       -- print(self.positionSave[i]:x())
     self.arrButton[i].posUp=self.positionSave[i]
     self.arrButton[i].posDown=Position(self.arrButton[i].posUp:x()+1,self.arrButton[i].posUp:y()+30)
    end
  else
    for i=1,6 do
     self.positionSave[i]=self.arrButton[i].object:position()
     self.arrButton[i].posUp=self.positionSave[i]
     self.arrButton[i].posDown=Position(self.arrButton[i].posUp:x()+1,self.arrButton[i].posUp:y()+30)
    end
  end
  for i=1,6 do
    self.arrButton[i].object:set_position(self.positionSave[i],true)
  end
  
  for j=1,9 do
   self.arrTree[j]=scene:object('tree_part0'..j)
   self.arrTree[j]:hide()
  end

  for k=10,12 do
   self.arrTree[k]=scene:object('tree_part'..k)
   self.arrTree[k]:hide()
  end
 end
 self.load=false
 self.ititetionOne=false
end

function p03_l11_m01_game:on_lb_down()
 --print('click')
 local scene=self:scene()
 local selected=scene:overed_object()
 if selected then
  for i=1,#self.arrButton do
   if selected:name()==self.arrButton[i].object:name() and self.arrButton[i].tween:getCompleteMoveGlob() then
    -- print('click_button')
    self:moveButton(self.arrButton[i])
    self:showTree(self.arrButton[i])
    self:completeGame()
    self.arrButton[i].state=self.arrButton[i].state*(-1)
    scene:play_sound('g_click_button')
   end
  end
 end
end

function p03_l11_m01_game:on_update(dt)
 for i=1,#self.arrButton do
  self.arrButton[i].tween:update(dt)
 end
end

function p03_l11_m01_game:moveButton(button)
--    print(button.object:name())
--    print(button.posUp:x())
--    print(button.posDown:y())
--    print(button.state)
   if button.state==1 then
    button.tween:getsParmsMove(button.object,100,button.posUp,button.posDown)
   end

   if button.state==-1 then
    button.tween:getsParmsMove(button.object,100,button.posDown,button.posUp)
   end
end

function p03_l11_m01_game:showTree(button)
 for i=1,#button.arrChenges do
  if self.arrTree[button.arrChenges[i]]:visible() then
   self.arrTree[button.arrChenges[i]]:hide()
  else
   self.arrTree[button.arrChenges[i]]:show()
  end
 end
end

function p03_l11_m01_game:completeGame()
 local count=0
 local scene=self:scene()
 for i =1,#self.arrTree do
  if self.arrTree[i]:visible() then
   count=count+1
  end
 end
 if count>11 then
  self:scene():play_sound('g_complete')
  scene:execute_action('complete_game1')
 end

end

function p03_l11_m01_game:save(os)
    os:save(0) -- version
    os:save(true)
    for i=1,6 do
      os:save(self.positionSave[i])
    end
end

function p03_l11_m01_game:load(is)
    local ver = is:load()
    self.load = is:load()
    for i=1,6 do
     self.positionSave[i]=is:load()
    end
end
