class 'p04_l03_m01_game01' (Script)

function p04_l03_m01_game01:__init()super()
self.arrEll={}
self.arrPosition={}
self.empty=nil
self.directionMove=nil

self.initOne=true
self.load=false
self.speed=200

self.current=nil

for i=1,6 do
  self.arrEll[i]=p04_l03_m01_element()
  if i<4 then
   self.arrEll[i].tween=tweener_mod()
  end
  if i>3 then
   self.arrEll[i].tween=tweener_mod()
  end
end

self.empty=p04_l03_m01_element()
self.empty.tween=tweener_mod()

end

function p04_l03_m01_game01:on_attach()
 local element=p04_l03_m01_element()
 local scene=self:scene()

self.sel=scene:object('fx')
for i=1,6 do
   if i<4 then
    self.arrEll[i].object=scene:object('red0'..i)
    self.arrEll[i].type=1
   end
   if i>3 then
    self.arrEll[i].object=scene:object('green0'..i-3)
    self.arrEll[i].type=2
   end
end

if self.initOne then
 for j=1,3 do
  self.arrPosition[j]=self.arrEll[j].object:position()
end

for k=5,7 do
  self.arrPosition[k]=self.arrEll[k-1].object:position()
end
self.arrPosition[4]=Position((self.arrEll[3].object:position():x()+self.arrEll[4].object:position():x())/2,self.arrEll[3].object:position():y())


 for i=1,6 do
    if i<4 then
    self.arrEll[i].coord=i
    self.arrEll[i].typePos=2

    end
    if i>3 then
     self.arrEll[i].coord=i+1
     self.arrEll[i].typePos=1

    end
 end
 self.empty.coord=4
 self.empty.typePos=0

end

if self.load then
   for i=1,6 do
    self.arrEll[i].object:set_position(self.arrPosition[self.arrEll[i].coord],true)
   end
end



self.load=false
self.initOne=false
end

function p04_l03_m01_game01:on_lb_down()
local scene=self:scene()

local selected=scene:overed_object()
self.sel:set_position(Position(-100,-100),true)
if selected then
 if self:completeRot() then
  for i=1,#self.arrEll do
   if selected:name()==self.arrEll[i].object:name() then
    self.sel:set_position(Position(-100,-100),true)
    if self:resolutionMove(self.arrEll[i]) then
     self.current=i
     if self.directionMove==1 then
      self.arrEll[i].tween:setParamsRotateTr(self.arrEll[i].object,self.speed,180,-1,self:getSenter(self.arrEll[i]),90)
     end
     if self.directionMove==-1 then
      self.arrEll[i].tween:setParamsRotateTr(self.arrEll[i].object,self.speed,180,1,self:getSenter(self.arrEll[i]),270)
     end
     if self.directionMove==2 then
      self.arrEll[i].tween:setParamsRotateTr(self.arrEll[i].object,self.speed,180,1,self:getSenter(self.arrEll[i]),90)
     end
     if self.directionMove==-2 then
      self.arrEll[i].tween:setParamsRotateTr(self.arrEll[i].object,self.speed,180,-1,self:getSenter(self.arrEll[i]),270)
     end
     self:exchangeEl(self.arrEll[i])
     scene:play_sound('g_click_cristl')
     self:completeGame()
    end
   end
  end
  end
end
end

function p04_l03_m01_game01:on_mouse_move()
 local scene=self:scene()
 local selected=scene:overed_object()
 if not self:completeRot() then
   self.sel:set_position(Position(-100,-100),true)
 end
 if selected then
  if self:completeRot() then
  for i=1,#self.arrEll do
   if selected:name()==self.arrEll[i].object:name() then
      self.sel:set_position(Position(self.arrPosition[self.arrEll[i].coord]:x()-8,self.arrPosition[self.arrEll[i].coord]:y()-8),true)
      return
   else
      self.sel:set_position(Position(-100,-100),true)

   end
  end

  end
  end
end

function p04_l03_m01_game01:on_update(dt)
for i=1,#self.arrEll do
  self.arrEll[i].tween:update(dt)
end
end

      --my function
      --1
function p04_l03_m01_game01:resolutionMove(obj)
if obj.coord-self.empty.coord==1 then
  self.directionMove=1
  return true
end
if  obj.coord-self.empty.coord==-1 then
  self.directionMove=-1
  return true
end

if  obj.coord-self.empty.coord==2 then
  self.directionMove=2
  return true
end

if  obj.coord-self.empty.coord==-2 then
  self.directionMove=-2
  return true
end
return false
end
      --2
function p04_l03_m01_game01:exchangeEl(ell)
local coord
local type
type=ell.typePos
coord=ell.coord

ell.typePos=self.empty.typePos
self.empty.typePos=type

ell.coord=self.empty.coord
self.empty.coord=coord
end
     --3
function p04_l03_m01_game01:getSenter(obj)
local pos
 --print(self.arrPosition[obj.coord]:x())
--print(self.empty.coord)
 pos=Position((self.arrPosition[obj.coord]:x()+self.arrPosition[self.empty.coord]:x())/2,self.arrPosition[obj.coord]:y())
  --pos=Position(1,1)
  return pos
end
--4
function p04_l03_m01_game01:completeGame()
 local scene=self:scene()
 for i=1,#self.arrEll do
  if self.arrEll[i].typePos~=self.arrEll[i].type then
   return
  end
 end
 self:scene():play_sound('g_complete')
 scene:execute_action('complete_game01')
end
--5
function p04_l03_m01_game01:completeRot()
 for i=1,#self.arrEll do
  if not self.arrEll[i].tween:completerot() then
   return false
  end
 end
 return true
end

function p04_l03_m01_game01:save(os)
  os:save(0) -- version
  os:save(true)
  os:save(self.initOne)

  os:save(self.empty.typePos)
  os:save(self.empty.coord)

  for j=1,7 do
   os:save(self.arrPosition[j])
  end

  for i=1,6 do
   os:save(self.arrEll[i].typePos)
   os:save(self.arrEll[i].coord)
  end
   os:save(self.directionMove)
   os:save(self.current)
end

function p04_l03_m01_game01:load(is)
  local ver = is:load()
  self.load = is:load()
  self.initOne=is:load()

  self.empty.typePos=is:load()
  self.empty.coord=is:load()

  for j=1,7 do
   self.arrPosition[j]= is:load()
  end

  for i=1,6 do
    self.arrEll[i].typePos = is:load()
    self.arrEll[i].coord = is:load()
  end
  self.directionMove=is:load()
 self.current=is:load()
end
