幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1820|回复: 5

八方向行走RGSS

[复制链接]

7

主题

18

帖子

568

积分

⑤进阶

积分
568
发表于 2004-11-10 16:00:12 | 显示全部楼层 |阅读模式
谁知道加到哪里?小弟感激不尽!!!

效果:可以实现八方向键行走并且解除系统默认的行走限制。

代码:

# ドット単位移動人柱バージョン

#==============================================================================
# ■ Game_Player
#------------------------------------------------------------------------------
#  プレイヤーを扱うクラスです。イベントの起動判定や、マップのスクロールなどの
# 機能を持っています。このクラスのインスタンスは $game_player で参照されます。
#==============================================================================

class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # ● 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_accessor :revise_x                 # X座標補正値
  attr_accessor :revise_y                 # Y座標補正値
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize
    @revise_x = 0
    @revise_y = 0
    super
  end
  def moving?
    if @move_route_forcing
      super
    else
      return (@x != (@real_x / 128.0).round or @y != (@real_y / 128.0).round)
    end
  end
  #--------------------------------------------------------------------------
  # ● 下に移動
  #--------------------------------------------------------------------------
  def move_down_p
    # 下を向く
    turn_down
    # 下に移動
    distance = 2 ** @move_speed
    down1(@x, @y, distance)
  end
  def down1(x, y, distance)
    return false unless down2(x, y, distance)
    if @revise_x < -32
      return false unless down2(x - 1, y, distance)
    elsif @revise_x > 32
      return false unless down2(x + 1, y, distance)
    end
    @revise_y += distance
    return true
  end
  def down2(x, y, distance)
    if @revise_y + distance > 0
      unless passable?(x, y, 2)
        @revise_y = 0
        @event = check_event_trigger_touch(x, y+1)
        return false
      end
    end
    return true
  end
  #--------------------------------------------------------------------------
  # ● 左に移動
  #--------------------------------------------------------------------------
  def move_left_p
    # 左を向く
    turn_left
    distance = 2 ** @move_speed
    left1(@x, @y, distance)
  end
  def left1(x, y, distance)
    return false unless left2(x, y, distance)
    if @revise_y < -48
      return false unless left2(x, y - 1, distance)
    elsif @revise_y > 0
      return false unless left2(x, y + 1, distance)
    end
    @revise_x -= distance
    return true
  end
  def left2(x, y, distance)
    if @revise_x - distance < -32
      unless passable?(x, y, 4)
        @revise_x = -32
        @event = check_event_trigger_touch(x-1, y)
        return false
      end
    end
    return true
  end
  #--------------------------------------------------------------------------
  # ● 右に移動
  #--------------------------------------------------------------------------
  def move_right_p
      # 右を向く
      turn_right
    distance = 2 ** @move_speed
    right1(@x, @y, distance)
  end
  def right1(x, y, distance)
    return false unless right2(x, y, distance)
    if @revise_y < -48
      return false unless right2(x, y, distance)
    elsif @revise_y > 0
      return false unless right2(x, y + 1, distance)
    end
    @revise_x += distance
    return true
  end
  def right2(x, y, distance)
    if @revise_x + distance > 32
      unless passable?(x, y, 6)
        @revise_x = 32
        @event = check_event_trigger_touch(x+1, y)
        return false
      end
    end
    return true
  end
  #--------------------------------------------------------------------------
  # ● 上に移動
  #--------------------------------------------------------------------------
  def move_up_p
    # 上を向く
    turn_up
    # 下に移動
    distance = 2 ** @move_speed
    up1(@x, @y, distance)
  end
  def up1(x, y, distance)
    return false unless up2(x, y, distance)
    if @revise_x < -32
      return false unless up2(x - 1, y, distance)
    elsif @revise_x > 32
      return false unless up2(x + 1, y, distance)
    end
    @revise_y -= distance
    return true
  end
  def up2(x, y, distance)
    if @revise_y - distance < -48
      unless passable?(x, y, 8)
        @revise_y = -48
        @event = check_event_trigger_touch(x, y-1)
        return false
      end
    end
    return true
  end
  #--------------------------------------------------------------------------
  # ● 左下に移動
  #--------------------------------------------------------------------------
  def move_lower_left_p
    # 向き固定でない場合
    unless @direction_fix
      # 右向きだった場合は左を、上向きだった場合は下を向く
      @direction = (@direction == 6 ? 4 : @direction == 8 ? 2 : @direction)
    end
    # 左下に移動
    distance = (2 ** @move_speed) / Math.sqrt(2)
    turn_left unless down1(@x, @y, distance)
    turn_down if @event
    turn_down unless left1(@x, @y, distance) unless @event
    turn_left if @event
  end
  #--------------------------------------------------------------------------
  # ● 右下に移動
  #--------------------------------------------------------------------------
  def move_lower_right_p
    # 向き固定でない場合
    unless @direction_fix
      # 左向きだった場合は右を、上向きだった場合は下を向く
      @direction = (@direction == 4 ? 6 : @direction == 8 ? 2 : @direction)
    end
    # 右下に移動
    distance = (2 ** @move_speed) / Math.sqrt(2)
    turn_right unless down1(@x, @y, distance)
    turn_down if @event
    turn_down unless right1(@x, @y, distance) unless @event
    turn_right if @event
  end
  #--------------------------------------------------------------------------
  # ● 左上に移動
  #--------------------------------------------------------------------------
  def move_upper_left_p
    # 向き固定でない場合
    unless @direction_fix
      # 右向きだった場合は左を、下向きだった場合は上を向く
      @direction = (@direction == 6 ? 4 : @direction == 2 ? 8 : @direction)
    end
    # 左下に移動
    distance = (2 ** @move_speed) / Math.sqrt(2)
    turn_left unless up1(@x, @y, distance)
    turn_up if @event
    turn_up unless left1(@x, @y, distance) unless @event
    turn_left if @event
  end
  #--------------------------------------------------------------------------
  # ● 右上に移動
  #--------------------------------------------------------------------------
  def move_upper_right_p
    # 向き固定でない場合
    unless @direction_fix
      # 左向きだった場合は右を、下向きだった場合は上を向く
      @direction = (@direction == 4 ? 6 : @direction == 2 ? 8 : @direction)
    end
    # 右下に移動
    distance = (2 ** @move_speed) / Math.sqrt(2)
    turn_right unless up1(@x, @y, distance)
    turn_up if @event
    turn_up unless right1(@x, @y, distance) unless @event
    turn_right if @event
  end
  #--------------------------------------------------------------------------
  # ● 座標修正
  #--------------------------------------------------------------------------
  def move_on
    if @y < (@real_y / 128.0).round
      @y += 1
      @revise_y -= 128
      increase_steps
    end
    if @x > (@real_x / 128.0).round
      @x -= 1
      @revise_x += 128
      increase_steps
    end
    if @x < (@real_x / 128.0).round
      @x += 1
      @revise_x -= 128
      increase_steps
    end
    if @y > (@real_y / 128.0).round
      @y -= 1
      @revise_y += 128
      increase_steps
    end
  end
  #--------------------------------------------------------------------------
  # ● アニメーションアップデート
  #--------------------------------------------------------------------------
  def anime_update
    # 移動時アニメが ON の場合
    if @walk_anime
      # アニメカウントを 1.5 増やす
      @anime_count += 1.5
    # 移動時アニメが OFF で、停止時アニメが ON の場合
    elsif @step_anime
      # アニメカウントを 1 増やす
      @anime_count += 1
    end
    # アニメカウントが最大値を超えた場合
    # ※最大値は、基本値 18 から移動速度 * 1 を引いた値
    if @anime_count > 18 - @move_speed * 2
      # 停止時アニメが OFF かつ 停止中の場合
      if not @step_anime and @stop_count > 0
        # パターンをオリジナルに戻す
        @pattern = @original_pattern
      # 停止時アニメが ON または 移動中の場合
      else
        # パターンを更新
        @pattern = (@pattern + 1) % 4
      end
      # アニメカウントをクリア
      @anime_count = 0
    end
  end
  #--------------------------------------------------------------------------
  # ● 正面のイベント起動判定
  #--------------------------------------------------------------------------
  # オリジナルのイベントを改名
  alias :check_event_trigger_there_original :check_event_trigger_there
  def check_event_trigger_there(triggers)
    result = check_event_trigger_there_original(triggers)
    unless result
      if @direction == 2 or @direction == 8
        if @revise_x < -32
          @x -= 1
          result = check_event_trigger_there_original(triggers)
          @x += 1
        elsif @revise_x > 32
          @x += 1
          result = check_event_trigger_there_original(triggers)
          @x -= 1
        end
      elsif @direction == 4 or @direction == 6
        if @revise_y < -48
          @y -= 1
          result = check_event_trigger_there_original(triggers)
          @y += 1
        elsif @revise_y > 0
          @y += 1
          result = check_event_trigger_there_original(triggers)
          @y -= 1
        end
      end
    end
    return result
  end
  #--------------------------------------------------------------------------
  # ● フレーム更新
  #--------------------------------------------------------------------------
  def update
    if @revise_x == nil and @revise_y == nil
      @revise_x = 0
      @revise_y = 0
    end
    if @move_route_forcing
      # ローカル変数に移動中かどうかを記憶
      last_moving = moving?
      # ローカル変数に座標を記憶
      last_real_x = @real_x
      last_real_y = @real_y
      # 座標がずれている場合
      if @revise_x != 0 or @revise_y != 0
        distance1 = 2 ** @move_speed
        distance2= Math.sqrt(@revise_x ** 2 + @revise_y ** 2)
        if distance1 > distance2
          @real_x = (@real_x - @revise_x).round
          @real_y = (@real_y - @revise_y).round
          @revise_x = 0
          @revise_y = 0
          anime_update
        else
          @revise_x -= distance1 * @revise_x / distance2
          @revise_y -= distance1 * @revise_y / distance2
          # 移動処理
          @real_x -= distance1 * @revise_x / distance2
          @real_y -= distance1 * @revise_y / distance2
          anime_update
        end
      else
        super
      end
    else
      # 移動中、イベント実行中、移動ルート強制中、
      # メッセージウィンドウ表示中のいずれでもない場合
      unless moving? or $game_system.map_interpreter.running? or
             @move_route_forcing or $game_temp.message_window_showing
        #ダッシュ機能。エンターキーが押されている間、移動速度を変更する。
        if Input.press?(Input::C)
          if @move_speed != 5
            @move_speed = 5
          end
        else
          if @move_speed != 4
            @move_speed = 4
          end
        end
        @event = false
        # 方向ボタンが押されていれば、その方向へプレイヤーを移動
        case Input.dir8
        when 1
          move_lower_left_p
        when 2
          move_down_p
        when 3
          move_lower_right_p
        when 4
          move_left_p
        when 6
          move_right_p
        when 7
          move_upper_left_p
        when 8
          move_up_p
        when 9
          move_upper_right_p
        end
      end
      # ローカル変数に座標を記憶
      last_real_x = @real_x
      last_real_y = @real_y
      # 移動処理
      @real_x = @x * 128 + @revise_x
      @real_y = @y * 128 + @revise_y
      # ローカル変数に移動中かどうかを記憶
      last_moving = moving?
      move_on
      if (last_real_x != @real_x or last_real_y != @real_y)
        anime_update
      else
        @pattern = 0
      end
    end
    # キャラクターが下に移動し、かつ画面上の位置が中央より下の場合
    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
end

回复

使用道具 举报

carol3 该用户已被删除
发表于 2004-11-10 18:07:46 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

7

主题

18

帖子

568

积分

⑤进阶

积分
568
 楼主| 发表于 2004-11-10 18:18:13 | 显示全部楼层
晕!!!教教我怎么用,我还不会用呢
回复 支持 反对

使用道具 举报

carol3 该用户已被删除
发表于 2004-11-10 21:11:26 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

3

主题

24

帖子

476

积分

④见习

黑奴啊黑奴

积分
476
发表于 2004-11-10 21:13:02 | 显示全部楼层
1、切换场景的时候会有坐标偏移,可能和人物移动速度有关,感觉比较别扭。


是因为把原来的一个格子切割成了四个小格子,index在左上角那个小格子上

因为这个所以没用……总觉得有些不舒服

[此贴子已经被作者于2004-11-10 21:16:02编辑过]
--艾泽拉斯--
回复 支持 反对

使用道具 举报

1

主题

22

帖子

533

积分

⑤进阶

杀人皇帝

积分
533
发表于 2004-11-10 22:07:06 | 显示全部楼层
大大影响画面效果。。没用。。。再Main之上建一个新脚本。。粘贴就可以了
男人掌握了世界,女人掌握了男人.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2025-6-8 10:45 , Processed in 0.012106 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表