class 'p03_l02_z01_game01' (Script)

function p03_l02_z01_game01:__init()super()
 self.initOne=true
 self.load=false

 self.arrBox={}
 self.arrLever={}
 self.arrAnim={}
 self.arrLight={}

  for i=1,2 do
  self.arrBox[i]=p03_l02_z01_box()
  end

 --a_witch01; a_witch02; a_witch03; a_witch04;lamp00_light; lamp01_light; lamp02_light
end

function p03_l02_z01_game01:on_attach()
local scene=self:scene()


  self.arrLever[1]={}
  self.arrLever[2]={}
 for j=1,3 do
   self.arrLever[1][j]=scene:object('lever0'..j..'_1')
   self.arrLever[2][j]=scene:object('lever0'..j..'_2')

 end

 for i=1,2 do
  self.arrBox[i].objectArr=self.arrLever[i]
 end

 for a=1,4 do
  self.arrAnim[a]=scene:object('a_witch0'..a)
 end

 for b=1,3 do
  self.arrLight[b]=scene:object('lamp0'..(b-1)..'_light')
 end

self.arrEff={{{self.arrAnim[4]},{self.arrAnim[1],self.arrLight[1]},{self.arrAnim[2],self.arrLight[2]}},
             {{self.arrAnim[3],self.arrLight[1],self.arrLight[2]},{self.arrAnim[1],self.arrLight[2],self.arrLight[3]},{self.arrAnim[2],self.arrLight[1],self.arrLight[3]}},
             {{self.arrAnim[1],self.arrLight[2],self.arrLight[3]},{self.arrLight[1],self.arrLight[2],self.arrLight[3]},{self.arrAnim[3],self.arrLight[3]}}}


 if self.initOne then
  for i=1,#self.arrBox do
   self.arrBox[i].state=1
  end
 end

 if self.load then
 end

 self.initOne=false
 self.load=false
end

function p03_l02_z01_game01:on_lb_down()
 local scene=self:scene()
 local selected=scene:overed_object()
 if selected then
  --print(selected:name())
  for a=1,#self.arrBox do
   for b=1,#self.arrBox[a].objectArr do
    if self.arrBox[a].objectArr[b]:name()==selected:name() then
     self.arrBox[a]:changeState(selected)
     self:hide_all()
     scene:play_sound('g_click_lever')
    -- print(self.arrBox[1].state)
     for c=1,#self.arrEff[self.arrBox[1].state][self.arrBox[2].state] do
      self.arrEff[self.arrBox[1].state][self.arrBox[2].state][c]:show()
     end
     self:complete_game()
    end
   end
  end
 end
end

function p03_l02_z01_game01:hide_all()
 for a=1,#self.arrEff do
  for b=1,#self.arrEff[a] do
   for c=1,#self.arrEff[a][b] do
    self.arrEff[a][b][c]:hide()
   end
  end
 end
end

function p03_l02_z01_game01:complete_game()
 local scene=self:scene()
 for a=1,#self.arrLight  do
  if not self.arrLight[a]:visible() then
   return
  end
 end
 scene:play_sound('g_complete')
 scene:execute_action('catch_witch')
end

function p03_l02_z01_game01:save(os)
  os:save(0) -- version

  os:save(true)
  os:save(self.initOne)
  local size=#self.arrBox
  os:save(size)
  for i=1,size do
   os:save(self.arrBox[i].state)
   os:save(self.arrBox[i].direction)
  end

end

function p03_l02_z01_game01:load(is)
  local ver = is:load()

  self.load= is:load()
  self.initOne= is:load()
  local size=is:load()
  for a=1,size do
   self.arrBox[a].state=is:load()

   self.arrBox[a].direction=is:load()
   --print(self.arrBox[a].direction)
  end
end