class 'game_puzle' (Script)

function game_puzle:__init() super()
 self.height=4
 self.width=4
 self.current=nil
 self.active =nil
end

function game_puzle:on_attach()
 self:MyInit()

end

function game_puzle:on_lb_down()
 local scene=self:scene()
 local selection =scene:overed_object()

 if selection then

  for i=1,#self.arr_object do
   if selection:name()==self.arr_MyObject[i]['object']:name() then
    if self.current==nil then
     self.current=self.arr_MyObject[i]
     self.active:show()
     self.active:set_position(Position(self.current['object']:position():x()-10,self.current['object']:position():y()-14))
     scene:play_sound('g_select_el')
    else
     self:changePos(self.current,self.arr_MyObject[i])
     self:mySetPosition(self.current)
     self:mySetPosition(self.arr_MyObject[i])
     self.current=nil
     self.active:hide()
     self:complete_or()
     self:scene():play_sound('g_exchange_el')
    end
   end
  end

 end
end

function game_puzle:MyInit()
 local scene=self:scene()
 local part='minigame_part'

 self.arr_object={}
 self.arr_position={}
 self.arr_data_complete={{x=1,y=1},{x=1,y=2},{x=1,y=3},{x=1,y=4},{x=2,y=1},{x=2,y=2},{x=2,y=3},{x=2,y=4},{x=3,y=1},{x=3,y=2},{x=3,y=3},{x=3,y=4},{x=4,y=1},{x=4,y=2},{x=4,y=3},{x=4,y=4}}
 self.arr_data_start={{x=1,y=1},{x=1,y=3},{x=2,y=1},{x=2,y=3},{x=3,y=1},{x=3,y=3},{x=4,y=1},{x=4,y=3},{x=4,y=4},{x=4,y=2},{x=3,y=4},{x=3,y=2},{x=2,y=4},{x=2,y=2},{x=1,y=4},{x=1,y=2}}
 self.arr_MyObject={}


 for i=1,16 do
  local arrPps={};
  self.arr_object[i]=scene:object(part..i)
  arrPps={}
  rawset(self.arr_MyObject,i,arrPps)
  rawset(arrPps,'data',self.arr_data_start[i])
  rawset(arrPps,'object',self.arr_object[i])
 end
 self.arr_position=self:createTablePos(self.arr_object[1],self.height,self.width)
 for j=1,16 do
  self:mySetPosition(self.arr_MyObject[j])
 end

 self.active=scene:object('minigame_active_el')
end

function game_puzle:createTablePos(object,height,width)
 local arrPos={}

 local xPos=object:position():x()
 local yPos=object:position():y()

 local xDem=object:dimensions():lx()
 local yDem=object:dimensions():ly()

 for i=1,height do
  local col={}
  rawset(arrPos,i,col)
  for j=1,width do
   rawset(col,j,Position(xPos+xDem*(j-1),yPos+yDem*(i-1)))
  end
 end
 return arrPos

end

function game_puzle:mySetPosition(myObject)
 local pos
 pos=self.arr_position[myObject['data'].x][myObject['data'].y]
 myObject['object']:set_position(pos)
end

function game_puzle:changePos(myObject1,myObject2)
 local posStartOb1=myObject1['data']
 local posStartOb2=myObject2['data']

 local posFinishOb1=posStartOb2
 local posFinishOb2=posStartOb1

 myObject1['data']=posFinishOb1
 myObject2['data']=posFinishOb2

end

function game_puzle:complete_or()
 local count=1
 local scene=self:scene()
 for i=1,16 do
   if self.arr_MyObject[i]['data'].x==self.arr_data_complete[i].x  and self.arr_MyObject[i]['data'].y==self.arr_data_complete[i].y then
    count=count+1
   end
 end
  if count>16 then
     -- self:scene():play_sound('g_complete')
    scene:execute_action('complite_game')
  end
end


