class 'p03_l06_m01_game' (Script)

function p03_l06_m01_game:__init()super()
  self.crane={}
  self.water={}
  self.drop={}
  self.arrow=nil

  self.load=false

  self.savePos={}
  self.savePos[1]={}
  self.savePos[2]={}

  self.itirationOne=true

  self.arrChanges={{1,2},{1,3},{2,3},{2,4} }
  self.arrPosChanges={{20,-10},{-10,20},{20,-20},{-30,20} }
  self.moveArrow=false
end

function p03_l06_m01_game:on_attach()
  local water = p03_l06_m01_water()
  local crane =p03_l06_m01_crane()
  local tween=tweener()
  self.drop_move_sound=self:scene():sound('g_drop_move')
  self.drop_move_sound:play(true)
  local scene=self:scene()
  if self.itirationOne then
    for i=1,4 do
     water = p03_l06_m01_water()
     crane =p03_l06_m01_crane()
     tween=tweener()
     water.object=scene:object('liquid_activ0'..i)
     water.tween=tween
     crane.object=scene:object('crane0'..i)
     crane.arrChenge=self.arrChanges[i]
     crane.arrParamChenges=self.arrPosChanges[i]
     crane.state=1

      self.crane[i]=crane
      self.water[i]=water
    end
--    water[1].object:set_position(Position(water[1].object:position():x(),water[1].object:position():y()-20))
--    water[2].object:set_position(Position(water[2].object:position():x(),water[2].object:position():y()-20))
--    water[3].object:set_position(Position(water[3].object:position():x(),water[3].object:position():y()+15))
--    water[4].object:set_position(Position(water[4].object:position():x(),water[4].object:position():y()-15))
     for j=1,3 do
       tween=tweener()
       water = p03_l06_m01_water()
       water.tween=tween
       water.object=scene:object('drop0'..j)
      -- water.posUp= water.object:position()
      -- water.posDown=Position(water.object:position():x()+1,water.object:position():y()+100+j*10)
       self.drop[j]= water
     end
     water = p03_l06_m01_water()
       tween=tweener()
       water.tween=tween
       water.object=scene:object('arrow')
       water.posUp= water.object:position()
       water.posDown=Position(water.object:position():x()+1,water.object:position():y()+100)
     self.arrow=water

     if self.load then
      for i=1,4 do
        self.water[i].object:set_position(self.savePos[1][i],true)
      end

      for j=1,3 do
        self.drop[j].object:set_position(self.savePos[2][j],true)
        self.drop[j].posUp= self.savePos[2][j]
        self.drop[j].posDown=Position(self.drop[j].posUp:x()+1,self.drop[j].posUp:y()+100+j*10)
      end

     else
      for i=1,4 do
       self.savePos[1][i]=self.water[i].object:position()
      end

      for j=1,3 do
       self.drop[j].posUp=self.drop[j].object:position()
       self.drop[j].posDown=Position(self.drop[j].object:position():x()+1,self.drop[j].object:position():y()+100+j*10)
       self.savePos[2][j]= self.drop[j].object:position()
      end
     end
  end
  self.load=false
  self.itirationOne=false
end

function p03_l06_m01_game:on_lb_down()
 -- print('hy')
  local scene=self:scene()
  local selected = scene:overed_object()
  if selected then
    for i=1,#self.crane do
     if self.crane[i].object:name()==selected:name() and self:complete_move() then
      self:go_move(self.crane[i])
      self.crane[i].state= self.crane[i].state*(-1)
      self:complete_game()
      scene:play_sound('g_click_crane')
     end
    end
  end
end
function p03_l06_m01_game:on_update(dt)
  for i=1,#self.water do
   self.water[i].tween:update(dt)
  end

  for j=1,#self.drop do
   self.drop[j].tween:update(dt)
   if self.drop[j].tween:getCompleteMoveGlob() then

    self.drop[j].tween:getsParmsMove(self.drop[j].object,90+j*20,self.drop[j].posUp,self.drop[j].posDown)
   end
  end
   self.arrow.tween:update(dt)
  if self.moveArrow then
    self.arrow.tween:getsParmsMove(self.arrow.object,100,self.arrow.posUp,self.arrow.posDown)
    self.moveArrow=false
  end

end

function p03_l06_m01_game:complete_move()
  local count=0
  for i=1,#self.water do
   if self.water[i].tween:getCompleteMoveGlob() then
   else
    return false
   end
  end
  return true
end

function p03_l06_m01_game:go_move(crane)
if crane.state==1 then
 for i=1,#crane.arrChenge do
  self.water[crane.arrChenge[i]].tween:getsParmsMove(self.water[crane.arrChenge[i]].object,100
                                                ,self.water[crane.arrChenge[i]].object:position()
                                   ,Position(self.water[crane.arrChenge[i]].object:position():x()+0.01,
                                              self.water[crane.arrChenge[i]].object:position():y()+crane.arrParamChenges[i]))
 end
end
if crane.state==-1 then
  for j=1,#crane.arrChenge do
      self.water[crane.arrChenge[j]].tween:getsParmsMove(self.water[crane.arrChenge[j]].object,100
                                                ,self.water[crane.arrChenge[j]].object:position()
                                   ,Position(self.water[crane.arrChenge[j]].object:position():x()-0.01,
                                              self.water[crane.arrChenge[j]].object:position():y()-crane.arrParamChenges[j]))

 end
end
end

function p03_l06_m01_game:complete_game()
 local scene=self:scene()
 if self.crane[1].state==-1 and self.crane[2].state==1 and self.crane[3].state==-1 and self.crane[4].state==-1 then
     self.drop_move_sound:stop()
     self:scene():play_sound('g_complete')
     scene:execute_action('complete_game')
   self.moveArrow=true
 end

end

function p03_l06_m01_game:save(os)
    os:save(0) -- version
    os:save(true)    
    os:save(#self.savePos)
    for i=1,#self.savePos do
     os:save(#self.savePos[i])
     for j=1,#self.savePos[i] do
      os:save(self.savePos[i][j])
     end
    end
end

function p03_l06_m01_game:load(is)
    local ver = is:load()
    self.load = is:load()
    for i=1,is:load() do
     for j=1,is:load() do
      self.savePos[i][j]=is:load()
     end
    end
end
function p03_l06_m01_game:on_detach()
    self.drop_move_sound:stop()
end