class 'GameTrain' (Script)

function GameTrain:__init() super()

--    self.points = { self:createPoint({down={'right'},right={'down'}}, Position(255,109)),
--                    self:createPoint({left={'down'}, right={'down'}}, Position(457,109)),
--                    self:createPoint({down={'left'}, right={'left'}}, Position(642,109)),
--                    self:createPoint({down={'right'}, right={'left'}}, Position(826,109)),
--                    self:createPoint({down={'left'}, left={'down'}}, Position(1030,109)),
--
--                    self:createPoint({left={'up', 'right'},right={'up'}, down={'up'}}, Position(255,244)),
--                    self:createPoint({left={'down','right'}, right={'down','left'}, down={'left','right'}, up={'left','right','down'}}, Position(457,244)),
--                    self:createPoint({left={'down', 'right'}, right={'up','left'}, down={'left','up'}}, Position(642,244)),
--                    self:createPoint({right={'down','left'}, down={'up','right'}, left={'right'}}, Position(826,244)),
--                    self:createPoint({left={'up'}, up={'left'}, down={'up'}}, Position(1030,244)),
--
--                    self:createPoint({up={'down','right'}, right={'up','down'}, down={'up','right'}}, Position(255,368)),
--                    self:createPoint({left={'right','down'}, down={'left','right'}, up={'right'}}, Position(457,368)),
--                    self:createPoint({up={'down'}, left={'up', 'down'}, down={'up'}}, Position(642,368)),
--                    self:createPoint({up={'right','down'}, down={'up'}}, Position(826,368)),
--                    self:createPoint({left={'down'}, down={'up'}}, Position(1030,368)),
--
--                    self:createPoint({up={'down','right'}, right={'up','down'}, down={'up','right'}}, Position(255,501)),
--                    self:createPoint({left={'up','right'}, up={'left','right'}, down={'right'}}, Position(457,501)),
--                    self:createPoint({left={'up'},down={'up', 'right'},right={'up'}, up={'right'}}, Position(642,501)),
--                    self:createPoint({left={'up','right'},up={'left','right'},right={'left','up'}, down={'left','right'}}, Position(826,501)),
--                    self:createPoint({up={'left','down'}, down={'up','right'}, left={'up'}}, Position(1030,501)),
--
--                    self:createPoint({up={'right'}, right={'up'}}, Position(255,630)),
--                    self:createPoint({left={'up','right'}, up={'left','right'}, right={'up','left'}}, Position(457,630)),
--                    self:createPoint({left={'up','right'}, right={'up','left'}}, Position(642,630)),
--                    self:createPoint({left={'up','right'}, right={'up','left'}}, Position(826,630)),
--                    self:createPoint({up={'left'}, left={'up'}}, Position(1030,630))}
--
--
--
--
--    self.startPoint = 6
--    self.radius     = 25
--    self.row        = 2
--    self.col        = 1
--
--    self.train      = nil
--    self.smoke      = nil
--    self.from       = 'left'
--
--   self.needRotate  = false
--   self.needMove    = false
--
--   self.trainAngle = 0
--   self.trainNeedAngle = 0
--
--   self.targetAngles = {left=180, down=90, right=0, up=270 }
--
--   self.trainPosition       = 0
--   self.trainTargetPosition = 0
--
--   self.shiftX = 0
--   self.shiftY = 0
--
--   self.rotationDirect = 1
end

function GameTrain:createPoint(options, position)
    return {wayOptions  = options,
            pos         = position}
end

function GameTrain:on_attach()
    self.points = { self:createPoint({down={'right'},right={'down'}}, Position(255,109)),
        self:createPoint({left={'down'}, right={'down'}}, Position(457,109)),
        self:createPoint({down={'left'}, right={'left'}}, Position(642,109)),
        self:createPoint({down={'right'}, right={'left'}}, Position(826,109)),
        self:createPoint({down={'left'}, left={'down'}}, Position(1030,109)),

        self:createPoint({left={'up', 'right'},right={'up'}, down={'up'}}, Position(255,244)),
        self:createPoint({left={'down','right'}, right={'down','left'}, down={'left','right'}, up={'left','right','down'}}, Position(457,244)),
        self:createPoint({left={'down', 'right'}, right={'up','left'}, down={'left','up'}}, Position(642,244)),
        self:createPoint({right={'down','left'}, down={'up','right'}, left={'right'}}, Position(826,244)),
        self:createPoint({left={'up'}, up={'left'}, down={'up'}}, Position(1030,244)),

        self:createPoint({up={'down','right'}, right={'up','down'}, down={'up','right'}}, Position(255,368)),
        self:createPoint({left={'right','down'}, down={'left','right'}, up={'right'}}, Position(457,368)),
        self:createPoint({up={'down'}, left={'up', 'down'}, down={'up'}}, Position(642,368)),
        self:createPoint({up={'right','down'}, down={'up'}}, Position(826,368)),
        self:createPoint({left={'down'}, down={'up'}}, Position(1030,368)),

        self:createPoint({up={'down','right'}, right={'up','down'}, down={'up','right'}}, Position(255,501)),
        self:createPoint({left={'up','right'}, up={'left','right'}, down={'right'}}, Position(457,501)),
        self:createPoint({left={'up'},down={'up', 'right'},right={'up'}, up={'right'}}, Position(642,501)),
        self:createPoint({left={'up','right'},up={'left','right'},right={'left','up'}, down={'left','right'}}, Position(826,501)),
        self:createPoint({up={'left','down'}, down={'up','right'}, left={'up'}}, Position(1030,501)),

        self:createPoint({up={'right'}, right={'up'}}, Position(255,630)),
        self:createPoint({left={'up','right'}, up={'left','right'}, right={'up','left'}}, Position(457,630)),
        self:createPoint({left={'up','right'}, right={'up','left'}}, Position(642,630)),
        self:createPoint({left={'up','right'}, right={'up','left'}}, Position(826,630)),
        self:createPoint({up={'left'}, left={'up'}}, Position(1030,630))}




    self.startPoint = 6
    self.radius     = 25
    self.row        = 2
    self.col        = 1

    self.train      = nil
    self.smoke      = nil
    self.from       = 'left'

    self.needRotate  = false
    self.needMove    = false

    self.trainAngle = 0
    self.trainNeedAngle = 0

    self.targetAngles = {left=180, down=90, right=0, up=270 }

    self.trainPosition       = 0
    self.trainTargetPosition = 0

    self.shiftX = 0
    self.shiftY = 0

    self.rotationDirect = 1



    local scene = self:scene()
    local arrow
    local pnt
    
    	self.soundmov= self:scene():sound('movetrain')

    self.arrows     = { {right=scene:object('arrow11glow1'),down=scene:object('arrow11glow2')},
                        {down=scene:object('arrow12glow3'),right=scene:object('arrow12glow1'), left=scene:object('arrow12glow2')},
                        {left=scene:object('arrow13glow')},
                        {right=scene:object('arrow14glow2'),left=scene:object('arrow14glow1')},
                        {left=scene:object('arrow15glow1'),down=scene:object('arrow15glow2')},

                        {up=scene:object('arrow21glow2'),right=scene:object('arrow21glow3'), left=scene:object('arrow21glow1')},
                        {right=scene:object('arrow22glow3'), down=scene:object('arrow22glow1'), left=scene:object('arrow22glow2')},
                        {up=scene:object('arrow23glow3'),right=scene:object('arrow23glow4'), down=scene:object('arrow23glow1'), left=scene:object('arrow23glow2')},
                        {up=scene:object('arrow24glow1'),right=scene:object('arrow24glow4'), down=scene:object('arrow24glow3'), left=scene:object('arrow24glow2')},
                        {left=scene:object('arrow25glow1'),up=scene:object('arrow25glow2')},

                        {up=scene:object('arrow31glow2'),right=scene:object('arrow31glow1'), down=scene:object('arrow31glow3')},
                        {right=scene:object('arrow32glow2'), down=scene:object('arrow32glow3'), left=scene:object('arrow32glow4')},
                        {up=scene:object('arrow33glow1'),down=scene:object('arrow33glow2')},
                        {right=scene:object('arrow34glow2'), up=scene:object('arrow34glow1'), down=scene:object('arrow34glow3')},
                        {up=scene:object('arrow35glow2'),down=scene:object('arrow35glow1')},

                        {up=scene:object('arrow41glow2'),right=scene:object('arrow41glow3'), down=scene:object('arrow41glow1')},
                        {up=scene:object('arrow42glow3'),right=scene:object('arrow42glow4'), down=scene:object('arrow42glow1'), left=scene:object('arrow42glow2')},
                        {up=scene:object('arrow43glow1'),right=scene:object('arrow43glow2')},
                        {up=scene:object('arrow44glow1'),right=scene:object('arrow44glow3'), left=scene:object('arrow44glow2')},
                        {up=scene:object('arrow45glow4'),right=scene:object('arrow45glow3'), down=scene:object('arrow45glow2'), left=scene:object('arrow45glow1')},

                        {right=scene:object('arrow51glow2'),up=scene:object('arrow51glow1')},
                        {up=scene:object('arrow52glow3'),right=scene:object('arrow52glow2'), left=scene:object('arrow52glow1')},
                        {up=scene:object('arrow53glow3'),right=scene:object('arrow53glow2'), left=scene:object('arrow53glow1')},
                        {up=scene:object('arrow54glow2'),right=scene:object('arrow54glow3'), left=scene:object('arrow54glow1')},
                        {left=scene:object('arrow55glow1'),up=scene:object('arrow55glow2')}
                      }

    self.train = scene:object('paravoz')
    self.smoke = scene:object('smokeanim')

    self:enableArrows()
    local point = self.points[self.startPoint]
    self.train:set_position(Position( point.pos:x()-self.train:dimensions():lx()/2,
                                      point.pos:y()-self.train:dimensions():ly()/2)
                                      )
    self.smoke:set_position(self.train:position())

end

function GameTrain:moveTrain(pointIndex)
    local point = self.points[pointIndex]

    self.trainPosition = self.train:position()
    self.trainTargetPosition = Position( point.pos:x()-self.train:dimensions():lx()/2,
                                         point.pos:y()-self.train:dimensions():ly()/2)

    if self.trainAngle == self.trainNeedAngle then
        self.needMove = true
    else
        self.needRotate = true
    end

end

function GameTrain:on_update(dt)
    if self.needRotate then
        self.train:set_angle( self.trainAngle , Position( self.train:dimensions():lx()/2,
                                                          self.train:dimensions():ly()/2))

        self.smoke:set_angle( self.trainAngle , Position( self.train:dimensions():lx()/2,
                                                          self.train:dimensions():ly()/2))


        self.trainAngle = self.trainAngle + 90*dt*self.rotationDirect

        if self.trainAngle > 360 then
            self.trainAngle = 360 - self.trainAngle
        elseif self.trainAngle < 0 then
            self.trainAngle = 360 + self.trainAngle
        end

      --  print(self.trainAngle)

        if math.abs(self.trainNeedAngle - self.trainAngle) < 2 then
            self.trainAngle = self.trainNeedAngle
            self.train:set_angle( self.trainAngle , Position( self.train:dimensions():lx()/2,
                                                              self.train:dimensions():ly()/2))

            self.smoke:set_angle(self.trainAngle , Position( self.train:dimensions():lx()/2,
                                                              self.train:dimensions():ly()/2))

            self.needRotate = false
            self.needMove = true
        end

    end

   if self.needMove then
       self.train:set_position(Position( self.train:position():x()+self.shiftX*dt,
                                         self.train:position():y()+self.shiftY*dt))
       self.smoke:set_position(self.train:position())

       if math.abs(self.train:position():x()-self.trainTargetPosition:x()) < 1 and math.abs(self.train:position():y()-self.trainTargetPosition:y()) < 2 then
          self.train:set_position(self.trainTargetPosition)

          self.smoke:set_position(self.train:position())


          self.needMove = false


            
           local position = 5*(self.row-1)+self.col

           local point   = self.points[position]
           local arrows   = self.arrows[position]

           if point.wayOptions then
             if point.wayOptions[self.from] then
               local opt = point.wayOptions[self.from]
               if #opt > 1 then
                 self:enableArrows()
                  self.soundmov: fade_out()
               else
                 self:doMove(arrows[opt[1]], opt[1])

               end
             end
           end

       end
   end
end


function GameTrain:on_lb_down()
    local selection = self:scene():overed_object()
    local position  = 5*(self.row-1)+self.col
    local arrows    = self.arrows[position]

    if selection then
    
		
      for k,v in pairs(arrows) do
        if v:name() == selection:name() then

            self:scene():execute_action('click_arrow')
            self:scene():execute_action('start_train')
             self.soundmov:play(true)
            -- self:scene():execute_action('end_game')

          if self:doMove(v, k) then

                self:scene():execute_action('end_game')
           return
          end

        end
      end

    end
end


function GameTrain:doMove(v,k)
  if  v:name() == 'arrow45glow3' then       --      arrow45glow3

    return true
  end

  self:disableArrows()

  self.trainNeedAngle = self.targetAngles[k]

  if self.trainNeedAngle > 180 and self.trainAngle == 0 then
    self.rotationDirect = -1
  else
    if self.trainNeedAngle == 0 and self.trainAngle > 180 then
     self.rotationDirect = 1
    else
      if self.trainNeedAngle < self.trainAngle then
        self.rotationDirect = -1
      else
      self.rotationDirect = 1
      end
    end
  end

  self:updatePosition(k)

  self:moveTrain(5*(self.row-1)+self.col)
 return false
end

function GameTrain:updatePosition(k)
    if k == 'left' then
      self.col = self.col-1
      self.from = 'right'
      self.shiftX = -70
      self.shiftY = 0
    end
    if k == 'up' then
      self.row = self.row-1
      self.from = 'down'
      self.shiftY = -70
      self.shiftX = 0
    end
    if k == 'right' then
      self.col = self.col+1
      self.from = 'left'
      self.shiftX = 70
      self.shiftY = 0
    end
    if k == 'down' then
      self.row = self.row+1
      self.from = 'up'
      self.shiftY = 70
      self.shiftX = 0
    end
end


function GameTrain:disableArrows()
   local position = 5*(self.row-1)+self.col

   local arrows   = self.arrows[position]

   if arrows then
      for k,v in pairs(arrows) do
         v:hide()
      end
   end

end

function GameTrain:enableArrows()
   local position = 5*(self.row-1)+self.col

   local point   = self.points[position]
   local arrows   = self.arrows[position]

   if point.wayOptions then
     if point.wayOptions[self.from] then
       local opt = point.wayOptions[self.from]
       for i=1,#opt do
          arrows[opt[i]]:show()
       end
     end
   end
end