class 'p04_l03_m02_game01' (Script)

function p04_l03_m02_game01:__init()super()
 self.arrDisk={}
 self.arrCell={}
 self.arrSphere={}
 self.emptyCell=nil
 self.emptySfere=nil
 self.emptyIndex=nil
 self.greenSelected=nil
 self.blueSelected=nil
 self.redSelected=nil
 self.moveCompleteOne=true

 self.initOne=true
 self.directionMoveSphere=nil
 self.exeOne=true
 self.effgoOne=true

 self.timeLag=2
 self.timeCurrentLag=0
 self.timeOne=false
self.isLoad=false

self.circPos={}

 --params
 self.arrSfereInDisk={{1,2,1,2,3,1,2,3},
                {3,3,1,2,3,2,3,1},
                {1,3,1,2,3,1,2,2} }
 self.shiftDiskRadius={37,35,38}
 self.angel=45
 self.speedDisk=100
 self.speedSfere=150
end

function p04_l03_m02_game01:on_attach()
 local scene=self:scene()
 local disk=p01_l03_z04_disk()
 local arrSfereObject={}
 local arrSphereInDosk={}
 if self.initOne then
  -- print('init two')
  self.greenSelected=scene:object('sphere_green_select')
  self.blueSelected=scene:object('sphere_blue_select')
  self.redSelected=scene:object('sphere_red_select')
  self.backDisk=scene:object('discs_background')
  self.tweenDisk=tweener_mod()
  self.diskSenters=scene:object('disc_center')

  self.tweenSen=tweener_mod()
  self.tweenSen.posSave=false

  for j=1,3 do
   arrSfereObject[j]={}
  for s=1,8 do
     arrSfereObject[j][s]=p01_l03_z04_sphere()
     if j==1 then
      arrSfereObject[j][s].object=scene:object('sphere_green0'..s)
      arrSfereObject[j][s].type='green'
     end
     if j==2 then
      arrSfereObject[j][s].object=scene:object('sphere_blue0'..s)
      arrSfereObject[j][s].type='blue'
     end
     if j==3 then
      arrSfereObject[j][s].object=scene:object('sphere_red0'..s)
      arrSfereObject[j][s].type='red'
     end
  end
  end
  for o=1,#arrSfereObject do
   self.arrSphere[o]={}
   for w=1,#arrSfereObject[o] do
     self.arrSphere[o][w]=arrSfereObject[o][w]
   end
  end

 --self.arrSphere=arrSfereObject

  for t=1,3 do
   self.arrCell[t]={}
   arrSphereInDosk[t]={}
   for p=1,8 do
    self.arrCell[t][p]=p01_l03_z04_cell()
    if t==1 then
     self.arrCell[t][p].type='green'
     self.arrCell[t][p].objectComplete=scene:object('sphere_green0'..p..'_effect')
    end
    if t==2 then
     self.arrCell[t][p].type='blue'
     self.arrCell[t][p].objectComplete=scene:object('sphere_blue0'..p..'_effect')
    end
    if t==3 then
     self.arrCell[t][p].type='red'
     self.arrCell[t][p].objectComplete=scene:object('sphere_red0'..p..'_effect')
    end
    arrSphereInDosk[t][p]=arrSfereObject[self.arrSfereInDisk[t][p]][1]
    --print(arrSfereObject[self.arrSfereInDisk[t][p]][1].object:name())
    table.remove (arrSfereObject[self.arrSfereInDisk[t][p]],1)
    self.arrCell[t][p].object=arrSphereInDosk[t][p]
    self.arrCell[t][p].tween=tweener_mod()
   -- self.arrCell[t][p].tween.posSave=false
    --print(self.arrCell[t][p].object.object:name())
    self.arrCell[t][p].object.tween=tweener_mod()
 --   self.arrCell[t][p].object.tween.posSave=false
   end
  end
  for i=1,3 do
   disk=p01_l03_z04_disk()
   disk.arrCell=self.arrCell[i]
   disk.tween=tweener_mod()
  -- disk.tween.posSave=false
   disk.tweenSel=tweener_mod()
   --disk.tweenSel.posSave=false
   disk.direction=(-1)^(i+1)
   self.arrDisk[i]=disk
  end
   self.arrDisk[1].object=scene:object('circle_mouseoff_green')
   self.arrDisk[2].object=scene:object('circle_mouseoff_blue')
   self.arrDisk[3].object=scene:object('circle_mouseoff_red')

   self.arrDisk[1].selObject=scene:object('circle_mouseon_green')
   self.arrDisk[2].selObject=scene:object('circle_mouseon_blue')
   self.arrDisk[3].selObject=scene:object('circle_mouseon_red')
   if self.isLoad then

  else
    for d=1,3 do
     self.circPos[d]=self.arrDisk[d].object:position()
    end
   end



  for d=1,3 do
   self.arrDisk[d].senter=Position( self.circPos[d]:x()+self.arrDisk[d].object:dimensions():lx()/2,
                                    self.circPos[d]:y()+self.arrDisk[d].object:dimensions():ly()/2)
   self.arrDisk[d].radius=self.arrDisk[d].object:dimensions():ly()/2- self.shiftDiskRadius[d]
   for b=1,8 do
    self.arrDisk[d].arrCell[b].startAngel=45*(b-1)
    self.arrDisk[d].arrCell[b].position=self:getPositionSfere(b,self.arrDisk[d].senter,self.arrDisk[d].radius)  --1
    self.arrDisk[d].arrCell[b]:set_positionObject()  --change position
    --self.arrDisk[d].arrCell[b]:set_positionCompleteObject(-10,-10)
   -- print(self.arrCell[d][b].object.object:name())
   end
  end
  for a=1,8 do
    self.arrDisk[1].arrCell[a]:set_positionCompleteObject(-4,-4)
    self.arrDisk[2].arrCell[a]:set_positionCompleteObject(-2,-2)
    self.arrDisk[3].arrCell[a]:set_positionCompleteObject(-9,-9)
   end
  self.emptySfere=p01_l03_z04_sphere()
  self.emptySfere.object=scene:object('empty01')
 -- print(self.emptySfere.object:name())
  self.emptySfere.type='empty'
  self.emptySfere.tween=tweener_mod()
 -- self.emptySfere.tween.posSave=false

  self.emptyCell=p01_l03_z04_cell()
  self.emptyCell.type='empty'
  self.emptyCell.object=self.emptySfere
  self.emptyCell.position=Position(self.arrDisk[3].object:dimensions():lx()/2+self.circPos[3]:x()-20,
                                  self.arrDisk[3].object:dimensions():ly()/2+self.circPos[3]:y()-20)
  self.emptyCell:set_positionObject()


   for e=1,3 do
   self.arrDisk[e].object:set_angle(0, self.arrDisk[e].senter, true)
   self.arrDisk[e].selObject:set_angle(0, self.arrDisk[e].senter, true)
   self.arrDisk[e].object:set_position(self.circPos[e],true)
   self.arrDisk[e].selObject:set_position(self.circPos[e],true)
  end

  self:goEffect()
 end


 -- print(self.emptyCell.object.object:name())
self.initOne=false
self.isLoad=false
end

function p04_l03_m02_game01:on_lb_down()
  local scene=self:scene()
  local selected=scene:overed_object()
  if selected then
    if self:completeMove() then                              --2
   --  print(selected:name())
    if selected:name()==self.emptyCell.object.object:name() and selected:name()~='empty01'  then
    -- print(selected:name())
     if self:resolutionEmpty() then                        --4
      self:moveSphere(0,0)
      self:completeGame()
      self:hideEff()
      self.effgoOne=true
       self.greenSelected:set_position(Position(-100,-100))
       self.blueSelected:set_position(Position(-100,-100))
       self.redSelected:set_position(Position(-100,-100))
       scene:play_sound('g_click_sfere')
      return
     end
    end

    for i=1,#self.arrDisk do
      if selected:name()==self.arrDisk[i].object:name() then

       self.arrDisk[i].tween:setParamsRotate(self.arrDisk[i].object, self.speedDisk,self.angel,
                      self.arrDisk[i].direction,Position(self.arrDisk[i].object:dimensions():lx()/2,self.arrDisk[i].object:dimensions():ly()/2))

        self.arrDisk[i].tweenSel:setParamsRotate(self.arrDisk[i].selObject, self.speedDisk,self.angel,
                      self.arrDisk[i].direction,Position(self.arrDisk[i].object:dimensions():lx()/2,self.arrDisk[i].object:dimensions():ly()/2))
       self.arrDisk[i]:rotateSfere(self.speedDisk,self.angel,self.arrDisk[i].direction)
       self.arrDisk[i]:exchangeSfere()
       self:completeGame()
       self:hideOnDiskEff(i)
       self.effgoOne=true
        self.greenSelected:set_position(Position(-100,-100))
       self.blueSelected:set_position(Position(-100,-100))
       self.redSelected:set_position(Position(-100,-100))
       scene:play_sound('g_click_disk')
       return
      end
    end
   --
    for j=1,#self.arrDisk do
     for k=1,#self.arrDisk[j].arrCell do
       if selected:name()==self.arrDisk[j].arrCell[k].object.object:name() and selected:name()~='empty01'  then

        if self:resolutionMoveSphere(j,k) then            --3
        -- print(self.arrDisk[j].arrCell[k].object.position:x())
          self:moveSphere(j,k)
          self:completeGame()
          self:hideEff()
          self.effgoOne=true
          self.greenSelected:set_position(Position(-100,-100))
          self.blueSelected:set_position(Position(-100,-100))
          self.redSelected:set_position(Position(-100,-100))
          scene:play_sound('g_click_sfere')
          return
        end
       end
     end
    end

   end
  end
          self.greenSelected:set_position(Position(-100,-100))
       self.blueSelected:set_position(Position(-100,-100))
       self.redSelected:set_position(Position(-100,-100))
end

function p04_l03_m02_game01:on_mouse_move()
  local scene=self:scene()
  local selected=scene:overed_object()
 if selected then
   for i=1,#self.arrDisk do
  if self:completeMove() then
    if selected:name()==self.arrDisk[i].object:name() then
     self.arrDisk[i].selObject:show()
    else
     self.arrDisk[i].selObject:hide()
    end
    end
  if self:completeMove() then
   if selected:name()==self.emptyCell.object.object:name() then
      --print(self.emptyCell.object.object.type)
      if self.emptyCell.object.type=='green'then
        self.greenSelected:set_position(Position(self.emptyCell.position:x()-4,self.emptyCell.position:y()-4),true)
        return
       else
       end
       if self.emptyCell.object.type=='blue'then
        self.blueSelected:set_position(Position(self.emptyCell.position:x()-2,self.emptyCell.position:y()-2),true)
        return
       end
       if self.emptyCell.object.type=='red'then
        self.redSelected:set_position(Position(self.emptyCell.position:x()-9,self.emptyCell.position:y()-9),true)
       return
       end
      else
       self.greenSelected:set_position(Position(-100,-100))
       self.blueSelected:set_position(Position(-100,-100))
       self.redSelected:set_position(Position(-100,-100))
      end
  end


    for j=1,#self.arrDisk[i].arrCell do
    if self:completeMove() then
     if selected:name()==self.arrDisk[i].arrCell[j].object.object:name() then
      if self.arrDisk[i].arrCell[j].object.type=='green'then
        self.greenSelected:set_position(Position(self.arrDisk[i].arrCell[j].position:x()-4,self.arrDisk[i].arrCell[j].position:y()-4),true)
        return
       else
       end
       if self.arrDisk[i].arrCell[j].object.type=='blue'then
        self.blueSelected:set_position(Position(self.arrDisk[i].arrCell[j].position:x()-2,self.arrDisk[i].arrCell[j].position:y()-2),true)
        return
       end
       if self.arrDisk[i].arrCell[j].object.type=='red'then
        self.redSelected:set_position(Position(self.arrDisk[i].arrCell[j].position:x()-9,self.arrDisk[i].arrCell[j].position:y()-9),true)
       return
     end
      else
       self.greenSelected:set_position(Position(-100,-100))
       self.blueSelected:set_position(Position(-100,-100))
       self.redSelected:set_position(Position(-100,-100))
     end
        self.greenSelected:show(1)
       self.blueSelected:show(1)
       self.redSelected:show(1)
    end
    end
   end

 else
  for j=1,#self.arrDisk do
   self.arrDisk[j].selObject:hide()
  end

 end
end

function p04_l03_m02_game01:on_update(dt)
  for i=1,#self.arrCell do
    self.arrDisk[i].tween:update(dt)
    self.arrDisk[i].tweenSel:update(dt)
    for j=1,#self.arrCell[i] do
     self.arrDisk[i].arrCell[j].object.tween:update(dt)
     self.arrDisk[i].arrCell[j].tween:update(dt)
    end
  end
  self.tweenDisk:update(dt)
   self.tweenSen:update(dt)
  self.emptyCell.object.tween:update(dt)

  if self:completeMove() then
   if self.effgoOne then
    self:goEffect()
    self.effgoOne=false
   end
  end

  if self.timeOne then
   if self.moveCompleteOne then
    if self:completeMove() then
      self.moveCompleteOne=false
      self:scene():play_sound('g_complete')
      self:move_finish()
    end
   end
  end

  if self.timeOne then
    if self:completeMove() then
     self.timeOne=false
     self.greenSelected:hide()
     self.blueSelected:hide()
     self.redSelected:hide()
     self.diskSenters:hide()
     self:scene():execute_action('complete_game01')
    end
  end

end

-- my function

--1
function p04_l03_m02_game01:getPositionSfere(i,senter,radius)
 local x
 local y

 x= radius*math.sin(math.pi*45*(i-1)/180)+senter:x()-20
 y= radius*math.cos(math.pi*45*(i-1)/180)+senter:y()-20
 return Position(x,y)
end
--2
function p04_l03_m02_game01:completeMove()
  for i=1,#self.arrCell do
    if not self.arrDisk[i].tween:completerot() then
     -- print('1')
     return false
    end
    for j=1,#self.arrCell[i] do
     if not self.arrDisk[i].arrCell[j].object.tween:getCompleteMoveGlob() then

     -- print(self.arrDisk[i].arrCell[j].object.object:name())
      return false
     end

     if not self.arrDisk[i].arrCell[j].object.tween:completerot() then
      -- print('3')
      return false
     end
    end

  end

  if not self.emptyCell.object.tween:getCompleteMoveGlob() then
    -- print('4')
    return false
  end

  if not self.emptyCell.object.tween:completerot() then
  -- print('5')
   return false
  end
  return true
end
--3
function p04_l03_m02_game01:resolutionMoveSphere(indexDisk,indexCell)

 if self.arrDisk[indexDisk-1] then
  if self.arrDisk[indexDisk-1].arrCell[indexCell].object.object:name()=='empty01' then
    self.directionMoveSphere=-1
    return true
  end
 end

 if self.arrDisk[indexDisk+1] then
  if self.arrDisk[indexDisk+1].arrCell[indexCell].object.object:name()=='empty01' then
   self.directionMoveSphere=1
   return true
  end
 end

 if indexDisk+1>#self.arrDisk then
  --print(self.emptyCell.object.object:name())
  if self.emptyCell.object.object:name()=='empty01' then
   self.directionMoveSphere=0
   return true
  end
 end
 return false
end
--4
function p04_l03_m02_game01:resolutionEmpty()
 for i=1,#self.arrDisk[#self.arrDisk].arrCell do
  if self.arrDisk[#self.arrDisk].arrCell[i].object.object:name()=='empty01' then
   self.emptyIndex=i
   self.directionMoveSphere=5
   return true
  end
 end
 return false
end

--5
function p04_l03_m02_game01:moveSphere(indexDisc,indexCell)
 local sphere=nil
 if self.directionMoveSphere==1 then
  sphere=self.arrDisk[indexDisc].arrCell[indexCell].object
  self.arrDisk[indexDisc].arrCell[indexCell].object=self.arrDisk[indexDisc+1].arrCell[indexCell].object
  self.arrDisk[indexDisc+1].arrCell[indexCell].object=sphere

  self.arrDisk[indexDisc+1].arrCell[indexCell].object.tween:getsParmsMove(self.arrDisk[indexDisc+1].arrCell[indexCell].object.object,
                                                                          self.speedSfere,self.arrDisk[indexDisc].arrCell[indexCell].position,
                                                                          self.arrDisk[indexDisc+1].arrCell[indexCell].position)

 else
  if self.directionMoveSphere==-1 then
   sphere=self.arrDisk[indexDisc].arrCell[indexCell].object
   self.arrDisk[indexDisc].arrCell[indexCell].object=self.arrDisk[indexDisc-1].arrCell[indexCell].object
   self.arrDisk[indexDisc-1].arrCell[indexCell].object=sphere

   self.arrDisk[indexDisc-1].arrCell[indexCell].object.tween:getsParmsMove(self.arrDisk[indexDisc-1].arrCell[indexCell].object.object,
                                                                            self.speedSfere,self.arrDisk[indexDisc].arrCell[indexCell].position,
                                                                             self.arrDisk[indexDisc-1].arrCell[indexCell].position)

  else

   if self.directionMoveSphere==0 then
    --print('hy')
   -- print(self.arrDisk[indexDisc].arrCell[indexCell].object.position:x())
     sphere=self.arrDisk[indexDisc].arrCell[indexCell].object
     self.arrDisk[indexDisc].arrCell[indexCell].object=self.emptyCell.object
     self.emptyCell.object=sphere
--     print(self.arrDisk[indexDisc].arrCell[indexCell].position:x())
--     print(self.emptyCell.position:x())
--     print(self.emptyCell.object.object:name())
    self.emptyCell.object.tween:getsParmsMove(self.emptyCell.object.object,self.speedSfere,
                                                self.arrDisk[indexDisc].arrCell[indexCell].position,self.emptyCell.position)
   else
    if self.directionMoveSphere==5 then
     sphere=self.arrDisk[#self.arrDisk].arrCell[self.emptyIndex].object
     self.arrDisk[#self.arrDisk].arrCell[self.emptyIndex].object=self.emptyCell.object
     self.emptyCell.object=sphere

     self.arrDisk[#self.arrDisk].arrCell[ self.emptyIndex].object.tween:getsParmsMove(self.arrDisk[#self.arrDisk].arrCell[ self.emptyIndex].object.object,
                                                                                       self.speedSfere,self.emptyCell.position,
                                                                                       self.arrDisk[#self.arrDisk].arrCell[ self.emptyIndex].position)

    end
   end
  end
 end
end
--6
function p04_l03_m02_game01:completeGame()
 local scene=self:scene()
 if self.exeOne then
  for i=1,#self.arrCell do
    for j=1,#self.arrCell[i] do
     if self.arrDisk[i].arrCell[j].type~=self.arrDisk[i].arrCell[j].object.type then
      return
     end
    end
  end
 end
 self.exeOne=false
 self.timeOne=true

 --scene:execute_action('complete_game01')
end
--7
function p04_l03_m02_game01:goEffect()
  for i=1,#self.arrCell do
    for j=1,#self.arrCell[i] do
     if self.arrDisk[i].arrCell[j].type==self.arrDisk[i].arrCell[j].object.type then
      self.arrDisk[i].arrCell[j].objectComplete:show(1)
     else
      self.arrDisk[i].arrCell[j].objectComplete:hide(0.2)
     end
    end
  end
end
--8
function p04_l03_m02_game01:hideEff()
   for i=1,#self.arrCell do
    for j=1,#self.arrCell[i] do
      if self.arrDisk[i].arrCell[j].type==self.arrDisk[i].arrCell[j].object.type then

      else
        self.arrDisk[i].arrCell[j].objectComplete:hide()
      end
    end
   end
end
--9
function p04_l03_m02_game01:hideOnDiskEff(i)

    for j=1,#self.arrCell[i] do
      self.arrDisk[i].arrCell[j].objectComplete:hide()
    end

end
--10
function p04_l03_m02_game01:move_finish()
 self.tweenSen:getsParmsMove(self.diskSenters,150,self.diskSenters:position(),Position(self.diskSenters:position():x()-800,self.diskSenters:position():y()))
 self.tweenDisk:getsParmsMove(self.backDisk,150,self.backDisk:position(),Position(self.backDisk:position():x()-800,self.backDisk:position():y()))
 self:scene():reset_mark('p04_l03_m02_game_start')
 --p04_l03_m02_game_start
 --self.backDisk
 for i=1,#self.arrDisk do
     self.arrDisk[i].tween:getsParmsMove(self.arrDisk[i].object,150,self.arrDisk[i].object:position(),Position(self.arrDisk[i].object:position():x()-800,self.arrDisk[i].object:position():y()))
    for j=1,#self.arrDisk[i].arrCell do
     local pos1=self.arrDisk[i].arrCell[j].object.object:position()
     local pos2=self.arrDisk[i].arrCell[j].objectComplete:position()
     self.arrDisk[i].arrCell[j].object.tween:getsParmsMove(self.arrDisk[i].arrCell[j].object.object,150,pos1,Position(pos1:x()-800,pos1:y()))
     self.arrDisk[i].arrCell[j].tween:getsParmsMove(self.arrDisk[i].arrCell[j].objectComplete,150,pos2,Position(pos2:x()-800,pos2:y()))
    end
   end
end

function p04_l03_m02_game01:save(os)
  os:save(0) -- version
  os:save(true)
  for d=1,3 do
   os:save(self.circPos[d])
  end
end

function p04_l03_m02_game01:load(is)
  local ver = is:load()
  self.isLoad = is:load()
    for d=1,3 do
    self.circPos[d]= is:load()
  end
end