class 'starGame' (Script)
function starGame:__init() super()
-- self.radius=100
-- self.countNumberlocal=0
-- self.arControlNumber={25,-33,73,-69}
-- self.positionNumber2=Position(645, 549)
-- self.positionNumber3=Position(725, 549)
-- self.countNumberAbs=0
-- self.arrPosition={Position(524,311),Position(502,241),Position(506,163),Position(520,93),Position(616,78),Position(608,167),
--                   Position(613,243),Position(620,323),Position(709,316),Position(724,240),Position(723,163),Position(709,85) }
-- self.arrPosSky={{100,-100},{20,20},{45,-85},{300,-50},{5,-25},{60,9},{170,-120},{40,-60},{5,5},{88,-400}}
--  self.arrFixet={{4,5,7,10},{12,10,1},{5,6,7,1,9},{4,12,1,9} }
--
-- self.xStar=0
-- self.yStar=0
--
-- self.arrRendomPosition={}
-- self.countLocal=0
-- self.countAbs=0
--
--
-- self.finish_x =1.1
-- self.finish_y =1.1
-- self.time=0
-- self.rand=false
-- self.executeone=1
-- self.countFixet=1
-- self.countZnak=1
--
-- self.exeOne=true

end

function starGame:on_attach()
    self.sound= self:scene():sound('g_click')
    self.radius=100
    self.countNumberlocal=0
    self.arControlNumber={25,-33,73,-69}
    self.positionNumber2=Position(645, 549)
    self.positionNumber3=Position(725, 549)
    self.countNumberAbs=0
    self.arrPosition={Position(524,311),Position(502,241),Position(506,163),Position(520,93),Position(616,78),Position(608,167),
        Position(613,243),Position(620,323),Position(709,316),Position(724,240),Position(723,163),Position(709,85) }
    self.arrPosSky={{100,-100},{20,20},{45,-85},{300,-50},{5,-25},{60,9},{170,-120},{40,-60},{5,5},{88,-400}}
    self.arrFixet={{4,5,7,10},{12,10,1},{5,6,7,1,9},{4,12,1,9} }

    self.xStar=0
    self.yStar=0

    self.arrRendomPosition={}
    self.countLocal=0
    self.countAbs=0


    self.finish_x =1.1
    self.finish_y =1.1
    self.time=0
    self.rand=false
    self.executeone=1
    self.countFixet=1
    self.countZnak=1

    self.exeOne=true




  --self.countLocal=0

 local scene= self:scene()
 local star='star'
 local number='number'
 local number2='number2'
 local arrDataStar={}
 local arrDataNumber={}

 self.plus=scene:object('plus')
 self.minus=scene:object('minus')
    self.minus:hide()

 self.buttonUp1=scene:object('buttonup1')
 self.buttonDaun1=scene:object('buttondaun1')

 self.buttonUp2=scene:object('buttonup2')
 self.buttonDaun2=scene:object('buttondaun2')

 self.buttonUp3=scene:object('buttonup3')
 self.buttonDaun3=scene:object('buttondaun3')

 self.arrButton={self.buttonUp1,self.buttonDaun1,self.buttonUp2,self.buttonDaun2,self.buttonUp3,self.buttonDaun3}

 self.object=scene:object('nebo')



 self.arrStar={}
 self.arrNumber2={}
 self.arrNumber3={}


 for i=1,10 do
  arrDataNumber={}

  rawset(self.arrNumber2,i,arrDataNumber)
  rawset(arrDataNumber,'object',scene:object(number2..10*(i-1)))
  rawset(arrDataNumber,'mass',10*(i-1))
  rawset(arrDataNumber,'order',i)

  arrDataNumber={}
  rawset(self.arrNumber3,i,arrDataNumber)
  rawset(arrDataNumber,'object',scene:object(number..(i-1)))
  rawset(arrDataNumber,'mass',(i-1))
  rawset(arrDataNumber,'order',i)

  self.arrNumber2[i]['object']:set_position(self.positionNumber2,true)
  self.arrNumber3[i]['object']:set_position(self.positionNumber3,true)
  self.arrNumber2[i]['object']:hide()
  self.arrNumber3[i]['object']:hide()
 end

 for j=1,6 do
  arrDataStar={}
  rawset(self.arrStar,j,arrDataStar)
  rawset(arrDataStar,'object',scene:object(star..j))
  rawset(arrDataStar,'order',(j))
  self.arrStar[j]['object']:hide()
 end


 if self.exeOne then
   self.current1=self.plus
 self.current2=self.arrNumber2[1]
 self.current3=self.arrNumber3[1]

  self.current1:show()
  self.current2['object']:show()
  self.current3['object']:show()
  self:changeScy(1,1,1,1)
 end

 --self.changeNumber(0)
 self.exeOne=false
 end



function starGame:on_lb_down()

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

 if selection then
  if selection:name()==self.buttonUp1:name() then
   self:clickPlusOrMinus()
   self.sound:play()
  end

  if selection:name()== self.buttonDaun1:name() then
   self:clickPlusOrMinus()
   self.sound:play()
  end

  if selection:name()==self.buttonUp2:name() then
   self:clickUp2()
   self.sound:play()
  end

  if selection:name()== self.buttonDaun2:name() then
   self:clickDown2()
   self.sound:play()
  end

  if selection:name()==self.buttonUp3:name() then
   self:clickUp3()
   self.sound:play()
  end

  if selection:name()== self.buttonDaun3:name() then
   self:clickDown3()
   self.sound:play()
  end

 end

end

function starGame:clickPlusOrMinus()
 if self.current1:name()=='plus' then
  self.current1:hide()
  self.current1=self.minus
  self.current1:show()
 else
   self.current1:hide()
  self.current1=self.plus
  self.current1:show()
 end
  self:changeNumber(0)
end

function starGame:clickUp2()
  local order= self.current2['order']

  if order==10 then
   order=1
    self:changeNumber(-90)
  else
   order = order+1
    self:changeNumber(10)
  end
  self.current2['object']:hide()
  self.arrNumber2[order]['object']:show()
  self.current2=self.arrNumber2[order]

end

function starGame:clickDown2()
 local order=self.current2['order']

 if order==1 then
  order=10
  self:changeNumber(90)
 else
  order=order-1
  self:changeNumber(-10)
 end

 self.current2['object']:hide()
 self.arrNumber2[order]['object']:show()
 self.current2=self.arrNumber2[order]
end

function starGame:clickUp3()
  local order= self.current3['order']

  if order==10 then
   order=1
   self:changeNumber(-9)
  else
   order = order+1
   self:changeNumber(1)
  end
  self.current3['object']:hide()
  self.arrNumber3[order]['object']:show()
  self.current3=self.arrNumber3[order]


end

function starGame:clickDown3()
   local order=self.current3['order']

 if order==1 then
  order=10
  self:changeNumber(9)
 else
  order=order-1
  self:changeNumber(-1)
 end

 self.current3['object']:hide()
 self.arrNumber3[order]['object']:show()
 self.current3=self.arrNumber3[order]


end

function starGame:changeNumber(direction)

 self.countLocal=self.countLocal+direction
 local count=1
 local countAbs =0

 if direction==0 then
  self.countZnak=self.countZnak*(-1)
 end

 if self.countZnak==1 then
  countAbs=self.countLocal
 else
  countAbs=(-1)*self.countLocal
 end
 -- print(countAbs)
 for i=1,#self.arControlNumber do
  if countAbs==self.arControlNumber[i] then
   for i=1,#self.arrStar do
      self.arrStar[i]['object']:hide()
   end

   self.rand=true
   self.countFixet=i
   self:randSkyPosition()                          --this is change

  else
   count=count+1
  end
 end
 if count>#self.arControlNumber then
  self.rand=false
  for i=1,#self.arrStar do
     self.arrStar[i]['object']:hide()
   end

   self:randSkyPosition()

 end
end

function starGame:randomStar()
 local arrNumberPosition={}
 local countStar
 local posRandStar
 local i=0

 for i=1,12 do
  arrNumberPosition[i]=i
 end

 countStar=2+math.floor(math.random()*5)

 for j=1,countStar do
  posRandStar=math.floor(math.random()*#arrNumberPosition)+1
  self.arrStar[j]['object']:set_position(self.arrPosition[arrNumberPosition[posRandStar]],true)
  table.remove(arrNumberPosition,posRandStar)
  self.arrStar[j]['object']:show(1)
 end

end

function starGame:fixetStar(index)

 for j=1,#self.arrFixet[index] do
  self.arrStar[j]['object']:set_position(self.arrPosition[self.arrFixet[index][j]],true)
  self.arrStar[j]['object']:show(1)
 end

end

function starGame:randSkyPosition()
 local rand=math.floor(math.random()*9)+1
 --print(self.finish_y)
  --print(self.arrPosSky[rand][2])

 if self.finish_x~=self.arrPosSky[rand][1] or self.finish_y~= self.arrPosSky[rand][2] then
  self:changeScy(self.finish_x,self.finish_y,self.arrPosSky[rand][1],self.arrPosSky[rand][2])
  self.executeone=0


 else
  self:randSkyPosition()
 end
end

function starGame:changeScy(start_x,start_y,finish_x,finish_y)
 local scene = self:scene()

 	 self.start_x =start_x
	 self.start_y =start_y
	 self.finish_x =finish_x
	 self.finish_y =finish_y
	 self.time_animation = 1
	 self.speed_x 		= 200
	 self.acceleration_x=0
	 self.speed_y 		= 200
	 self.acceleration_y=0
       self.speedXY=200
        self.time=0


	self.point_start=Position(self.start_x,self.start_y)
	self.point_finish = Position(self.finish_x,self.finish_y)

	self.chench_x = math.abs(self.start_x-self.finish_x)
	self.chench_y = math.abs(self.start_y-self.finish_y)
	self.cooficient = self.chench_x/self.chench_y
      self.speed_y= math.sqrt(self.speedXY*self.speedXY/(1+self.cooficient*self.cooficient))
      self.speed_x=self.speed_y*self.cooficient

	self.current_x=self.start_x
	self.current_y=self.start_y

	--self.speed_y=self.speed_x*(self.chench_y/self.chench_x)
	--self.acceleration_y=  self.acceleration_x*(self.chench_y/self.chench_x)


	if self.finish_x-self.start_x>0 then
		self.direction_x = 1
	else
		self.direction_x = -1
	end

	if self.finish_y-self.start_y>0 then
		self.direction_y = 1
	else
		self.direction_y = -1
      end

end

function starGame:on_update(dt)
 if self.start_x~=nil then

  self.time=self.time+dt;
  if self:isCompliteMove() then	    --print(self.time);

   self.current_x=self.start_x + self.direction_x*(self.speed_x*self.time + self.acceleration_x*self.time*self.time/2)
   self.current_y=self.start_y + self.direction_y*(self.speed_y*self.time + self.acceleration_y*self.time*self.time/2)
   self.object:set_position(Position(self.current_x,self.current_y), true)
  else

   if self.executeone<1 then
    if self.rand then
     self.executeone=5
     self:fixetStar(self.countFixet)
     self.executeone=5

    else
     self.executeone=5
     self:randomStar()
     self.executeone=5
    end
   end
   self.sound:fade_out()
 end
end
end

function starGame:isCompliteMove()
 if  (self.direction_x==1) and (self.direction_y==1) then
  if (self.direction_x*self.current_x<self.finish_x) and (self.direction_y*self.current_y<self.finish_y) then
   return true
  else
   return false
  end
 end
 if  (self.direction_x==1) and (self.direction_y==-1) then
  if (self.current_x<self.finish_x) and (self.current_y>self.finish_y) then
   return true
  else
   return false
  end
 end
 if  (self.direction_x==-1) and (self.direction_y==1) then
  if (self.current_x>self.finish_x) and (self.current_y<self.finish_y) then
    return true
  else
   return false
  end
 end
 if  (self.direction_x==-1) and (self.direction_y==-1) then
   if (self.current_x>self.finish_x) and (self.current_y>self.finish_y) then
    return true
   else
    return false
   end
 end

end

function starGame:on_mouse_move()
 local scene=self:scene()
 local selected=scene:overed_object()
 if selected then
  for i=1,#self.arrButton do
   if selected:name()==self.arrButton[i]:name() then
    self.arrButton[i]:set_deep_level(5)
    return
   end
  end
 else
   for i=1,#self.arrButton do
    self.arrButton[i]:set_deep_level(9)
   end
 end

end
