class 'p02_l01_game01' (Script)

function p02_l01_game01:__init()super()
--memory
self.itirayionOne=true
self.currentStone=nil
self.executOne=true

self.arrColumnObj={}
self.arrStone={}
self.arrColumn={}

self.arrMaskStone={}
self.arrPutStone={}
self.arrNameInventory={}

self.minHeightColumn=nil

--parms
self.factorColumn={0.8,0.9,1.1 }
self.shiftColumn=15
self.speedStoneMoveBack=300
self.speedStoneInColumn=100
self.speedColumn=100
self.levelUp=40
self.levelDawn=30

--save
self.load=false
self.savePos={}
self.savePos[1]={}
self.savePos[2]={}
self.number={}
self.number[1]={}
self.number[2]={}
self.exeSkip=true
end

function p02_l01_game01:on_attach()
self.clickOne=true
local scene=self:scene()

local column
local stone
local maskStone
local putStone


if self.itirayionOne then
  for s=1,3 do
   self.arrColumnObj[s]=scene:object('column_'..s)
  end

 self.minHeightColumn=self:getsMinHeight()

  for i=1,3 do
   column=p02_l01_column01()
   stone=p02_l01_stone01()


   column.object=scene:object('column_'..i)
   column.number=i
   column.index=i
   column.factor=self.factorColumn[i]
   column.tween=tweener()

   stone.object=scene:object('stone0'..i)
   stone.number=i
   stone.index=i
   stone.tween=tweener()
   stone.mass=self:getsMass(column)

   self.arrColumn[i]=column
   self.arrStone[i]=stone
  end

  for j=1,3 do
   maskStone=p02_l01_maskStone01()
   maskStone.object=scene:object('stone_mask0'..j)
   maskStone.number=j
   self.arrMaskStone[j]=maskStone

   self.arrNameInventory[j]='stone'..j..'-i'
  end

  for k=1,3 do
   putStone=p02_l01_putStone01()
   putStone.object=scene:object('input_stone'..k)
   putStone.number=k
   self.arrPutStone[k]= putStone
  end

  if self.load then
--   if self:startMove() then
--
--   else
--
--   end
   for i=1,3 do
     self.arrStone[i].object:set_position(self.savePos[1][i],true)
     self.arrColumn[i].object:set_position(self.savePos[2][i],true)
     self.arrStone[i].number=self.number[1][i]
     self.arrColumn[i].number=self.number[2][i]
   end
  else
   for i=1,3 do
    self.savePos[1][i]= self.arrStone[i].object:position()
    self.savePos[2][i]= self.arrColumn[i].object:position()
    self.number[1][i]= self.arrStone[i].number
    self.number[2][i]= self.arrColumn[i].number
   end
  end
end

self.itirayionOne=false
self.load=false
end

-- --------------------------------------------------------------------------------------
function p02_l01_game01:getsMinHeight()
 local min=self.arrColumnObj[1]:dimensions():ly()
 for i=1,#self.arrColumnObj do
   if self.arrColumnObj[i]:dimensions():ly()<min then
   min=self.arrColumnObj[i]:dimensions():ly()
   end
 end
 return min
end

-- --------------------------------------------------------------------------------------
function p02_l01_game01:getsMass(column)
 local mass
 mass= (self.shiftColumn+column.object:dimensions():ly()-self.minHeightColumn)/column.factor
 --print(mass..column.object:dimensions():ly())
 return mass
end
-- ---------------------------------------------------------------------------------------
function p02_l01_game01:on_lb_down()
 local scene=self:scene()
 local selected=scene:overed_object()
 local selectedInventory=scene:item_in_hand()
 local count=0
 local count2=0

 self.clickOne=true

 if self:completeMove() then
  --print('completeMove')
  if selectedInventory~='' then
   for p=1,#self.arrNameInventory  do
    if self.arrNameInventory[p]==selectedInventory then
      --print('selectedInventory')
      self.arrMaskStone[p].object:set_position(Position(scene:cursor_position():x()-(self.arrMaskStone[p].object:dimensions():lx()/2),
                                             scene:cursor_position():y()-(self.arrMaskStone[p].object:dimensions():ly()/2) ),true)
      self:capture_object(self.arrMaskStone[p].object)

      for m=1,#self.arrPutStone do
       if self.arrMaskStone[p].object:is_crossing(self.arrPutStone[m].object) then
         self:putStone(p,m)
         scene:play_sound('g_put_stone')
         break
       else
        count2=count2+1
       end
      end
      if count2>#self.arrPutStone-1 then
       self:release_capture(self.arrMaskStone[p].object)
       self.arrMaskStone[p].object:set_position(Position(-300,-300),true)

      end
    end
   end
  end

   if selected then
    --self:setsStartPos()
    for i=1,#self.arrStone do

    if selected:name()==self.arrStone[i].object:name() then
     if self:startMove() then
      for t=1,#self.arrStone do
       self.arrStone[t].object:set_deep_level(self.levelDawn, true)
      end

      if self.currentStone==nil then
       self:setsStartPos()
       self.currentStone=self.arrStone[i]
       self.currentStone.object:set_position(Position(scene:cursor_position():x()-(self.currentStone.object:dimensions():lx()/2),
                                                      scene:cursor_position():y()-(self.currentStone.object:dimensions():ly()/2) ),true)
       self.currentStone.object:set_deep_level(self.levelUp, true)
       self:capture_object(self.currentStone.object)
       scene:play_sound('g_capture_stone')
      else
       self:release_capture(self.currentStone.object)
       for j=1,#self.arrStone do

        if self.currentStone.number~=self.arrStone[j].number then
         if self.currentStone.object:is_crossing(self.arrStone[j].object) then
          self:exchangeStone(self.currentStone,self.arrStone[j])
           --print(self.currentStone.number)
          scene:play_sound('g_exchange_stone')
          self.currentStone=nil
          break
         else
          count=count+1
         end
        end

       end
      end
       if count>#self.arrStone-2 then
        scene:play_sound('g_fail_stone')
        self:release_capture(self.currentStone.object)
        self:moveStartPosition(self.currentStone)
        self.currentStone=nil
       end
     else
      scene:set_hint('root/hints/p02_l01/@stone')
     end
    end
   end
  end
  for i=1,3 do
      self.number[1][i]= self.arrStone[i].number
      self.number[2][i]= self.arrColumn[i].number
  end
 end
end

function p02_l01_game01:on_lb_up()
    local scene=self:scene()
    local selected=scene:overed_object()
    local selectedInventory=scene:item_in_hand()
    local count=0
    local count2=0
    self.clickOne=true


    if self:completeMove() then
        --print('completeMove')
        if selectedInventory~='' then
            for p=1,#self.arrNameInventory  do
                if self.arrNameInventory[p]==selectedInventory then
                    --print('selectedInventory')
                    self.arrMaskStone[p].object:set_position(Position(scene:cursor_position():x()-(self.arrMaskStone[p].object:dimensions():lx()/2),
                        scene:cursor_position():y()-(self.arrMaskStone[p].object:dimensions():ly()/2) ),true)
                    self:capture_object(self.arrMaskStone[p].object)

                    for m=1,#self.arrPutStone do
                        if self.arrMaskStone[p].object:is_crossing(self.arrPutStone[m].object) then
                            self:putStone(p,m)
                            scene:play_sound('g_put_stone')
                            break
                        else
                            count2=count2+1
                        end
                    end
                    if count2>#self.arrPutStone-1 then
                        self:release_capture(self.arrMaskStone[p].object)
                        self.arrMaskStone[p].object:set_position(Position(-300,-300),true)

                    end
                end
            end
        end

--        if selected then
--            --self:setsStartPos()
--            for i=1,#self.arrStone do
--
--                if selected:name()==self.arrStone[i].object:name() then
--                    if self:startMove() then
--                        for t=1,#self.arrStone do
--                            self.arrStone[t].object:set_deep_level(self.levelDawn, true)
--                        end
--
--                        if self.currentStone==nil then
--                            self:setsStartPos()
--                            self.currentStone=self.arrStone[i]
--                            self.currentStone.object:set_position(Position(scene:cursor_position():x()-(self.currentStone.object:dimensions():lx()/2),
--                                scene:cursor_position():y()-(self.currentStone.object:dimensions():ly()/2) ),true)
--                            self.currentStone.object:set_deep_level(self.levelUp, true)
--                            self:capture_object(self.currentStone.object)
--                            scene:play_sound('g_capture_stone')
--                        else
--                            self:release_capture(self.currentStone.object)
--                            for j=1,#self.arrStone do
--
--                                if self.currentStone.number~=self.arrStone[j].number then
--                                    if self.currentStone.object:is_crossing(self.arrStone[j].object) then
--                                        self:exchangeStone(self.currentStone,self.arrStone[j])
--                                        --print(self.currentStone.number)
--                                        scene:play_sound('g_exchange_stone')
--                                        self.currentStone=nil
--                                        break
--                                    else
--                                        count=count+1
--                                    end
--                                end
--
--                            end
--                        end
--                        if count>#self.arrStone-2 then
--                            scene:play_sound('g_fail_stone')
--                            self:release_capture(self.currentStone.object)
--                            self:moveStartPosition(self.currentStone)
--                            self.currentStone=nil
--                        end
--                    else
--                        scene:set_hint('root/hints/p02_l01/@stone')
--                    end
--                end
--            end
--        end
        for i=1,3 do
            self.number[1][i]= self.arrStone[i].number
            self.number[2][i]= self.arrColumn[i].number
        end
    end
end

function p02_l01_game01:startMove()
 for i=1,#self.arrStone do
  if not self.arrStone[i].object:visible() then
   return false
  end
 end
 return true
end


function p02_l01_game01:putStone(stoneNumber,columnNumber)
 self.arrPutStone[columnNumber].object:set_position(Position(-300,-300),true)
 self.arrPutStone[columnNumber].object:hide()
 self.arrStone[stoneNumber].number=columnNumber
 self.arrStone[stoneNumber].object:set_position(self:getsStartPosStoneInColumn(self.arrStone[stoneNumber].object,self.arrColumn[columnNumber].object),true)
 self.arrStone[stoneNumber].object:show()
 self.arrStone[stoneNumber].posStart=self.arrStone[stoneNumber].object:position()
 self:moveColumnWithStone(self.arrStone[stoneNumber],self.arrColumn[columnNumber],self.arrStone[stoneNumber].mass)

 if self:startMove() then
   if self.exeSkip then
       self:scene():execute_action('game_s_i')
       self:scene():set_mark('start_games')
   end
   self.exeSkip=false
 end
end

function p02_l01_game01:setsStartPos()
 for i=1,#self.arrStone do
  self.arrStone[i].posStart=self.arrStone[i].object:position()
 end

 for j=1,#self.arrColumn do
  self.arrColumn[j].posStart=self.arrStone[j].object:position()
 end
end

function p02_l01_game01:on_update(dt)
 local count=0
 local scene=self:scene()
 for i=1,#self.arrColumn do
  self.arrColumn[i].tween:update(dt)
 end
 for j=1,#self.arrStone do
  self.arrStone[j].tween:update(dt)
 end

 if self:completeMove() then
  if self.clickOne then
      for i=1,3 do
          if self.savePos[1][i] then
          self.arrStone[i].object:set_position(self.savePos[1][i],true)
          self.arrColumn[i].object:set_position(self.savePos[2][i],true)
          -- print(self.savePos[2][i]:y())
          end
      end
     self.clickOne=false
  end
 end


 if self.executOne then
 if self:completeMove() then
  for i=1,#self.arrColumn do
   if math.abs(self.arrColumn[1].object:position():y()-self.arrColumn[i].object:position():y())<5 then
    count=count+1
   else
    break
   end
  end
  if count>#self.arrColumn-1 then
      for i=1,3 do
          self.arrStone[i].object:set_position(self.savePos[1][i],true)
          self.arrColumn[i].object:set_position(self.savePos[2][i],true)
          --print(self.savePos[2][i]:y())
      end

      scene:play_sound('g_complete')
   scene:execute_action('complite_game')

   self.executOne=false
  end
 end
 end

end

function p02_l01_game01:exchangeStone(stone1,stone2)
 local number
 local difMass1
 local difMass2

 number=stone1.number
 stone1.number=stone2.number
 stone2.number=number

 stone1.posStart=self:getsStartPosStoneInColumn(stone1.object,self.arrColumn[stone1.number].object)
 stone2.posStart=self:getsStartPosStoneInColumn(stone2.object,self.arrColumn[stone2.number].object)

 stone1.object:set_position(stone1.posStart,true)
 stone2.object:set_position(stone2.posStart,true)
 stone1.object:set_deep_level(self.levelDawn, true)
 stone2.object:set_deep_level(self.levelDawn, true)
 difMass1=stone2.mass-stone1.mass
 difMass2=stone1.mass-stone2.mass


 self:moveColumnWithStone(stone1,self.arrColumn[stone1.number],difMass2)
 self:moveColumnWithStone(stone2,self.arrColumn[stone2.number],difMass1)

end

function p02_l01_game01:moveColumnWithStone(stone,column,difMass)
 local finishPosStone
 local finishPosColumn

 finishPosStone=Position(stone.posStart:x(),stone.posStart:y()+difMass*column.factor)
 finishPosColumn=Position(column.object:position():x(),column.object:position():y()+difMass*column.factor)
 stone.tween:getsParmsMove(stone.object,self.speedStoneInColumn,stone.object:position(),finishPosStone)
 column.tween:getsParmsMove(column.object,self.speedColumn,column.object:position(),finishPosColumn)
 self.savePos[1][stone.index]=finishPosStone
 self.savePos[2][column.index]=finishPosColumn
-- print(finishPosColumn:y())
end

function p02_l01_game01:moveStartPosition(stone)
 local scene = self:scene()
 stone.tween:getsParmsMove(stone.object,self.speedStoneMoveBack,stone.object:position(),stone.posStart)
 stone.object:set_deep_level(self.levelUp,true)
 self.savePos[1][stone.index]= stone.posStart
end

function p02_l01_game01:completeMove()
 for i=1,#self.arrStone do
  if not self.arrStone[i].tween:getCompleteMoveGlob() then
   return false
  end
 end

 for j=1,#self.arrColumn do
  if not self.arrColumn[j].tween:getCompleteMoveGlob() then
   return false
  end
 end
 return true
end

function p02_l01_game01:getsStartPosStoneInColumn(stoneObj,columnObj)
 local posStone

 posStone=Position((columnObj:dimensions():lx()/2)-(stoneObj:dimensions():lx()/2)+columnObj:position():x(),
                    columnObj:position():y()-stoneObj:dimensions():ly())
 return posStone
end

function p02_l01_game01: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
    os:save(#self.number)
    for i=1,#self.number do
        os:save(#self.number[i])
        for j=1,#self.number[i] do
            os:save(self.number[i][j])
        end
    end
    os:save(self.exeSkip)
    --self.number[1][i]
end

function p02_l01_game01: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
    for i=1,is:load() do
        for j=1,is:load() do
            self.number[i][j]=is:load()
        end
    end
    self.exeSkip=is:load()
end