class 'p03_l07_m01_game1' (Script)

function p03_l07_m01_game1:__init()super()
 self.moveObject={}
 for i=1,4 do
  self.moveObject[i]=tweener()
 end

 self.buttonObject={}
 self.buttonOverObject={}
 self.point_grayObject={}
 self.point_redObject={}
 self.point_redOverObject={}

self.buttonAr={}
self.buttonArOver={}
self.point={}
self.pointOver={}

self.randPosPoint={}
self.randIndex={}
 --PRAMS

 --self.completeCoord={{1,0},{2,0},{0,1},{1,1},{2,1},{0,2},{1,2},{2,2}}
   self.completeCoord={{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2} }
 -- MEMORY
 self.incialOne=true
 self.arrStartPos={}
 self.completeMove=true

end

function p03_l07_m01_game1:on_attach()
 local scene = self:scene()
 if self.incialOne then

  for i=1,8 do
   self.buttonObject[i]=scene:object('button_rotate0'..i)
   self.buttonOverObject[i]=scene:object('button_rotate0'..i..'_mouseover')
  end

  for j=1,7 do
   self.point_grayObject[j]=scene:object('button_gray0'..j)
  end

  for k=1,8 do
   self.point_redObject[k]=scene:object('button_red0'..k)
   self.point_redOverObject[k]=scene:object('button_red0'..k..'_fx')
  end
  self:createScriptElement()
 end
 self.incialOne=false
end

function p03_l07_m01_game1:createScriptElement()
 local button=p03_l07_m01_game1_Button()
 local point_gray=p03_l07_m01_game1_Point()
 local point_red=p03_l07_m01_game1_Point()

 for i=1,7 do

  point_gray=p03_l07_m01_game1_Point()

  self.point[i]=point_gray

  self.point[i].object=self.point_grayObject[i]
  self.point[i].type=-1
 end

 for s=1,8 do
  button=p03_l07_m01_game1_Button()
  point_red=p03_l07_m01_game1_Point()
  self.buttonArOver[s]=button
  self.buttonArOver[s].object= self.buttonOverObject[s]
  self.pointOver[s]= point_red
  self.pointOver[s].object= self.point_redOverObject[s]
 end

 for j=8,15 do
  button=p03_l07_m01_game1_Button()
  point_red=p03_l07_m01_game1_Point()
  self.buttonAr[j-7]=button
  self.point[j]=point_red
  self.buttonAr[j-7].object=self.buttonObject[j-7]
  self.point[j].object= self.point_redObject[j-7]
  self.point[j].type=1
 end
 -- BUTTON
 self.buttonAr[1].coord={0,0}
 self.buttonAr[1].type=1

 self.buttonAr[2].coord={0,1}
 self.buttonAr[2].type=1

 self.buttonAr[3].coord={0,2}
 self.buttonAr[3].type=1

 self.buttonAr[4].coord={1,2}
 self.buttonAr[4].type=1

 self.buttonAr[5].coord={1,1}
 self.buttonAr[5].type=-1

 self.buttonAr[6].coord={1,0}
 self.buttonAr[6].type=-1

 self.buttonAr[7].coord={2,0}
 self.buttonAr[7].type=-1

 self.buttonAr[8].coord={2,1}
 self.buttonAr[8].type=-1
 -- POINT
 for s=1,4 do
  self.point[s].coord={s-1,0 }
  self.point[s+4].coord={s-1,1}
  self.point[s+8].coord={s-1,2}
 end
 self.point[13].coord={0,3}
 self.point[14].coord={1,3}
 self.point[15].coord={2,3}
 --print(self.point[1].object:name())
  -- print(self.point[9].object:name())

 for h=1,15 do
   self.randPosPoint[h]= self.point[h].coord
 end
 self:randPointPos()

 for p=1,15 do
  self.point[p].object:set_position(self.point[p]:calculatePosition(),true)
 end
 --self:start_game1()
end

function p03_l07_m01_game1:start_game1()
 local count=1
 for j=1,#self.point do
  if self.point[j].type==1 then
   self.pointOver[count].coord=self.point[j].coord
   self.pointOver[count].object:set_position(Position(self.pointOver[count]:calculatePosition():x()-5,self.pointOver[count]:calculatePosition():y()-5),true)
   --self.pointOver[count].object:show(1)
   count=count+1
  end
 end


end

function p03_l07_m01_game1:randPointPos()
 local rand=1
 local randArr={}
 for i=1,15 do
  randArr[i]=i
 end
 for j=1,15 do
  rand=math.floor(math.random()*#randArr)+1
   table.insert(self.randIndex,randArr[rand])
  table.remove(randArr,rand)
 end
  for n=1,15 do
  self.point[n].coord =self.randPosPoint[self.randIndex[n]]
 end
  if self:completeGame(true) then
   self:randPointPos()
  end
end

function p03_l07_m01_game1:on_lb_down()
 local scene=self:scene()
 local selected=scene:overed_object()
 local arrPoint_in_button={}
 local activePoint={}
 local activePointComplete={}
 if selected then
  for i=1,#self.buttonAr do
   if self.buttonAr[i].object:name()==selected:name() then
     if self.completeMove then
      --print(self.buttonAr[i].object:name())
      self.completeMove=true
      arrPoint_in_button=self.buttonAr[i]:_getCoordPoint()
     -- print(arrPoint_in_button[4][1])

      activePoint=self:generate_active_point(arrPoint_in_button)
       for t=1,#activePoint do
        self.arrStartPos[t]={}
        self.arrStartPos[t][1]=activePoint[t]:calculatePosition():x()
        self.arrStartPos[t][2]=activePoint[t]:calculatePosition():y()

       end

      --print(activePoint[2].coord[1])
      activePointComplete=self:movePoint(activePoint,self.buttonAr[i].type)
     -- print(self.arrStartPos[4][1])
     -- print(activePointComplete[4]:calculatePosition():x())

      for j=1,#self.moveObject do
       self.moveObject[j]:getsParmsMove(activePoint[j].object,500,Position(self.arrStartPos[j][1],self.arrStartPos[j][2]),activePointComplete[j]:calculatePosition())
      end
      scene:play_sound('g_move_point')
 --     for j=1,#activePointComplete do
 --      activePointComplete[j].object:set_position(activePointComplete[j]:calculatePosition())
 --     end
    end
   end
  end
 end
 self:completeGame(false)
end

--function p03_07_m01_game1:on_mouse_move()
--local scene =self:scene()
--local selected=scene:overed_object()
--for i=1,#self.buttonAr do
-- if selected then
--   for i=1,#self.buttonAr do
--    if self.buttonAr[i].object:name()==selected:name() then
--     self.buttonArOver[i].object:show()
--    else
--     self.buttonArOver[i].object:hide()
--    end
--   end
-- end
--end
--end

function p03_l07_m01_game1:generate_active_point(ar_pos)
 local activePoint={}
  --print(ar_pos[1][1])
 for i=1,#self.point do
  if self.point[i].coord[1]==ar_pos[1][1] and self.point[i].coord[2]==ar_pos[1][2] then
   activePoint[1]=self.point[i]
  end

  if self.point[i].coord[1]==ar_pos[2][1] and self.point[i].coord[2]==ar_pos[2][2] then
   activePoint[2]=self.point[i]
  end

  if self.point[i].coord[1]==ar_pos[3][1] and self.point[i].coord[2]==ar_pos[3][2] then
   activePoint[3]=self.point[i]
  end

  if self.point[i].coord[1]==ar_pos[4][1] and self.point[i].coord[2]==ar_pos[4][2] then
   activePoint[4]=self.point[i]
  end
 end
 return activePoint

end
function p03_l07_m01_game1:on_mouse_move()
   local scene =self:scene()
  local selected=scene:overed_object()
  for i=1,#self.buttonAr do
  if selected then
     for i=1,#self.buttonAr do
      if self.buttonAr[i].object:name()==selected:name() then
       self.buttonArOver[i].object:show()
      end
     end
  else
   self.buttonArOver[i].object:hide()
  end

 end
end

function p03_l07_m01_game1:movePoint(arrActivePoint,direction)
 if direction ==1 then
  arrActivePoint[1].coord[2]=arrActivePoint[1].coord[2]+1
  arrActivePoint[2].coord[1]=arrActivePoint[2].coord[1]-1
  arrActivePoint[3].coord[1]=arrActivePoint[3].coord[1]+1
  arrActivePoint[4].coord[2]=arrActivePoint[4].coord[2]-1
  return arrActivePoint
 else
  arrActivePoint[1].coord[1]=arrActivePoint[1].coord[1]+1
  arrActivePoint[2].coord[2]=arrActivePoint[2].coord[2]+1
  arrActivePoint[3].coord[2]=arrActivePoint[3].coord[2]-1
  arrActivePoint[4].coord[1]=arrActivePoint[4].coord[1]-1
  return arrActivePoint
 end
end

function p03_l07_m01_game1:on_update(dt)
   self.completeMove=true
   for j=1,#self.moveObject do
      self.moveObject[j]:update(dt)
      self.completeMove=self.completeMove and self.moveObject[j].completeMoveGlob
   end
end
function p03_l07_m01_game1:completeGame(flag)
 local arrRed={}
 local count=0
 for i=1,#self.point do
  if self.point[i].type==1 then
   table.insert(arrRed,self.point[i])
  end
 end
 --print(#arrRed)
 for j=1,#self.completeCoord do
   for k=1,#arrRed do
   -- print(arrRed[k].coord[1])
    --print(self.completeCoord[k][1])
   -- print(arrRed[k].coord[2])
    --print(self.completeCoord[k][2])
    if (arrRed[k].coord[1]==self.completeCoord[j][1]) and (arrRed[k].coord[2]==self.completeCoord[j][2]) then
    -- print('hy')
     count=count+1
    end
   end
 end
 if count>7 then
  if flag then
   return true
  else
  self:scene():enable_input(false)
   for s=1,8 do

     self.buttonArOver[s].object:hide(1)
     self.buttonAr[s].object:hide(1)
   end
  self:scene():play_sound('g_complete')
   self:scene():execute_action('complete_minigame1')
  end
 end
 return false
 --print(count)   --self.completeCoord
end
