#==============================================================================
# 仿DQ系列的马车行走
#
# 使用说明:新建2个角色,行走图分别设置为马和车厢。
# 开启对应的开关后,在地图备注中写有<马车Style>的地图上,就能以马车队列行进。
# 在<马车Style>的地图上开启开关时,如果需要要刷新队列,请在事件中执行脚本:
# $game_player.refresh
#==============================================================================
module CarriageStyle
  HORSE    = 10  #设置行走图为马的角色ID
  CARRIAGE = 11  #设置行走图为马车的角色ID
  SWITCH   = 120   #控制马车行走的公共开关
end
 
#==============================================================================
# ■ Game_Follower
#==============================================================================
class Game_Follower
attr_accessor :need_refresh
  #--------------------------------------------------------------------------
  # ● 获取对应的角色(override method)
  #--------------------------------------------------------------------------
  def actor
    if @need_refresh
      $carriage_array = $game_party.battle_members
      if $game_map.carriage_style? && $game_switches[CarriageStyle::SWITCH]
        $carriage_array.insert(1, $game_actors[CarriageStyle::HORSE],
        $game_actors[CarriageStyle::CARRIAGE])
      end
      @need_refresh = false
    end
    $carriage_array[@member_index]
  end
  #--------------------------------------------------------------------------
  # ● 追随带队角色(override method)
  #--------------------------------------------------------------------------
  def chase_preceding_character
    unless moving?
      case @member_index
      when 1..3; chase(@preceding_character.x, @preceding_character.y)
      when 4;    left_side_chase(@preceding_character)
      when 5;    right_side_chase(@preceding_character); end
    end
  end
  #--------------------------------------------------------------------------
  # ● 追随(new method)
  #--------------------------------------------------------------------------
  def chase(x, y)
    sx = distance_x_from(x)
    sy = distance_y_from(y)
    if sx != 0 && sy != 0
      move_diagonal(sx > 0 ? 4 : 6, sy > 0 ? 8 : 2)
    elsif sx != 0
      move_straight(sx > 0 ? 4 : 6)
    elsif sy != 0
      move_straight(sy > 0 ? 8 : 2)
    end
  end
  #--------------------------------------------------------------------------
  # ● 左侧人物的移动(new method)
  #--------------------------------------------------------------------------
  def left_side_chase(character)
    if left_side_passable?(character) && !$game_player.followers.gathering?
      sx = distance_x_from(character.left_side_x)
      sy = distance_y_from(character.left_side_y)
      if (sx.abs + sy.abs) >= 3
        moveto(character.back_side_x, character.back_side_y)
      end
      chase(character.left_side_x, character.left_side_y)
    else
      chase(character.x, character.y)
    end
    @direction = character.direction
  end
  #--------------------------------------------------------------------------
  # ● 右侧人物的移动(new method)
  #--------------------------------------------------------------------------
  def right_side_chase(character)
    if right_side_passable?(character) && !$game_player.followers.gathering?
      sx = distance_x_from(character.right_side_x)
      sy = distance_y_from(character.right_side_y)
      if (sx.abs + sy.abs) >= 3
        moveto(character.back_side_x, character.back_side_y)
      end
      chase(character.right_side_x, character.right_side_y)
    else
      chase(character.x, character.y)
    end
    @direction = character.direction
  end
  #--------------------------------------------------------------------------
  # ● 得到目标背后的坐标(new method)
  #--------------------------------------------------------------------------
  def back_side_x
    case self.direction
    when 2; x = 0
    when 4; x = 1
    when 6; x = -1
    when 8; x = 0; end
    $game_map.round_x(x += self.x)
  end
  def back_side_y
    case self.direction
    when 2; y = -1
    when 4; y = 0
    when 6; y = 0
    when 8; y = 1; end
    $game_map.round_y(y += self.y)
  end
  #--------------------------------------------------------------------------
  # ● 得到目标左侧的坐标(new methord)
  #--------------------------------------------------------------------------
  def left_side_x
    case self.direction
    when 2; x = 1
    when 4; x = 0
    when 6; x = 0
    when 8; x = -1; end
    $game_map.round_x(x += self.x)
  end
  def left_side_y
    case self.direction
    when 2; y = 0
    when 4; y = 1
    when 6; y = -1
    when 8; y = 0; end
    $game_map.round_x(y += self.y)
  end
  #--------------------------------------------------------------------------
  # ● 得到目标右侧的坐标(new method)
  #--------------------------------------------------------------------------
  def right_side_x
    case self.direction
    when 2; x = -1
    when 4; x = 0
    when 6; x = 0
    when 8; x = 1; end
    $game_map.round_x(x += self.x)
  end
  def right_side_y
    case self.direction
    when 2; y = 0
    when 4; y = -1
    when 6; y = 1
    when 8; y = 0; end
    $game_map.round_y(y += self.y)
  end
  #--------------------------------------------------------------------------
  # ● 判定HORSE左侧通行度(new method)
  #--------------------------------------------------------------------------
  def left_side_passable?(character)
    $game_map.passable?(character.left_side_x,character.left_side_y,
    character.direction) &&
    !collide_with_events?(character.left_side_x, character.left_side_y)
  end
  #--------------------------------------------------------------------------
  # ● 判定HORSE右侧通行度(new method)
  #--------------------------------------------------------------------------
  def right_side_passable?(character)
    $game_map.passable?(character.right_side_x,character.right_side_y,
    character.direction) &&
    !collide_with_events?(character.right_side_x, character.right_side_y)
  end
end
 
#==============================================================================
# ■ Scene_Map(new method)
#==============================================================================
class Game_Map
  def carriage_style?
    return if map_id == 0
    object = load_data(sprintf("Data/Map%03d.rvdata2", map_id))
    object.note.each_line do |line|
      return true if line.include?("<马车Style>")
    end
    return false
  end
end
 
#==============================================================================
# ■ Scene_Map(override method)
#==============================================================================
class Scene_Map
  def perform_transfer
    pre_transfer
    $game_player.perform_transfer
    $game_player.refresh
    post_transfer
  end
end
 
#==============================================================================
# ■ Game_Followers(alias method)
#==============================================================================
class Game_Followers
  #--------------------------------------------------------------------------
  # ● 初始化(alias method)
  #--------------------------------------------------------------------------
  alias old_initialize initialize
  def initialize(leader)
    old_initialize(leader)
    @data.push(Game_Follower.new(4, @data[0]))
    @data.push(Game_Follower.new(5, @data[0]))
  end
  #--------------------------------------------------------------------------
  # ● 刷新(alias method)
  #--------------------------------------------------------------------------
  alias old_refresh refresh
  def refresh
    @data[0].need_refresh = true
    old_refresh
  end
end