class 'GameKey' (Script)

--     * a tttty i
--     * a * c y i
--     kkkk* c * o
--     uuuu* d * o
--     * * * d * *
--     * * * * * *

-- +a +t +y +i +o +k +u +d +c


function GameKey:__init() super()
    self.items   = {--k    1
      {type=1, moveType=1, col=1, row=3},
      --a                  2
      {type=0, moveType=0, col=2, row=1},
      --u                    3
      {type=0, moveType=1, col=1, row=4},
      --t                      4
      {type=0, moveType=1, col=3, row=1},
      --i                     5
      {type=0, moveType=0, col=6, row=1},
      --o                     6
      {type=0, moveType=0, col=6, row=3},
      --d                     7
      {type=0, moveType=0, col=4, row=4},
      --c                     8
      {type=0, moveType=0, col=4, row=2},
      --y                     9
      {type=0, moveType=0, col=5, row=1}}
    
    self.cellSize = 100
    self.offsetX = 208
    self.offsetY = 89

    self.map ={{0,1,1,1,1,1},
              {0,1,0,1,1,1},
              {1,1,0,1,0,1},
              {1,1,0,1,0,1},
              {0,0,0,1,0,0},
              {0,0,0,0,0,0} }

    self.dirDown = 1
    self.dirRight = 1

    self.step = 2

    self.activeItem = nil
    self.prevMousePos = nil
    self.needItemMoving = false
    self.needUpdateDirection = true
end

function GameKey:on_attach()
    local scene = self:scene()
    local item

    for i=1,#self.items do
        item = self.items[i]
        rawset(item, 'object', scene:object('item'..i))
    end
end

function GameKey:on_mouse_move()
  if self.activeItem and self.needUpdateDirection and self.prevMousePos ~= self:scene():cursor_position() then
     if self.prevMousePos:x() < self:scene():cursor_position():x() then
       self.dirRight = 1
      else
       self.dirRight = -1
      end

      if self.prevMousePos:y() < self:scene():cursor_position():y() then
       self.dirDown = 1
      else
       self.dirDown = -1
      end

      self.needUpdateDirection = false

      self:move()

      self.activeItem           = nil
      self.prevMousePos         = nil
      self.needUpdateDirection  = false
  end
end

function GameKey:move()
      if self.activeItem.moveType == 0 then
        if self.dirDown == 1 and self.activeItem.row<5 and self.map[self.activeItem.row+2][self.activeItem.col] == 0 then
          self:updateMap(self.activeItem.row, self.activeItem.col, self.activeItem.moveType, self.dirDown)
          self.activeItem.row=self.activeItem.row+1
          self:updateActiveItemTarget()
        elseif self.activeItem.row>1 and self.map[self.activeItem.row-1][self.activeItem.col] == 0 then
          self:updateMap(self.activeItem.row, self.activeItem.col, self.activeItem.moveType, self.dirDown)
          self.activeItem.row=self.activeItem.row-1
          self:updateActiveItemTarget()
        end
      else
        if self.dirRight == 1 and self.activeItem.col<5 and self.map[self.activeItem.row][self.activeItem.col+2] == 0 then
          self:updateMap(self.activeItem.row, self.activeItem.col, self.activeItem.moveType, self.dirRight)
          self.activeItem.col=self.activeItem.col+1
          self:updateActiveItemTarget()
        elseif self.activeItem.col>1 and self.map[self.activeItem.row][self.activeItem.col-1] == 0 then
          self:updateMap(self.activeItem.row, self.activeItem.col, self.activeItem.moveType, self.dirRight)
          self.activeItem.col=self.activeItem.col-1
          self:updateActiveItemTarget()
        end
      end

end


function GameKey:updateActiveItemTarget()
  self.activeItem.object:set_position(Position((self.activeItem.col-1)*self.cellSize+self.offsetX, (self.activeItem.row-1)*self.cellSize+self.offsetY))

  if self.activeItem.type == 1 and self.activeItem.col == 5 then
      self:scene():execute_action('complete_GameKey')
      self:remove()
   end
end


function GameKey:updateMap(row, col, moveType, dir)
  if moveType == 0 then
     if dir > 0 then
      self.map[row][col]=0
      self.map[row+2][col]=1
    else
       self.map[row+1][col]=0
       self.map[row-1][col]=1
    end
  else
     if dir > 0 then
      self.map[row][col]=0
      self.map[row][col+2]=1
    else
       self.map[row][col+1]=0
       self.map[row][col-1]=1
    end
  end

end

function GameKey:on_lb_down()
    self.needUpdateDirection = true

    self.prevMousePos = self:scene():cursor_position()

    local scene     = self:scene()
    local selection = scene:overed_object()
    
    if selection then
      for i=1,#self.items do
        if selection:name() == self.items[i].object:name() then
           self.activeItem = self.items[i]
           break
        end
      end
    end
end