class 'p03_l12_m01_game01' (Script)

function p03_l12_m01_game01:__init()super()
  self.incialOne=true
  self.arrPartAvtive={}
  self.arrPartNoAvtive={}
  self.currentObj=nil
  self.tween={}

 self.load=false
 self.loadPos={}
end
function p03_l12_m01_game01:on_attach()
  local scene = self:scene()
  local part = p03_l12_m01_game01_part()
  if self.incialOne then
     for i=1,9 do
       part = p03_l12_m01_game01_part()
       part.object=scene:object('minigame_part0'..i)
    --   part.startPos=part.object:position()
       part.index=i
      self.arrPartAvtive[i]  =part
      self.arrPartNoAvtive[i]=scene:object('minigame_part0'..i..'_1')
    end
    for j=10,21 do
      part = p03_l12_m01_game01_part()
       part.object=scene:object('minigame_part'..j)
      -- part.startPos=part.object:position()
      part.index=j
      self.arrPartAvtive[j]  =part
      self.arrPartNoAvtive[j]=scene:object('minigame_part'..j..'_1')
    end

   if  self.load then

   else
    for i=1,#self.arrPartAvtive do
       self.loadPos[i]=self.arrPartAvtive[i].object:position()
    end
   end

   for i=1,#self.arrPartAvtive do
     self.arrPartAvtive[i].startPos=self.loadPos[i]
     self.arrPartAvtive[i].object:set_position(self.arrPartAvtive[i].startPos,true)
   end
   for s=1,#self.arrPartAvtive do
     self.tween[s]=tweener()
   end
  end

  --print(self.arrPartAvtive[10].object:name())
  self.incialOne=false
 self.load=false
end



function p03_l12_m01_game01:on_lb_down()
 local scene=self:scene()
 local selected =self:scene():overed_object()
 if selected then
   if self.currentObj==nil then
     for i=1,#self.arrPartAvtive do
       if selected:name()==self.arrPartAvtive[i].object:name() then
         self.currentObj=self.arrPartAvtive[i]
         self.currentObj.object:set_position(Position(scene:cursor_position():x()-self.currentObj.object:dimensions():lx()/2,scene:cursor_position():y()-self.currentObj.object:dimensions():ly()/2))
         self:capture_object(self.currentObj.object)
         scene:play_sound('g_capture_object')
       end
     end
   else
      if self.currentObj.object:is_crossing(self.arrPartNoAvtive[self.currentObj.index]) then
        self:is_cross()
        scene:play_sound('g_cross_object')
      else
       self:no_cross()
       scene:play_sound('g_fail_cross_object')
      end
   end
 end
end

function p03_l12_m01_game01:is_cross()
   self.arrPartNoAvtive[self.currentObj.index]:set_deep_level(30,true)
   self:release_capture(self.currentObj.object)
   self.currentObj.object:set_position(Position(-200,-200))
   self.currentObj.object:hide()
   self.currentObj=nil
  self:completeGame()
end

function p03_l12_m01_game01:no_cross()

  self:release_capture(self.currentObj.object)

-- print(self.currentObj.object:position():x())
--  print(self.currentObj.startPos:x())
 self.tween[self.currentObj.index]:getsParmsMove(self.currentObj.object,500,self.currentObj.object:position(),self.currentObj.startPos)

 self.currentObj=nil
end

function p03_l12_m01_game01:on_update(dt)

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

function p03_l12_m01_game01:completeGame()
  local scene =self:scene()
  local count=0
 for i=1,#self.arrPartNoAvtive do
  if self.arrPartNoAvtive[i]:deep_level()==30 then
    count=count+1
  end
   if count>20 then
       self:scene():play_sound('g_complete')
     scene:execute_action('complete_game1')
   end
 end
end

function p03_l12_m01_game01:save(os)
  os:save(0) -- version
  os:save(true)
  for i=1,21 do
   os:save(self.loadPos[i])
  end
end

function p03_l12_m01_game01:load(is)
  local ver = is:load()
  self.load = is:load()
  for i=1,21 do
   self.loadPos[i]=is:load()
  end
end