
class 'p02_l01_game1' (Script)

function p02_l01_game1:__init() super()


--CONST
self.STONE1_INHAND='stone1_inhand'
self.STONE2_INHAND='stone2_inhand'
self.STONE3_INHAND='stone3_inhand'
self.NO_STONE='no_stone'
self.COLUMN_MOVE_DOWN='column_move_down'
self.COLUMN_MOVE_UP='column_move_up'
self.COLUMN_NO_MOVE='column_no_move'

self.STONE_MOVE='stone_move'
self.STONE_NO_MOVE='stone_no_move'
 --PARAMS
self.column_factor={0.5,0.9,1.1 }
self.common_shift_column=20
self.speed_move=1
--MEMORY
self.automatic_case=self.NO_STONE
self.move_column_case=self.NO_COLUMN
self.execute_one=true
self.current_mass=0
self.exe_one=true
--NO_GROUP
self.great_array_object= {}
end

function p02_l01_game1:on_attach()
 self:init_object()
 self:create_table()
end

function p02_l01_game1:init_object()
 local scene=self:scene()

 self.input_stone1=scene:object('input_stone1')
 self.input_stone2=scene:object('input_stone2')
 self.input_stone3=scene:object('input_stone3')

 self.stone_mask01=scene:object('stone_mask01')
 self.stone_mask02=scene:object('stone_mask02')
 self.stone_mask03=scene:object('stone_mask03')

 self.m01_stone01_01=scene:object('m01_stone01-01')
 self.m01_stone01_02=scene:object('m01_stone01-02')
 self.m01_stone01_03=scene:object('m01_stone01-03')

 self.m01_stone02_01=scene:object('m01_stone02-01')
 self.m01_stone02_02=scene:object('m01_stone02-02')
 self.m01_stone02_03=scene:object('m01_stone02-03')

 self.m01_stone03_01=scene:object('m01_stone03-01')
 self.m01_stone03_02=scene:object('m01_stone03-02')
 self.m01_stone03_03=scene:object('m01_stone03-03')

 self.column_1=scene:object('column_1')
 self.column_2=scene:object('column_2')
 self.column_3=scene:object('column_3')

end

function p02_l01_game1:create_table()
 local input_stone={self.input_stone1,self.input_stone2,self.input_stone3 }
 local input_stone_position={}
 local input_stone_hide_position={Position(-200,-200),Position(-200,-200),Position(-200,-200)}

 local stone_mask={self.stone_mask01, self.stone_mask02,self.stone_mask03 }
 local stone_mask_position={}
 local stone_mass={}

 local column= {self.column_1,self.column_2,self.column_3 }
 local column_position={}
 local column_start_position={}
 local column_finish_position={}


 local stone={{self.m01_stone01_01,self.m01_stone01_02,self.m01_stone01_03},
              {self.m01_stone02_01,self.m01_stone02_02,self.m01_stone02_03},
              {self.m01_stone03_01,self.m01_stone03_02,self.m01_stone03_03}}
 local stone_position={{0},{0},{0}}
 local stone_start_position={{0},{0},{0}}


 for i=1,3 do
  input_stone_position[i]=input_stone[i]:position()
  stone_mask_position[i]=stone_mask[i]:position()
  column_position[i]=column[i]:position()
  column_start_position[i]=column[i]:position()
  for j=1,3 do
   --print(stone[i][j]:name())
   stone_position[i][j]= stone[i][j]:position()
   stone_start_position[i][j]=stone[i][j]:position()
  end
 end



 if self.execute_one then

  stone_mass={self.common_shift_column/self.column_factor[1],
              (self.common_shift_column+column[2]:dimensions():ly()-column[1]:dimensions():ly())/self.column_factor[2],
              (self.common_shift_column+column[3]:dimensions():ly()-column[1]:dimensions():ly())/self.column_factor[3] }

  local data_arr={}
  --rawset(self.great_array_object,'input_stone',data_arr)
  --rawset(data_arr,'object',input_stone)
 -- rawset(data_arr,'position',input_stone_position)

 -- data_arr={}
  --rawset(self.great_array_object,'stone_mask',data_arr)
 -- rawset(data_arr,'object',stone_mask)
 -- rawset(data_arr,'position',stone_mask_position)

 -- data_arr={}
 -- rawset(self.great_array_object,'column',data_arr)
  --rawset(data_arr,'object',column)
  --rawset(data_arr,'position',column_position)

 -- data_arr={}
 -- rawset(self.great_array_object,'stone',data_arr)
  --rawset(data_arr,'object',stone)
 -- rawset(data_arr,'position',stone_position)


  rawset(self.great_array_object,'input_stone',data_arr)
   local arr={}
  for k=1,3 do
   arr={}
   rawset(data_arr,k,arr)
   rawset(arr,'object',input_stone[k])
   rawset(arr,'position',input_stone_position[k])
   rawset(arr,'position_hide',input_stone_hide_position[k])
  end

  data_arr={}
  rawset(self.great_array_object,'stone_mask',data_arr)
  for s=1,3 do
   arr={}
   rawset(data_arr,s,arr)
   rawset(arr,'object',stone_mask[s])
   rawset(arr,'position',stone_mask_position[s])
   rawset(arr,'mass',stone_mass[s])
  end

  data_arr={}
  rawset(self.great_array_object,'column',data_arr)
  for l=1,3 do
   arr={}
   rawset(data_arr,l,arr)
   rawset(arr,'object',column[l])
   rawset(arr,'position',column_position[l])
   rawset(arr,'start_position',column_start_position[l])
   rawset(arr,'factor',self.column_factor[l])
   rawset(arr,'state',self.COLUMN_NO_MOVE)
   rawset(arr,'mass',0)
   rawset(arr,'time',0)
   rawset(arr,'pos_y',column_position[l]:y())
   --rawset(arr,'')
  end

  data_arr={}
  rawset(self.great_array_object,'stone',data_arr)
  for p=1,3 do
   local arr_st={}
   rawset(data_arr,p,arr_st)
   for t=1,3 do
    local arr_st2={}
    rawset(arr_st,t,arr_st2)
    rawset(arr_st2,'object',stone[p][t])
    rawset(arr_st2,'position',stone_position[p][t])
    rawset(arr_st2,'start_position',stone_start_position[p][t])
    rawset(arr,'time',0)
    rawset(arr_st2,'pos_y',stone_position[p][t]:y())
    rawset(arr_st2,'state',self.STONE_NO_MOVE)

   end
  end

  self.execute_one=false
 end
end




function p02_l01_game1:on_lb_down()
 --print(self.great_array_object['stone'][3][3]['pos_y'])
 --print(self.great_array_object['stone_mask'][2]['object']:position():x())
 self:put_stone()
end

function p02_l01_game1:put_stone()
local scene=self:scene()
local selected=scene:overed_object()

 for i=1,3 do
  for j=1,3 do
   if self.great_array_object['input_stone'][i]['object']:is_crossing(self.great_array_object['stone_mask'][j]['object']) then
      --print('good')
      self:cross_handler(self.great_array_object['stone_mask'][j],
                         self.great_array_object['input_stone'][i],self.great_array_object['stone'][i][j],i)

   end
  end
 end

 if selected then
  for i=1,#self.great_array_object['stone'] do
  for j=1,#self.great_array_object['stone'][i] do
    if selected:name()==self.great_array_object['stone'][i][j]['object']:name() then
    -- print('tut')
    self.great_array_object['column'][i]['state']= self.COLUMN_MOVE_UP
      self.great_array_object['column'][i]['time']=0
      self:show_input(self.great_array_object['input_stone'][i],true)
      --self.great_array_object['stone'][i][j]['object']:set_position(self.great_array_object['stone'][i][j]['start_position'])
      self.great_array_object['stone'][i][j]['pos_y']=  self.great_array_object['stone'][i][j]['start_position']:y()
     self.great_array_object['stone'][i][j]['state']=self.STONE_NO_MOVE            -- tut

    end
   end
  end
 end
 self:stone_inhand_in_no_stone(0)
end

function p02_l01_game1:cross_handler(obj1_mask,obj2_input,obj3_stone,i)
 local st_mask=obj1_mask
 local st_input=obj2_input
 local st_real=obj3_stone
 self:scene():execute_action('remove_inventory')
 self:stone_inhand_in_no_stone(0)

 --st_input['object']:hide()

 self:hide_input(st_input)
 st_real['object']:show()
 -- ----------------------

 st_real['state']=self.STONE_MOVE
 self.great_array_object['column'][i]['state']= self.COLUMN_MOVE_DOWN
 self.great_array_object['column'][i]['mass']=st_mask['mass']
 self.great_array_object['column'][i]['time']=0
 st_real['time']=0
 st_real['y_pos']=st_real['start_position']:y()
end


-- HELP----------------------------------
function p02_l01_game1:hide_input(st_input)
  --print(st_input['position_hide']:x())
  st_input['object']:set_position(st_input['position_hide'],true)
end

function p02_l01_game1:show_input(st_input)
  st_input['object']:set_position(st_input['position'],true)
end
-- ------------------------------------------------


function p02_l01_game1:on_update(dt)
 if self.great_array_object['stone'][1][1]['object']:visible()
         and self.great_array_object['stone'][2][2]['object']:visible()
         and self.great_array_object['stone'][3][3]['object']:visible()  then
  if self.exe_one then
   self:scene():execute_action('complite_game')
   self.exe_one=false
  end
 end

self:capture_switch()

 for i=1,#self.great_array_object['column'] do
   self:move_column(self.great_array_object['column'][i],dt)
   for j=1,#self.great_array_object['stone'][i] do
    self:move_column(self.great_array_object['stone'][i][j])
   end
 end

end


function p02_l01_game1:move_stone(stone)
end
function p02_l01_game1:move_column(column,dt)
 local state=column['state']
 local dt=dt

 if state==self.COLUMN_MOVE_DOWN then
 -- self.speed_move
  for i=1,#self.great_array_object['stone'] do
   for j=1,#self.great_array_object['stone'][i] do
     if self.great_array_object['stone'][i][j]['state']==self.STONE_MOVE  then
     -- self.great_array_object['stone'][i][j]['pos_y']= self.great_array_object['stone'][i][j]['start_position']:y()
      self:load_move_dawn(column,self.great_array_object['stone'][i][j],dt)
     end
   end
  end
 end

 if state==self.COLUMN_MOVE_UP then
  self:load_move_up(column,dt)
 end

 if state==self.COLUMN_NO_MOVE then

 end


end

function p02_l01_game1:load_move_up(column,dt)
  local finish_pos=column['start_position']:y()
 column['time']=column['time']+dt

 if column['pos_y']>finish_pos then
  column['pos_y']=column['pos_y']-column['time']*self.speed_move
 else
  column['state']=self.COLUMN_NO_MOVE
 end
 column['object']:set_position(Position(column['object']:position():x(),column['pos_y']),true)
end

function p02_l01_game1:load_move_dawn(column,stone,dt)
 local finish_pos=column['start_position']:y()+column['mass']*column['factor']
 column['time']=column['time']+dt
 if column['pos_y']<finish_pos then
  column['pos_y']=column['pos_y']+column['time']*self.speed_move
  stone['pos_y']=stone['pos_y']+column['time']*self.speed_move
 else
  column['state']=self.COLUMN_NO_MOVE
  stone['state']= self.STONE_NO_MOVE
 end
 column['object']:set_position(Position(column['object']:position():x(),column['pos_y']),true)
 stone['object']:set_position(Position(stone['object']:position():x(),stone['pos_y']),true)
end




function p02_l01_game1:capture_switch()
  local scene = self:scene()

 if self.automatic_case==self.NO_STONE then
  self:load_no_stone()

  if scene:have_mark('stone1_inhand') then

    self.automatic_case=self.STONE1_INHAND
  end

  if scene:have_mark('stone2_inhand') then

     self.automatic_case=self.STONE2_INHAND
  end

  if scene:have_mark('stone3_inhand') then

     self.automatic_case=self.STONE3_INHAND
  end
 end

 if self.automatic_case==self.STONE1_INHAND then
  self:load_stone_inhand(1)
  if true then
   self.automatic_case=self.NO_STONE
   self:stone_inhand_in_no_stone(1)
  end
 end

 if self.automatic_case==self.STONE2_INHAND then
  self:load_stone_inhand(2)
  if true then
   self:stone_inhand_in_no_stone(2)
   self.automatic_case=self.NO_STONE
  end
 end

 if self.automatic_case==self.STONE3_INHAND then
  self:load_stone_inhand(3)
  if true then
   self:stone_inhand_in_no_stone(3)
   self.automatic_case=self.NO_STONE
  end
 end
end

-- ---------------------------

function p02_l01_game1:load_stone_inhand(i)
end

function p02_l01_game1:load_no_stone()
end
-- ---------------------------

function p02_l01_game1:stone_inhand_in_no_stone(i)
 local scene=self:scene()

 scene:reset_mark('stone1_inhand')
 scene:reset_mark('stone2_inhand')
 scene:reset_mark('stone3_inhand')
 for j=1,#self.great_array_object['stone_mask'] do
  if j~=i then
    self:release_capture(self.great_array_object['stone_mask'][j]['object'])
    self.great_array_object['stone_mask'][j]['object']:set_position(self.great_array_object['stone_mask'][j]['position'])
  else
    self.great_array_object['stone_mask'][j]['object']:set_position(Position(scene:cursor_position():x()-self.great_array_object['stone_mask'][j]['object']:dimensions():lx()/2
                                                            ,scene:cursor_position():y()-self.great_array_object['stone_mask'][j]['object']:dimensions():ly()/2))
    self:capture_object(self.great_array_object['stone_mask'][j]['object'])
  end
 end
end
