Project1

标题: 请问如何通过事件让玩家移动到指定位置 [打印本页]

作者: 是猪别乱叫    时间: 2016-8-23 22:04
标题: 请问如何通过事件让玩家移动到指定位置
不是场所移动,而是移动到指定坐标......
作者: QQ蚊子湯    时间: 2016-8-24 01:57

將淡出淡入設置成"無"可以製作出類似瞬間移動的效果
作者: imsy    时间: 2016-8-24 02:00
想一步一步走过去的话 需要寻路算法
如果地图是完全的平地的话可以很简单 不然就要专业点的算法了
作者: 是猪别乱叫    时间: 2016-8-24 08:38
有相关的脚本吗?直接移动到指定位置,带移动动画的
作者: 丿绯羽丶    时间: 2016-8-24 10:59
这是类似自动寻路的脚本:
使用方法:
设置移动路线→脚本move2pos_wait(目的地x,目的地y)
  1. =begin
  2. ■ 移動ルートの拡張 for VX Ace (ver 0.0.0.2)
  3.   by 半生
  4.   http://www.tktkgame.com/

  5. ● 内容
  6.  「プレイヤーに近づく」の座標指定・イベント指定バージョンのような
  7.  ものを追加します。

  8. ● 使用方法
  9.   移動ルートの指定でスクリプトを選択し以下のメソッドを指定します。

  10. ● メソッド
  11. ・ move2pos(tx, ty [, option])
  12.      指定座標に一歩近づく
  13.      tx, ty : 目標のマップ座標
  14.      option : ハッシュでオプションを指定(省略可)
  15.        :random_rate => 数字を指定するとその%の確率でランダム移動が入ります
  16.        :circumvent  => trueを指定すると微妙に回り込んだりします。

  17. ・ move2pos_wait(tx, ty [, option])
  18.      目的地にたどり着くまで指定座標に近づくを繰り返します。
  19.      「移動できない場合は無視」を無視します。
  20.      tx, ty : 目標のマップ座標
  21.      option : move2posと同じもの+[:distance]オプション
  22.        :distance => 対象と接触したとみなす距離(デフォルト:0)

  23. ・ move2event(event_id [, option])
  24.      指定イベントに一歩近づく
  25.      event_id : 目標のイベントID
  26.      option : move2posと同じ

  27. ・ move2event_wait(event_id [, option])
  28.      指定イベントに接触するまで近づくを繰り返す
  29.      event_id : 目標のマップイベントID
  30.      option : move2posと同じもの+[:distance]オプション
  31.        :distance => 対象と接触したとみなす距離(デフォルト:1)

  32. ・ turn_toward_pos(tx, ty)
  33.      指定座標の方向を向く
  34.      tx, ty : 目標のマップ座標

  35. ・ turn_toward_event(event_id)
  36.      指定イベントの方向を向く
  37.      event_id : 目標のイベントID


  38.   ■ 更新履歴
  39.     ver 0.0.0.2 (2013/12/29)
  40.      回り込み処理の修正
  41.     ver 0.0.0.1 (2013/12/29)
  42.      公開

  43. =end

  44. module TkTkGame
  45. end

  46. module TkTkGame::MovePlus
  47.   module Extend_GameCharacter
  48.     #--------------------------------------------------------------------------
  49.     # ● 指定位置からの X 距離計算
  50.     #--------------------------------------------------------------------------
  51.     def distance_x_from_target(tx)
  52.       sx = @x - tx
  53.       if $game_map.loop_horizontal?         # 横にループしているとき
  54.         if sx.abs > $game_map.width / 2     # 絶対値がマップの半分より大きい?
  55.           if sx > 0
  56.             sx -= $game_map.width           # マップの幅を引く
  57.           else
  58.             sx += $game_map.width           # マップの幅を足す
  59.           end
  60.         end
  61.       end
  62.       return sx
  63.     end
  64.    
  65.     #--------------------------------------------------------------------------
  66.     # ● 指定位置からの Y 距離計算
  67.     #--------------------------------------------------------------------------
  68.     def distance_y_from_target(ty)
  69.       sy = @y - ty
  70.       if $game_map.loop_vertical?           # 縦にループしているとき
  71.         if sy.abs > $game_map.height / 2    # 絶対値がマップの半分より大きい?
  72.           if sy > 0
  73.             sy -= $game_map.height          # マップの高さを引く
  74.           else
  75.             sy += $game_map.height          # マップの高さを足す
  76.           end
  77.         end
  78.       end
  79.       return sy
  80.     end

  81.     #--------------------------------------------------------------------------
  82.     # ● 指定位置からの 歩数計算
  83.     #--------------------------------------------------------------------------
  84.     def steps_from_pos(tx, ty)
  85.       sx = @x - tx
  86.       sy = @y - ty
  87.       if $game_map.loop_horizontal?         # 横にループしているとき
  88.         if sx.abs > $game_map.width / 2     # 絶対値がマップの半分より大きい?
  89.           if sx > 0
  90.             sx -= $game_map.width           # マップの幅を引く
  91.           else
  92.             sx += $game_map.width           # マップの幅を足す
  93.           end
  94.         end
  95.       end
  96.       if $game_map.loop_vertical?           # 縦にループしているとき
  97.         if sy.abs > $game_map.height / 2    # 絶対値がマップの半分より大きい?
  98.           if sy > 0
  99.             sy -= $game_map.height          # マップの高さを引く
  100.           else
  101.             sy += $game_map.height          # マップの高さを足す
  102.           end
  103.         end
  104.       end
  105.       return sx.abs + sy.abs
  106.     end
  107.    
  108.     #--------------------------------------------------------------------------
  109.     # ● 指定座標の方を向く
  110.     #--------------------------------------------------------------------------
  111.     def turn_toward_pos(tx, ty)
  112.       sx = distance_x_from_target(tx)
  113.       sy = distance_y_from_target(ty)
  114.       if sx.abs > sy.abs                    # 横の距離のほうが長い
  115.         sx > 0 ? turn_left : turn_right
  116.       elsif sx.abs < sy.abs                 # 縦の距離のほうが長い
  117.         sy > 0 ? turn_up : turn_down
  118.       end
  119.     end
  120.    
  121.     #--------------------------------------------------------------------------
  122.     # ● 指定イベントの方を向く
  123.     #--------------------------------------------------------------------------
  124.     def turn_toward_event(event_id)
  125.       event = $game_map.events[event_id]
  126.       return unless event.is_a?(Game_Event)
  127.       turn_toward_pos(event.x, event.y)
  128.     end
  129.    
  130.     #--------------------------------------------------------------------------
  131.     # ● 指定位置に近づく
  132.     #--------------------------------------------------------------------------
  133.     # tx,ty: 目標地点の座標
  134.     # oprion: {:random_rate => 5, :circumvent => true}のようにハッシュで指定
  135.     #   [:random_rate] : ランダム移動する確率[%]
  136.     #   [:circumvent] : 進めないときに回り込むかどうか[true or false]
  137.     def move2pos(tx, ty, option={})
  138.       sx = distance_x_from_target(tx)
  139.       sy = distance_y_from_target(ty)
  140.       if sx != 0 or sy != 0
  141.         # 回り込み判定
  142.         if option.key?(:circumvent) and option[:circumvent]
  143.           circumvent = true
  144.         else
  145.           circumvent = false
  146.         end
  147.         
  148.         if @tkgc_reserved_move != nil
  149.           case @tkgc_reserved_move
  150.           when 2,4,6,8
  151.             move_straight(@tkgc_reserved_move)
  152.           else
  153.             @move_succeed = false
  154.           end
  155.           @tkgc_reserved_move = nil
  156.           # 移動成功時はそのまま終了
  157.           return if @move_succeed
  158.         end
  159.         
  160.         # 確率ランダム移動が指定されている場合
  161.         if option.key?(:random_rate)
  162.           if rand(100) < option[:random_rate].to_i
  163.             return move_random
  164.           end
  165.         end
  166.         
  167.         # 近づく処理
  168.         if sx.abs > sy.abs                  # 横の距離のほうが長い
  169.           move_straight(sx > 0 ? 4 : 6)   # 左右方向を優先
  170.           if !@move_succeed
  171.             if sy != 0
  172.               move_straight(sy > 0 ? 8 : 2)
  173.             end
  174.             if !@move_succeed and circumvent
  175.               if sx < 0
  176.                 next_x = @x + 1
  177.                 next_move = 6
  178.               else
  179.                 next_x = @x - 1
  180.                 next_move = 4
  181.               end
  182.               if rand(2) == 0
  183.                 move_straight(8) if passable?(@x, @y, 8) and passable?(@x, @y-1, next_move)
  184.                 move_straight(2) if !@move_succeed and passable?(@x, @y, 2) and passable?(@x, @y+1, next_move)
  185.               else
  186.                 move_straight(2) if passable?(@x, @y, 2) and passable?(@x, @y+1, next_move)
  187.                 move_straight(8) if !@move_succeed and passable?(@x, @y, 8) and passable?(@x, @y-1, next_move)
  188.               end
  189.               if @move_succeed
  190.                 @tkgc_reserved_move = next_move
  191.               end
  192.             end
  193.           end
  194.         else                                # 縦の距離のほうが長いか等しい
  195.           move_straight(sy > 0 ? 8 : 2)      # 上下方向を優先
  196.           if !@move_succeed
  197.             if sx != 0
  198.               move_straight(sx > 0 ? 4 : 6)
  199.             end
  200.             if !@move_succeed and circumvent
  201.               if sy < 0
  202.                 next_y = @y + 1
  203.                 next_move = 2
  204.               else
  205.                 next_y = @y - 1
  206.                 next_move = 8
  207.               end
  208.               if rand(2) == 0
  209.                 move_straight(4) if passable?(@x ,@y, 4) and passable?(@x-1 ,@y, next_move)
  210.                 move_straight(6) if !@move_succeed and  passable?(@x ,@y, 6) and passable?(@x+1, @y, next_move)
  211.               else
  212.                 move_straight(6) if passable?(@x ,@y, 6) and passable?(@x+1 ,@y, next_move)
  213.                 move_straight(4) if !@move_succeed and passable?(@x ,@y, 4) and passable?(@x-1, @y, next_move)
  214.               end
  215.               unless !@move_succeed
  216.                 @tkgc_reserved_move = next_move
  217.               end
  218.             end
  219.           end
  220.         end
  221.       end
  222.     end # Game_Character#move2pos

  223.     #--------------------------------------------------------------------------
  224.     # ● 指定位置に近づく(移動完了まで繰り返す)
  225.     #--------------------------------------------------------------------------
  226.     def move2pos_wait(tx, ty, option={})
  227.       if option.key?(:distance)
  228.         distance = option[:distance].to_i
  229.       else
  230.         distance = 0
  231.       end
  232.       move2pos(tx, ty, option)
  233.       sx = distance_x_from_target(tx)
  234.       sy = distance_y_from_target(ty)
  235.       # 移動後に目的地に到着
  236.       if steps_from_pos(tx, ty) <= distance
  237.         @hn_reserved_mov = nil
  238.         @move_succeed = true
  239.       else
  240.         if @move_route.skippable
  241.           @move_route_index -= 1
  242.         end
  243.         @move_succeed = false
  244.       end
  245.     end
  246.    
  247.     #--------------------------------------------------------------------------
  248.     # ● マップイベントに近づく
  249.     #--------------------------------------------------------------------------
  250.     def move2event(event_id, option={})
  251.       event = $game_map.events[event_id]
  252.       return unless event.is_a?(Game_Event)
  253.       move2pos(event.x, event.y, option)
  254.     end
  255.    
  256.     #--------------------------------------------------------------------------
  257.     # ● マップイベントに近づく(移動完了まで繰り返す)
  258.     #--------------------------------------------------------------------------
  259.     def move2event_wait(event_id, option={})
  260.       if option.key?(:distance)
  261.         distance = option[:distance].to_i
  262.       else
  263.         distance = 1
  264.       end
  265.       
  266.       event = $game_map.events[event_id]
  267.       return unless event.is_a?(Game_Event)
  268.       tx = event.x
  269.       ty = event.y
  270.       
  271.       # 指定された距離まで近づいている場合
  272.       if steps_from_pos(tx, ty) <= distance
  273.         turn_toward_pos(tx, ty)
  274.         @move_succeed = true
  275.         @hn_reserved_mov = nil
  276.         return
  277.       end

  278.       # 目標イベントに向かって移動
  279.       move2pos(tx, ty, option)


  280.       # 到達していない場合は繰り返す
  281.       if @move_route.skippable
  282.         @move_route_index -= 1
  283.       end
  284.       @move_succeed = false
  285.     end
  286.    
  287.     # ■ 移動ルート拡張関連パラメータのリセット
  288.     def reset_move2pos_params
  289.       @tkgc_reserved_move = nil
  290.     end
  291.    
  292.     # ■ 移動ルートの強制
  293.     def force_move_route(move_route)
  294.       reset_move2pos_params
  295.       super(move_route)
  296.     end

  297.     # ■ 移動タイプ : カスタム
  298.     def update_routine_move
  299.       # 移動ルートの強制が解除されるときに移動予約も解除
  300.       if stopping? and @move_route.list[@move_route_index] == 0 and @move_route_forcing
  301.         reset_move2pos_params
  302.       end
  303.       super
  304.     end
  305.   end
  306. end

  307. class Game_Event
  308.   include TkTkGame::MovePlus::Extend_GameCharacter
  309.   alias :_hn_mplus__setup_page :setup_page unless method_defined?(:_hn_mplus__setup_page)
  310.   def setup_page(*args)
  311.     reset_move2pos_params
  312.     _hn_mplus__setup_page(*args)
  313.   end
  314. end

  315. class Game_Player
  316.   include TkTkGame::MovePlus::Extend_GameCharacter
  317.   # ■ 指定位置に移動
  318.   alias :_hn_mplus__moveto :moveto unless method_defined?(:_hn_mplus__moveto)
  319.   def moveto(x, y)
  320.     reset_move2pos_params
  321.     _hn_mplus__moveto(x, y)
  322.   end
  323. end
复制代码

作者: garfeng    时间: 2016-8-24 11:03
https://github.com/garfeng/rmva/blob/master/src/move_toward.rb




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1