class 'p04_l03_z04_game01' (Script)

function p04_l03_z04_game01:__init()super()
self.inicialOne=true
self.arrPart={}
self.arrFx={}
self.selected=fif_part()
self.selected.object=nil
self.current=nil
self.exeOne=true

self.load=false
self.name={}

--self.arrTweens={}

--self.stepComplete=500
--self.flagComplete=false

for i=1,3 do
   self.arrPart[i]={}
   for j=1,3 do
    self.arrPart[i][j]=fif_part()
    self.arrPart[i][j].tween=tweener()
   end
end

--for l=1,8 do
--  self.arrTweens[l]=tweener()
--end


end

function p04_l03_z04_game01:on_attach()

if self.inicialOne then
  local scene=self:scene()
  local arrObject={}
  local help=help_class()
  local coof=0
  local coord={x=0,y=0 }

  self.selected.object=scene:object('fx_mouseover')

  for l=1,8 do
   self.arrFx[l]=scene:object('line0'..l)
  end

  for s=1,9 do
   arrObject[s]=scene:object('i'..s)
  end
 -- help:randSort(arrObject)

  for i=1,3 do
   for j=1,3 do
    coord={x=0,y=0}
    coord.y=i
    coord.x=j

    self.arrPart[i][j].object=arrObject[i+3*(j-1)]
    self.arrPart[i][j].power=i+3*(j-1)
    self.arrPart[i][j].coord=coord

   end
  end
  self:exchangeEl(self.arrPart[1][2],self.arrPart[3][3])
  self:exchangeEl(self.arrPart[2][1],self.arrPart[3][1])
  for p=1,3 do
   for n=1,3 do
   self.arrPart[p][n]:setPosObject()
   end
  end
  self:play_fx()
end

if self.load then
   local arrObject={}
   local scene=self:scene()

  self.selected.object=scene:object('fx_mouseover')

  for l=1,8 do
   self.arrFx[l]=scene:object('line0'..l)
  end

  for s=1,9 do
     arrObject[s]=scene:object('i'..s)
  end

  for i=1,3 do
   for j=1,3 do
    self.arrPart[i][j].object=scene:object(self.name[i][j])
    self.arrPart[i][j]:setPosObject()
   end
  end
end

--self.load=false
self.inicialOne=false
end

function p04_l03_z04_game01:on_lb_down()

 local scene =self:scene()
local selected=scene:overed_object()
-- print(selected:name())

 if self:complete_move() then
    --  print(selected:name())
  if selected then

    for i=1,#self.arrPart do
     for j=1,#self.arrPart[i] do

      if self.arrPart[i][j].object:name()==selected:name() then
       if self.exeOne then
        self.exeOne=false

       if  self.current==nil then
        self.current=self.arrPart[i][j]
      -- print(self.current.object:name())
        self.selected.coord=self.arrPart[i][j].coord
        self.selected:set_goodpos(-9,-9)
        scene:play_sound('g_select_object')
       else
        self.selected.object:set_position(Position(-100,-100),true)
        if self:admissionExcange(self.current,self.arrPart[i][j]) then
        -- print(self.current.object:name())
         self:exchangeEl(self.current,self.arrPart[i][j])
         self:play_fx()
         if self:complete_game() then
         -- self:moveAll()
           self:scene():play_sound('g_complete')
          scene:execute_action('complete_game01')
         else
             scene:play_sound('g_move_object')
         end

         self.current=nil
        else
         self.current=nil
        end
        end
       end
      end
     end
    end
    self.exeOne=true
  end
 end
end

function p04_l03_z04_game01:admissionExcange(el1,el2)

if math.abs(el1.coord.x-el2.coord.x)==1 and  math.abs(el1.coord.y-el2.coord.y)==0 then
  return true
end

if math.abs(el1.coord.y-el2.coord.y)==1 and  math.abs(el1.coord.x-el2.coord.x)==0 then
  return true
end

return false
end

--function p04_l03_z04_game01:moveAll()
-- for l=1,8 do
--  self.arrTweens[l]:getsParmsMove(obj,speed,startPos,finishPos)
-- end
--
-- for i=1,#self.arrPart do
--   for j=1,#self.arrPart[i] do
--     self.arrPart[i][j].tween:update(dt)
--   end
-- end
--end

function p04_l03_z04_game01:exchangeEl(el1,el2)
local object
local power
object=el1.object
el1.object=el2.object
el2.object=object

power=el1.power
el1.power=el2.power
el2.power=power

el1:move()
el2:move()
  self.current=nil
end

function p04_l03_z04_game01:on_update(dt)
 for i=1,#self.arrPart do
   for j=1,#self.arrPart[i] do
     self.arrPart[i][j].tween:update(dt)
   end
 end
-- for l=1,8 do
--   self.arrTweens[l]:update(dt)
-- end

end


function p04_l03_z04_game01:complete_move()
  for i=1,#self.arrPart do
   for j=1,#self.arrPart[i] do
     if not self.arrPart[i][j].tween:getCompleteMoveGlob() then
      -- print('hy')
      return false
     end
   end
  end
--  print('re')
  return true
end

function p04_l03_z04_game01:complete_game()
 for i=1,#self.arrFx do
  if not self.arrFx[i]:visible() then
   return false
  end
 end
 return true
end

function p04_l03_z04_game01:play_fx()
 --print(self.arrFx[1]:name())

 if self.arrPart[1][1].power+self.arrPart[1][2].power+self.arrPart[1][3].power==15 then
  self.arrFx[4]:show()
 else
  self.arrFx[4]:hide()
 end

 if self.arrPart[2][1].power+self.arrPart[2][2].power+self.arrPart[2][3].power==15 then
  self.arrFx[5]:show()
 else
  self.arrFx[5]:hide()
 end

 if self.arrPart[3][1].power+self.arrPart[3][2].power+self.arrPart[3][3].power==15 then
  self.arrFx[6]:show()
 else
  self.arrFx[6]:hide()
 end

 if self.arrPart[1][1].power+self.arrPart[2][1].power+self.arrPart[3][1].power==15 then
  self.arrFx[1]:show()
 else
  self.arrFx[1]:hide()
 end

 if self.arrPart[1][2].power+self.arrPart[2][2].power+self.arrPart[3][2].power==15 then
  self.arrFx[2]:show()
 else
  self.arrFx[2]:hide()
 end


 if self.arrPart[1][3].power+self.arrPart[2][3].power+self.arrPart[3][3].power==15 then
  self.arrFx[3]:show()
 else
  self.arrFx[3]:hide()
 end

 if self.arrPart[1][1].power+self.arrPart[2][2].power+self.arrPart[3][3].power==15 then
  self.arrFx[7]:show()
 else
  self.arrFx[7]:hide()
 end


 if self.arrPart[3][1].power+self.arrPart[2][2].power+self.arrPart[1][3].power==15 then
  self.arrFx[8]:show()
 else
  self.arrFx[8]:hide()
 end

end

--function p04_l03_z04_game01:save(os)
--  os:save(0) -- version
--  os:save(self.inicialOne)
--  os:save(true)
--  for i=1,3 do
--   for j=1,3 do
--    os:save(self.arrPart[i][j].power)
--    os:save(self.arrPart[i][j].coord.x)
--    os:save(self.arrPart[i][j].coord.y)
--    os:save(self.arrPart[i][j].object:name())
--   end
--  end
--end
--
--function p04_l03_z04_game01:load(is)
--  local ver = is:load()
--  self.inicialOne= is:load()
--  self.load= is:load()
--  for i=1,3 do
--   self.name[i]={}
--   for j=1,3 do
--    self.arrPart[i][j].power  = is:load()
--    self.arrPart[i][j].coord.x  = is:load()
--    self.arrPart[i][j].coord.y  = is:load()
--    self.name[i][j]=is:load()
--   end
--  end
--end

