class 'GameClock' (Script)

function GameClock:__init() super()
    self.items   ={ {id=1},
                    {id=4},
                    {id=5},
                    {id=2},
                    {id=3}}

--    self.centers = { {id=1, pos=Position(331, 405), item=3},
--                    {id=2, pos=Position(330, 225), item=1},
--                    {id=3, pos=Position(500, 135), item=5},
--                    {id=4, pos=Position(676, 226), item=2},
--                    {id=5, pos=Position(675, 405), item=4},
--                    {id=6, pos=Position(501, 515), item=0} }
self.centers = { {id=1, pos=Position(331, 405), item=3},
    {id=4, pos=Position(676, 226), item=1},
    {id=5, pos=Position(675, 405), item=5},
    {id=2, pos=Position(330, 225), item=2},
    {id=3, pos=Position(500, 135), item=4},
    {id=6, pos=Position(501, 515), item=0} }

    self.centers = { {id=1, pos=Position(331, 405), item=3},
                     {id=4, pos=Position(676, 226), item=1},
                     {id=5, pos=Position(675, 405), item=5},
                    {id=2, pos=Position(330, 225), item=2},
                    {id=3, pos=Position(500, 135), item=4},
                    {id=6, pos=Position(501, 515), item=0} }

    self.emptyCenter = self.centers[6]
    self.canSelect = true
    self.needUpdate = false
    self.movingItem  = nil
    self.itirationOne=true
    self.arEf={}
end

function GameClock:on_attach()
    local scene = self:scene()
    local item
    local center

    if self.itirationOne then


     for i=1,#self.centers do
         item = self.centers[i]
         rawset(item, 'object', scene:object('center'..i))
         item.pos=Position(item.object:position():x()+item.object:dimensions():lx()/2,item.object:position():y()+item.object:dimensions():ly()/2)

     end

     for i=1,#self.items do
         item = self.items[i]
         rawset(item, 'object', scene:object('item'..i))
         rawset(item, 'target', nil)

         --item.object:set_position(self.centers[self.centers[i].item].pos,true)
     end

     for i=1,#self.items do
         self.items[self.centers[i].item].object:set_position(Position(self.centers[i].pos:x()-self.items[self.centers[i].item].object:dimensions():lx()/2,
                                                                        self.centers[i].pos:y()-self.items[self.centers[i].item].object:dimensions():ly()/2),true)
     end

     for k=1,6 do
       self.arEf[k]=scene:object('ef0'..k)
     end
    end

    self.itirationOne=false
end

function GameClock:on_lb_down()
    if self.canSelect == false then
       return
    end

    local scene     = self:scene()
    local selection = scene:overed_object()
    local selectedCenter

    if selection then
        for i=1,#self.centers do
          if selection:name() == self.centers[i].object:name() then
             selectedCenter = self.centers[i]

             break
          end
        end
    else
      return
    end

    if selectedCenter.item == 0 or
      (self.emptyCenter.id ~= 1 or selectedCenter.id ~= 6) and
      (self.emptyCenter.id ~= 6 or selectedCenter.id ~= 1) and
      (math.abs(self.emptyCenter.id - selectedCenter.id)~=1 and math.abs(self.emptyCenter.id - selectedCenter.id)~=3)
    then

      return
    end

    if math.abs(self.emptyCenter.id - selectedCenter.id)==1 or (self.emptyCenter.id == 1 and selectedCenter.id == 6) or (self.emptyCenter.id == 6 and selectedCenter.id == 1) then
      --  print('1')
        scene:play_sound('g_no_edge')
    end

    if math.abs(self.emptyCenter.id - selectedCenter.id)==3   then
        scene:play_sound('g_edge')
      -- print('2')
    end


    self.movingItem = self.items[selectedCenter.item]

    self.movingItem.target = Position( self.emptyCenter.pos:x()-self.movingItem.object:dimensions():lx()/2,
                                       self.emptyCenter.pos:y()-self.movingItem.object:dimensions():ly()/2)
    self.emptyCenter.item =  selectedCenter.item
    selectedCenter.item = 0

    self.emptyCenter = selectedCenter

    self.canSelect = false
    self.needUpdate = true


end

function GameClock:on_mouse_move()
 local scene     = self:scene()
 local selection = scene:overed_object()

 if selection then
    for i=1,#self.centers do
        if selection:name() == self.centers[i].object:name() then
          if self.centers[i].item~=0 then
            self.arEf[i]:show()
          end
         break
       end
    end
 else
  for k=1,6 do
   self.arEf[k]:hide()
  end
 end

end

function GameClock:on_update(dt)
   if self.needUpdate then

      local dx    = self.movingItem.target:x() - self.movingItem.object:position():x()
      local dy    = self.movingItem.target:y() - self.movingItem.object:position():y()
      local dist  = math.sqrt (dx * dx + dy * dy) / 2;

      local vX = 0
      local vY = 0
      
      if(dist ~= 0) then
				vX = dx * 2 / dist
				vY = dy * 2 / dist
      end

      if dist <= 2 or math.abs(dist) - 2 < 0 then
          self.canSelect = true
          self.needUpdate = false

          local isDone = true

           wnd_clear()

          for i=1,#self.centers-1 do
            --wnd_print(self.items[self.centers[i].item].id ..' '.. self.centers[i].id)
            if self.centers[i].item == 0 or self.items[self.centers[i].item].id ~= self.centers[i].id then
              if self.centers[i].item == 0 then
                wnd_print('0 '..self.centers[i].id)
              else
                wnd_print(self.items[self.centers[i].item].id ..' '.. self.centers[i].id)
              end
               isDone = false
               -- break
            end
          end


          if isDone then
              self:scene():play_sound('g_complete')
              self:scene():execute_action('complete_GameClock')
              self:remove()
          end

      end

      self.movingItem.object:set_position(Position(self.movingItem.object:position():x()+vX, self.movingItem.object:position():y()+vY),true)

   end
end