class 'p04_l01_m01' (Script)

function  p04_l01_m01:__init()super()
  self.arr_button={}
  self.arr_number ={}
  self.current={}
  self.load=false
  self.initOne=true
  self.arrIndex={}
  self.flagHide=false
  --params
  self.arrPos={{493,307},{600,307},{707,307} }
  self.speed=200
  self.complete={5,2,7}
end

function p04_l01_m01:on_attach()
 local button=code_button01()
 local number=code_number()
 local scene = self:scene()
 if self.initOne then

  for i=1,3 do
   button=code_button01()
   button.object=scene:object('z01_button_up0'..i)
   button.index=i
   button.type=1

   self.arr_button[i]=button
  end

  for s=1,3 do
    button=code_button01()
    button.object=scene:object('z01_button_down0'..s)
    button.index=s
    button.type=-1
    self.arr_button[s+3]=button
  end


  for j=1,3 do
   self.arr_number[j]={}
   for k=1,10 do
    number=code_number()
    number.index=j
    number.power=k-1
    number.object=scene:object('z01_number0'..(k-1)..'-'..j)

    number.posSenter=Position(self.arrPos[j][1],self.arrPos[j][2])
    number.posDown=Position(self.arrPos[j][1],self.arrPos[j][2]+number.object:dimensions():ly())
    number.posUp=Position(self.arrPos[j][1],self.arrPos[j][2]-number.object:dimensions():ly())
    number.tween=tweener()
    self.arr_number[j][k]=number
   end
  end

  if  self.load then
   for p=1,3 do
    self.current[p]= self.arr_number[p][self.arrIndex[p]]
    self.current[p].object:set_position(self.current[p].posSenter,true)

   end
  else
   for p=1,3 do
    self.current[p]= self.arr_number[p][1]
    self.current[p].object:set_position(self.current[p].posSenter,true)
   end
   self:my_save()
  end



  self.sel_up=scene:object('z01_button_onup01')
  self.sel_down=scene:object('z01_button_ondown01')
  self.click_up=scene:object('z01_button_mouseclick_up')
  self.click_down=scene:object('z01_button_mouseclick_down')
 end
 self.initOne=false

end

function p04_l01_m01:on_lb_up()
     self.click_up:set_position(Position(-100,-100),true)
     self.click_down:set_position(Position(-100,-100),true)
end

function p04_l01_m01:on_lb_down()
  local scene=self:scene()
  local selected=scene:overed_object()
  if selected then
   if self:completeMove() then
    for i=1,#self.arr_button do
      if selected:name()==self.arr_button[i].object:name() then
        --print(selected:name())
        if self.arr_button[i].type==1 then
         self.click_up:set_position(self.arr_button[i].object:position(),true)

        end
        if self.arr_button[i].type==-1 then
          self.click_down:set_position(self.arr_button[i].object:position(),true)
        end
         self:moveNumbers(self.arr_button[i].index,self.arr_button[i].type)
         self.flagHide=true
         self:my_save()
         self:completeGame()
         scene:play_sound('g_click_button')
        return
      end
    end
   end
  end
end

function p04_l01_m01:on_mouse_move()
  local scene=self:scene()
  local selected=scene:overed_object()
  if selected then

    for i=1,#self.arr_button do
      if selected:name()==self.arr_button[i].object:name() then
        --print(selected:name())
        if self.arr_button[i].type==1 then
         self.sel_up:set_position(self.arr_button[i].object:position(),true)

        end
        if self.arr_button[i].type==-1 then
          self.sel_down:set_position(self.arr_button[i].object:position(),true)
        end
        return
      end

    end
    self.sel_down:set_position(Position(-100,-100),true)
     self.sel_up:set_position(Position(-100,-100),true)

  end
end

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


  if self.flagHide then
   if  self:completeMove() then
    self.flagHide=false
    for j=1,3 do
      for k=1,10 do
       if self.current[j].object:name()~=self.arr_number[j][k].object:name() then
         self.arr_number[j][k].object:hide()
       end
      end
    end
   end
  end
end

--my function
--1
function p04_l01_m01:completeMove()
  for i=1,#self.arr_number do
    for j=1,#self.arr_number[i] do
     if not self.arr_number[i][j].tween:getCompleteMoveGlob() then
      return false
     end
    end
  end
  return true
end
--2
function p04_l01_m01:moveNumbers(index,type)
 --self.current[index].power

 if type==1 then
   if self.current[index].power~=9 then
--     print(self.current[index].posSenter:y())
--     print(self.current[index].posUp:y())

     self.current[index].tween:getsParmsMove(self.current[index].object,self.speed,
                                              self.current[index].posSenter,self.current[index].posUp)
     self.arr_number[index][self.current[index].power+2].object:show()
     self.arr_number[index][self.current[index].power+2].tween:getsParmsMove(self.arr_number[index][self.current[index].power+2].object,self.speed,
                                                                             self.arr_number[index][self.current[index].power+2].posDown,
                                                                             self.arr_number[index][self.current[index].power+2].posSenter)

     self.current[index]= self.arr_number[index][self.current[index].power+2]
   else
     self.current[index].tween:getsParmsMove(self.current[index].object,self.speed,
                                             self.current[index].posSenter,self.current[index].posUp)
     self.arr_number[index][1].object:show()
     self.arr_number[index][1].tween:getsParmsMove(self.arr_number[index][1].object,self.speed,
                                                   self.arr_number[index][1].posDown,
                                                  self.arr_number[index][1].posSenter)

     self.current[index]= self.arr_number[index][1]
   end
 end
  if type==-1 then
   if self.current[index].power~=0 then
    self.current[index].tween:getsParmsMove(self.current[index].object,self.speed,
                                             self.current[index].posSenter,self.current[index].posDown)

    self.arr_number[index][self.current[index].power].object:show()
    self.arr_number[index][self.current[index].power].tween:getsParmsMove(self.arr_number[index][self.current[index].power].object,self.speed,
                                                   self.arr_number[index][self.current[index].power].posUp,
                                                  self.arr_number[index][self.current[index].power].posSenter)
     self.current[index]= self.arr_number[index][self.current[index].power]
   else
      self.current[index].tween:getsParmsMove(self.current[index].object,self.speed,
                                             self.current[index].posSenter,self.current[index].posDown)
      self.arr_number[index][10].object:show()
      self.arr_number[index][10].tween:getsParmsMove(self.arr_number[index][10].object,self.speed,
                                                   self.arr_number[index][10].posUp,
                                                  self.arr_number[index][10].posSenter)
     self.current[index]= self.arr_number[index][10]
  end
  end

end
--3
function p04_l01_m01:completeGame()
  local scene=self:scene()
  for i=1,#self.current do
   if self.current[i].power~=self.complete[i] then
     return
   end

  end
  self:scene():play_sound('g_complete')
  scene:execute_action('complete_game01')

end

function p04_l01_m01:save(os)
  os:save(0) -- version

  os:save(true)
  os:save(self.flagHide)
 -- self:my_save()
  local size =#self.arrIndex
  os:save(size)
  for i=1,size do
   os:save(self.arrIndex[i])
  end
end

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

  self.load = is:load()
  self.flagHide=is:load()
  local size = is:load()
  --print(size)
  for j=1,size do
   self.arrIndex[j]=is:load()
  -- print(self.arrIndex[j])
  end
end

function p04_l01_m01:my_save()

 for i=1,3 do
  self.arrIndex[i]=self.current[i].power+1
 end
end

