class 'p04_l03_game03' (Script)
function p04_l03_game03:__init()super()
 self.arrElementChanges={{1,3},{2,3},{2,5},{5,6},{1,2,6},{4,6}}
 self.arrButton={}
 self.timeAnimationShow=0
 self.currentAnimTime=0

self.loadArrSate={}
self.loadPosRack={}

 self.timeClick=0
 self.currentTimeClik=0

 self.arrShowbook={}
 self.arrHidebook={}
 self.arrStaticbook={}

self.animBookFromDown={}
self.animBookFromUp={}
self.arrStatickBoockDown={}
self.arrStaickBookUp={}

 self.arrTween={}

 self.arrCurrentAnimation={}
 self.arrCurrentBookUp={}
 self.arrCurrentBookDown={}
 self.arrMemoryBook={}

 self.showOne=true
 self.flagStartAnim=false
 self.ititetionOne=true

 self.clikRedy=true
 self.startTimerClick=false
end

function p04_l03_game03: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('rack0'..i)
     if #self.loadPosRack<5 then
        button.posUp=button.object:position()
     else

          button.posUp= self.loadPosRack[i]
     end
   -- button.posDown=Position(button.object:position():x()+1,button.object:position():y()+30)
    if 5<#self.loadArrSate then
     button.state=self.loadArrSate[i]
    else
     button.state=1
    end
   -- print(button.state)
    button.arrChenges=self.arrElementChanges[i]
    self.arrTween[i]=tweener()
    button.tween=tweener()
   self.arrButton[i]=button
  end

  self.arrButton[1].posDown=Position(self.arrButton[1].posUp:x()+45, self.arrButton[1].posUp:y())
  self.arrButton[2].posDown=Position(self.arrButton[2].posUp:x()-35, self.arrButton[2].posUp:y())
  self.arrButton[3].posDown=Position(self.arrButton[3].posUp:x()-35, self.arrButton[3].posUp:y())
  self.arrButton[4].posDown=Position(self.arrButton[4].posUp:x()+35, self.arrButton[4].posUp:y())
  self.arrButton[5].posDown=Position(self.arrButton[5].posUp:x()+50, self.arrButton[5].posUp:y())
  self.arrButton[6].posDown=Position(self.arrButton[6].posUp:x()+40, self.arrButton[6].posUp:y())


  for j=1,6 do
   self.animBookFromDown[j]=scene:object('a_book_up0'..j)
   self.animBookFromUp[j]=scene:object('a_book_down0'..j)
   self.arrStatickBoockDown[j]=scene:object('book0'..j..'_down')
   self.arrStaickBookUp[j]=scene:object('book0'..j..'_up')
  end
  self.timeAnimationShow=self.animBookFromDown[1]:animation_play_time()
  self.timeClick= self.animBookFromDown[1]:animation_play_time()
 end

 self.ititetionOne=false
end

function p04_l03_game03: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.clikRedy  then
    self.arrMemoryBook={}
   self.clikRedy=false
    self.startTimerClick=true
    self.currentTimeClik=0
    -- print('click_button')
    self:moveButton(self.arrButton[i])
    self:showBook(self.arrButton[i])
    self:completeGame()
    self.arrButton[i].state=self.arrButton[i].state*(-1)
    scene:play_sound('g_click_rack')
   end
  end
 end
end

function p04_l03_game03:on_update(dt)
 for i=1,#self.arrButton do
  self.arrButton[i].tween:update(dt)
 end
-- if self.flagStartAnim then
--  self.currentAnimTime=self.currentAnimTime+dt
--  if  self.currentAnimTime>self.timeAnimationShow then
--   self.flagStartAnim=false
--   self.currentAnimTime=0
--   self:showAnimation()
--  end
-- end

if self.startTimerClick then
 self.currentTimeClik=self.currentTimeClik+dt
 if self.currentTimeClik>self.timeClick then
 self.startTimerClick=false
  self.clikRedy=true
  for s=1,#self.arrMemoryBook do
   self.arrMemoryBook[s]:show()
  end
  end
end

end

function p04_l03_game03: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 p04_l03_game03:showBook(button)
 for i=1,#button.arrChenges do
  if self.arrStatickBoockDown[button.arrChenges[i]]:visible() then
   self.arrStatickBoockDown[button.arrChenges[i]]:hide()
   self.animBookFromDown[button.arrChenges[i]]:show()
  self.animBookFromUp[button.arrChenges[i]]:hide()
   table.insert(self.arrMemoryBook,self.arrStaickBookUp[button.arrChenges[i]])
  else

   self.animBookFromDown[button.arrChenges[i]]:hide()
   self.animBookFromUp[button.arrChenges[i]]:show()
   self.arrStaickBookUp[button.arrChenges[i]]:hide()
   table.insert(self.arrMemoryBook,self.arrStatickBoockDown[button.arrChenges[i]])
   --table.insert(self.arrCurrentAnimation,self.arrStatickBoockDown[button.arrChenges[i]])
   --self.flagStartAnim=true
  end
 end
end

function p04_l03_game03:completeGame()
local count=0
local scene=self:scene()
for i =1,#self.animBookFromUp do
  if self.animBookFromUp[i]:visible() then
   count=count+1
  end
end
if count>#self.animBookFromUp-1 then
    self:scene():play_sound('g_complete')
  scene:execute_action('complete_book01')
end
end

function p04_l03_game03:showAnimation()
--  for i=1,#self.arrCurrentAnimation do
--   self.arrCurrentAnimation[i]:show()
--  end
--  self.arrCurrentAnimation={}
end

function p04_l03_game03:save(os)
  os:save(0)
 -- io:save(self.currentTimeClik)
 --io:save(#self.arrButton)
  local size=#self.arrButton
  os:save(size)
  for i=1,size do
   local state=self.arrButton[i].state
   --io:save(self.arrButton[i].state)
   os:save(state)
  end
  for t=1,size do
   os:save(self.arrButton[t].posUp)
  end

end

function p04_l03_game03:load(is)
  local ver = is:load()


 -- self.currentTimeClik = io:load()
  local size=is:load()
  for i=1,size do
   self.loadArrSate[i]=is:load()
  end
  for t=1,size do
   self.loadPosRack[t]=is:load()
  end

end

