八方向行走脚本

# 脚本功能:八方向走与多帧移动之图片修正__ver1.1。

# 更新日期:2005年8月6日

# 更新内容:增加斜方向触发,增加斜方向面向角色(1步之内)

# 使用方法:将本脚本插入到main之前。如果你使用了雅土版的八方向走脚本,请确保这个脚本的顺序位置,在雅土八方向走脚本的后面。

# 预先处理:请输入每一步的帧数和总共可用的方向数

$c3_每一步的帧数 = 4
$c3_总共可用的方向数 = 4 #——建议不要修改这个。如果要伪8方向的,就用伪的好了。

# 图片处理与功能说明:

# 1、每一步的帧数:
#    众所周知,RMXP的移动行走图是一个方向共有4帧,很多人都觉得这个帧数有点少。
# 使用这个脚本之后,只要将 $c3_每一步的帧数 这个变量设置一个需要的帧数即可修改
# 单方向移动帧数为相应输入值。修改后,需要用photoshop将所有用到的素材的横排
# 调整为相应帧数,比如输入了8则要将每一行设置8个图像(即每一个行走图有8列)。

# 2、可用方向调整(可用数量:4、8):
#    当为4方向时没有任何变化,还是一个图4行,上左右下4个方向。
#    如果想使用8方向走,将需要八方向行走的行走图在延伸扩大的画布中按照左下、右下、左上、右上继续排布图像。
# 即,行走图图片从上到下的面向排列顺序为:下,左,右,上,左下,右下,左上,右上。
#    至于不需要8方向走的普通的NPC(character),使用photoshop将画布向下扩大一倍即可使用。
#    需要注意的是,如果需要斜方向飞鸟,请不要忘记自制素材。

# 特别提示:   
#     使用本脚本前请先考虑清楚是否要做这种效果。因为使用本脚本后需要用photoshop处理character行走图素材
# 虽然这种处理用photoshop定义动作只需要5分钟即可全部完成,但在制作阶段会感觉不是很爽(具体的用了才知道)
# 作者的建议是,如果你没有足够的制作经验,使用这个脚本得不偿失。请确保自己的能力属于“高手”级别!

# 附赠功能:
#     可以让NPC角色随机8方向走,方法是:NPC角色移动路线为“自定义”,然后自定义里面使用脚本,输入c8即可
#     可以让NPC角色随机4斜角方向走,方法是:NPC角色移动路线为“自定义”,然后自定义里面使用脚本,输入c4即可
#     可以使用真·斜4方向行走,参考脚本53行开始
# 作者:carol3
###########################################################################################################################
###########################################################################################################################
#==============================================================================
# ■ Game_Event
#------------------------------------------------------------------------------
#  处理事件的类。条件判断、事件页的切换、并行处理、执行事件功能
# 在 Game_Map 类的内部使用。
#==============================================================================
class Game_Event < Game_Character
 #——————————————————————————————————————
 # 用来返回名称
 #——————————————————————————————————————
 def name
   return @event.name
 end 
 def name=(newname)
   @event.name = newname
 end
end

 

 

class Game_Player < Game_Character
  TIME_LIMIT = 20
  def update
    last_moving = moving?
    unless moving? or $game_system.map_interpreter.running? or
           @move_route_forcing or $game_temp.message_window_showing
      # 用井号后面的东西替代前面的,就可以实现斜4方向走
      case Input.dir8
      when 2
        if $game_switches[2]
          move_down
        else
          move_lower_left
        end           
      when 4
        if $game_switches[2]
          move_left
        else
          move_upper_left
        end
      when 6
        if $game_switches[2]
          move_right
        else
          move_lower_right
        end
      when 8
        if $game_switches[2]
          move_up
        else
          move_upper_right       
        end
      when 1
        move_lower_left
      when 3
        move_lower_right
      when 7
        move_upper_left
      when 9
        move_upper_right
      end
    end
    # 本地变量记忆坐标
    last_real_x = @real_x
    last_real_y = @real_y
    super
    # 角色向下移动、画面上的位置在中央下方的情况下
    if @real_y > last_real_y and @real_y - $game_map.display_y > CENTER_Y
      # 画面向下卷动
      $game_map.scroll_down(@real_y - last_real_y)
    end
    # 角色向左移动、画面上的位置在中央左方的情况下
    if @real_x < last_real_x and @real_x - $game_map.display_x < CENTER_X
      # 画面向左卷动
      $game_map.scroll_left(last_real_x - @real_x)
    end
    # 角色向右移动、画面上的位置在中央右方的情况下
    if @real_x > last_real_x and @real_x - $game_map.display_x > CENTER_X
      # 画面向右卷动
      $game_map.scroll_right(@real_x - last_real_x)
    end
    # 角色向上移动、画面上的位置在中央上方的情况下
    if @real_y < last_real_y and @real_y - $game_map.display_y < CENTER_Y
      # 画面向上卷动
      $game_map.scroll_up(last_real_y - @real_y)
    end
    # 不在移动中的情况下
    unless moving?
      # 上次主角移动中的情况
      if last_moving
        # 与同位置的事件接触就判定为事件启动
        result = check_event_trigger_here([1,2])
        # 没有可以启动的事件的情况下
        if result == false
          # 调试模式为 ON 并且按下 CTRL 键的情况下除外
          unless $DEBUG and Input.press?(Input::CTRL)
            # 遇敌计数下降
            if @encounter_count > 0
              @encounter_count -= 1
            end
          end
        end
      end
      # 按下 C 键的情况下
      if Input.trigger?(Input::C)
        # 判定为同位置以及正面的事件启动
        check_event_trigger_here([0])
        check_event_trigger_there([0,1,2])
      end
    end
  end
  #--------------------------------------------------------------------------
  # ● 正面事件的启动判定
  #--------------------------------------------------------------------------
  def check_event_trigger_there(triggers)
    result = false
    # 事件执行中的情况下
    if $game_system.map_interpreter.running?
      return result
    end
    # 计算正面坐标
    new_x = @x
    new_y = @y
    case @direction
   when 1
      new_x -= 1
      new_y += 1
    when 2
      new_y += 1
    when 3
      new_x += 1
      new_y += 1
    when 4
      new_x -= 1
    when 6
      new_x += 1
    when 7
      new_x -= 1
      new_y -= 1
    when 8
      new_y -= 1
    when 9
      new_x += 1
      new_y -= 1
    end
    # 全部事件的循环
    for event in $game_map.events.values
      # 事件坐标与目标一致的情况下
      if event.x == new_x and event.y == new_y and
         triggers.include?(event.trigger)
        # 跳跃中以外的情况下、启动判定是正面的事件
        if not event.jumping? and not event.over_trigger?
          event.start
          result = true
        end
      end
    end
    # 找不到符合条件的事件的情况下
    if result == false
      # 正面的元件是计数器的情况下
      if $game_map.counter?(new_x, new_y)
        # 计算 1 元件里侧的坐标
        new_x += (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
        new_y += (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
        # 全事件的循环
        for event in $game_map.events.values
          # 事件坐标与目标一致的情况下
          if event.x == new_x and event.y == new_y and
             triggers.include?(event.trigger)
            # 跳跃中以外的情况下、启动判定是正面的事件
            if not event.jumping? and not event.over_trigger?
              event.start
              result = true
            end
          end
        end
      end
    end
    return result
  end
  #--------------------------------------------------------------------------
  # ● 向左下移动
  #--------------------------------------------------------------------------
  def move_lower_left
    # 没有固定面向的场合
    unless @direction_fix
      # 朝向是右的情况下适合的面是左面、朝向是上的情况下适合的面是下面
      @direction = 3#(@direction == 6 ? 4 : @direction == 8 ? 2 : @direction)
    end
    # 下→左、左→下 的通道可以通行的情况下
    if (passable?(@x, @y, 2) and passable?(@x, @y + 1, 4)) or
       (passable?(@x, @y, 4) and passable?(@x - 1, @y, 2))
      # 更新坐标
      @x -= 1
      @y += 1
      # 增加步数
      increase_steps
    else
      check_event_trigger_touch(@x-1, @y+1)
    end
  end
  #--------------------------------------------------------------------------
  # ● 向右下移动
  #--------------------------------------------------------------------------
  def move_lower_right
    # 没有固定面向的场合
    unless @direction_fix
      # 朝向是右的情况下适合的面是左面、朝向是上的情况下适合的面是下面
      @direction = 9#(@direction == 4 ? 6 : @direction == 8 ? 2 : @direction)
    end
    # 下→右、右→下 的通道可以通行的情况下
    if (passable?(@x, @y, 2) and passable?(@x, @y + 1, 6)) or
       (passable?(@x, @y, 6) and passable?(@x + 1, @y, 2))
      # 更新坐标
      @x += 1
      @y += 1
      # 增加步数
      increase_steps
    else
      check_event_trigger_touch(@x+1, @y+1)
    end
  end
  #--------------------------------------------------------------------------
  # ● 向左上移动
  #--------------------------------------------------------------------------
  def move_upper_left
    # 没有固定面向的场合
    unless @direction_fix
      # 朝向是右的情况下适合的面是左面、朝向是上的情况下适合的面是下面
      @direction = 7#(@direction == 6 ? 4 : @direction == 2 ? 8 : @direction)
    end
    # 上→左、左→上 的通道可以通行的情况下
    if (passable?(@x, @y, 8) and passable?(@x, @y - 1, 4)) or
       (passable?(@x, @y, 4) and passable?(@x - 1, @y, 8))
      # 更新坐标
      @x -= 1
      @y -= 1
      # 增加步数
      increase_steps
    else
      check_event_trigger_touch(@x-1, @y-1)
    end
  end
  #--------------------------------------------------------------------------
  # ● 向右上移动
  #--------------------------------------------------------------------------
  def move_upper_right
    # 没有固定面向的场合
    unless @direction_fix
      # 朝向是右的情况下适合的面是左面、朝向是上的情况下适合的面是下面
      @direction = 1#(@direction == 4 ? 6 : @direction == 2 ? 8 : @direction)
    end
    # 上→右、右→上 的通道可以通行的情况下
    if (passable?(@x, @y, 8) and passable?(@x, @y - 1, 6)) or
       (passable?(@x, @y, 6) and passable?(@x + 1, @y, 8))
      # 更新坐标
      @x += 1
      @y -= 1
      # 增加步数
      increase_steps
    else
      check_event_trigger_touch(@x+1, @y-1)
    end
  end
end


class Sprite_Character < RPG::Sprite
    attr_accessor :character                # 角色
   def initialize(viewport, character = nil)
   name = character.name
  super(viewport)
   @character = character
   @namesprite = Sprite.new
   @namesprite.bitmap = Bitmap.new(160, 48)
   @namesprite.bitmap.font.name = "黑体"
   @namesprite.bitmap.font.size = 16
   @namesprite.bitmap.font.color.set(255, 255, 0)
   @evname = name
   @evname_split = name.split(/,/)[0]
   if name[0, 2]=="EV"
     @evname_split = " "
   end
   if name.split(/,/)[1] != nil
     case name.split(/,/)[1]
     when "0"
       @namesprite.bitmap.font.color.set(255, 255, 255)
     when "1"
       @namesprite.bitmap.font.color.set(128, 128, 255)
     when "2"
       @namesprite.bitmap.font.color.set(255, 128, 128)
     when "3"
       @namesprite.bitmap.font.color.set(128, 255, 128)
     when "4"
       @namesprite.bitmap.font.color.set(128, 255, 255)
     when "5"
       @namesprite.bitmap.font.color.set(255, 128, 255)
     when "6"
       @namesprite.bitmap.font.color.set(255, 255, 128)
     when "7"
       @namesprite.bitmap.font.color.set(192, 192, 192)
     else
       @namesprite.bitmap.font.color.set(255, 255, 255)
     end
   end
   if @evname_split != "" and @evname_split != nil
     @namesprite.bitmap.draw_text(0, 0, 160, 36, @evname_split, 1)
   end
   update
 end
 
  def update
    super
    # 元件 ID、文件名、色相与现在的情况存在差异的情况下
    if @tile_id != @character.tile_id or
       @character_name != @character.character_name or
       @character_hue != @character.character_hue
      # 记忆元件 ID 与文件名、色相
      @tile_id = @character.tile_id
      @character_name = @character.character_name
      @character_hue = @character.character_hue
      # 元件 ID 为有效值的情况下
      if @tile_id >= 384
        self.bitmap = RPG::Cache.tile($game_map.tileset_name,
          @tile_id, @character.character_hue)
        self.src_rect.set(0, 0, 32, 32)
        self.ox = 16
        self.oy = 32
      # 元件 ID 为无效值的情况下
      else
        self.bitmap = RPG::Cache.character(@character.character_name,
          @character.character_hue)
        @cw = bitmap.width / $c3_每一步的帧数
          @ch = bitmap.height / 4
        self.ox = @cw / 2
        self.oy = @ch
      end
    end
   
     if @evname != @character.name
     @namesprite.bitmap.clear
     @evname = @character.name
     @evname_split = @character.name.split(/,/)[0]
     if @character.name.split(/,/)[1] != nil
       case @character.name.split(/,/)[1]
       when "0"
         @namesprite.bitmap.font.color.set(255, 255, 255)
       when "1"
         @namesprite.bitmap.font.color.set(128, 128, 255)
       when "2"
         @namesprite.bitmap.font.color.set(255, 128, 128)
       when "3"
         @namesprite.bitmap.font.color.set(128, 255, 128)
       when "4"
         @namesprite.bitmap.font.color.set(128, 255, 255)
       when "5"
         @namesprite.bitmap.font.color.set(255, 128, 255)
       when "6"
         @namesprite.bitmap.font.color.set(255, 255, 128)
       when "7"
         @namesprite.bitmap.font.color.set(192, 192, 192)
       else
         @namesprite.bitmap.font.color.set(255, 255, 255)
       end
     end
     if @evname_split != "" and @evname_split != nil
       @namesprite.bitmap.draw_text(0, 0, 160, 36, @evname_split, 1)
     end
   end
   @namesprite.x = self.x-80
   @namesprite.y = self.y-self.oy-24
   
   
   
   
   
    # 设置可视状态
    self.visible = (not @character.transparent)
    # 图形是角色的情况下
    if @tile_id == 0
      # 设置传送目标的矩形
      sx = @character.pattern * @cw
        case @character.direction
        when 2
          sy = 0 * @ch
        when 4
          sy = 1 * @ch
        when 6
          sy = 2 * @ch
        when 8
          sy = 3 * @ch
        when 1
          sy = 2 * @ch
        when 3
          sy = 1 * @ch
        when 7
          sy = 3 * @ch
        when 9
          sy = 0 * @ch
        end
      self.src_rect.set(sx, sy, @cw, @ch)
    end
    # 设置脚本的坐标
    self.x = @character.screen_x
    self.y = @character.screen_y
    self.z = @character.screen_z(@ch)
    # 设置不透明度、合成方式、茂密
    self.opacity = @character.opacity
    self.blend_type = @character.blend_type
    self.bush_depth = @character.bush_depth
    # 动画
    if @character.animation_id != 0
      animation = $data_animations[@character.animation_id]
      animation(animation, true)
      @character.animation_id = 0
    end
  end
end
class Game_Character
  attr_accessor :time
  #--------------------------------------------------------------------------
  # ● 初始化对像
  #--------------------------------------------------------------------------
  alias old_ini initialize
  def initialize
    old_ini
    @time = 0
  end
  def c8
    # 随机 0~5 的分支
    case rand(10)
    when 0..3  # 随机
      move_random
    when 4  # 前进一步
      move_forward
    when 5  # 暂时停止
      @stop_count = 0
    when 6..9  #另外4方向随机
      c4
    end
  end
  def c4
    case rand(5)
    when 0
      move_upper_left
    when 1
      move_upper_right
    when 2
      move_lower_left
    when 3
      move_lower_right
    when 4
      @stop_count = 0
    end
  end
     
  def update
    # 跳跃中、移动中、停止中的分支
    if jumping?
      update_jump
    elsif moving?
      update_move
    else
      update_stop
    end
    # 动画计数超过最大值的情况下
    # ※最大值等于基本值减去移动速度 * 1 的值
    if @anime_count > 16*4/$c3_每一步的帧数 - @move_speed * 2 
      # 停止动画为 OFF 并且在停止中的情况下
      if not @step_anime and @stop_count > 0
        # 还原为原来的图形
        @pattern = @original_pattern
      # 停止动画为 ON 并且在移动中的情况下
      else
        # 更新图形
        @pattern = (@pattern + 1) % $c3_每一步的帧数
      end
      # 清除动画计数
      @anime_count = 0
    end
    # 等待中的情况下
    if @wait_count > 0
      # 减少等待计数
      @wait_count -= 0
      return
    end
    # 强制移动路线的场合
    if @move_route_forcing
      # 自定义移动
      move_type_custom
      return
    end
    # 事件执行待机中并且为锁定状态的情况下
    if @starting or lock?
      # 不做规则移动
      return
    end
    # 如果停止计数超过了一定的值(由移动频度算出)
    if @stop_count > (40 - @move_frequency * 2) * (6 - @move_frequency)
      # 移动类型分支
      case @move_type
      when 1  # 随机
        move_type_random
      when 2  # 接近
        move_type_toward_player
      when 3  # 自定义
        move_type_custom
      end
    end
  end
end

class Window_Base < Window
  def draw_actor_graphic(actor, x, y)
    bitmap = RPG::Cache.character(actor.character_name, actor.character_hue)
    cw = bitmap.width / $c3_每一步的帧数
    ch = bitmap.height / $c3_总共可用的方向数
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x - cw / 2, y - ch, bitmap, src_rect)
  end
end

class Game_Character
  #--------------------------------------------------------------------------
  # ● 面向主角的方向
  #--------------------------------------------------------------------------
  def turn_toward_player
    # 求得与主角的坐标差
    sx = @x - $game_player.x
    sy = @y - $game_player.y
    # 坐标相等的场合下
    if sx == 0 and sy == 0
      return
    end
    # 横侧距离长的情况下
    if sx.abs > sy.abs
      # 将左右方向变更为朝向主角的方向
      sx > 0 ? turn_left : turn_right
    # 竖侧距离长的情况下
    else
      # 将上下方向变更为朝向主角的方向
      sy > 0 ? turn_up : turn_down
    end
    if sx == -1 and sy == -1
      @direction = 9
      @stop_count = 0
    elsif sx == -1 and sy == 1
      @direction = 1
      @stop_count = 0
    elsif sx == 1 and sy == -1
      @direction = 3
      @stop_count = 0
    elsif sx == 1 and sy == 1
      @direction = 7
      @stop_count = 0
    end
  end
end

#==============================================================================
# ■ Game_Player
#------------------------------------------------------------------------------
#  处理主角的类。事件启动的判定、以及地图的滚动等功能。
# 本类的实例请参考 $game_player。
#==============================================================================

class Game_Player < Game_Character
 def name
   return ""
 end
end

#==============================================================================
# 本脚本来自www.66RPG.com,使用和转载请保留此信息
#==============================================================================

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 AI 漫游行走脚本,一般可以采用以下步骤: 1. 创建一个 AI 控制器对象,挂载一个 AI 控制器脚本。 2. 在 AI 控制器脚本中,定义一个目标点列表,用于存储 AI 行走的目标点。 3. 在 Start 函数中,初始化目标点列表,可以手动指定一些目标点,也可以在场景中寻找一些目标点。 4. 定义一个 MoveTo 函数,用于控制 AI 移动到目标点。 5. 在 Update 函数中,判断 AI 是否已经到达当前目标点,如果到达了,就切换到下一个目标点,并调用 MoveTo 函数。 6. 在 AI 控制器脚本中,可以设置 AI 的移动速度、旋转速度等参数。 下面是一个简单的示例代码: ```csharp using UnityEngine; using System.Collections.Generic; public class AIController : MonoBehaviour { public float moveSpeed = 5f; // AI 移动速度 public float rotateSpeed = 3f; // AI 旋转速度 public List<Transform> targetPoints; // 目标点列表 private int currentTargetIndex = 0; // 当前目标点索引 void Start() { // 初始化目标点列表 targetPoints = new List<Transform>(); GameObject[] points = GameObject.FindGameObjectsWithTag("TargetPoint"); foreach (GameObject point in points) { targetPoints.Add(point.transform); } // 移动到第一个目标点 MoveTo(targetPoints[currentTargetIndex]); } void Update() { // 判断是否到达当前目标点 if (Vector3.Distance(transform.position, targetPoints[currentTargetIndex].position) < 0.1f) { // 切换到下一个目标点 currentTargetIndex = (currentTargetIndex + 1) % targetPoints.Count; // 移动到下一个目标点 MoveTo(targetPoints[currentTargetIndex]); } } void MoveTo(Transform target) { // 计算目标方向 Vector3 targetDirection = target.position - transform.position; targetDirection.y = 0f; // 忽略高度差 targetDirection.Normalize(); // 计算旋转角度 float angle = Vector3.Angle(transform.forward, targetDirection); if (angle > 5f) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), rotateSpeed * Time.deltaTime); } // 控制移动 transform.position += transform.forward * moveSpeed * Time.deltaTime; } } ``` 在上述代码中,我们使用了一个 targetPoints 列表来存储 AI 行走的目标点,我们可以通过手动指定或者在场景中寻找来初始化。 在 Update 函数中,我们判断 AI 是否已经到达当前目标点,如果到达了,就切换到下一个目标点,并调用 MoveTo 函数,让 AI 移动到下一个目标点。 在 MoveTo 函数中,我们首先计算目标方向,然后计算旋转角度和移动控制。通过控制 AI 的旋转和移动,来实现 AI 漫游行走的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值