设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 3542|回复: 1
打印 上一主题 下一主题

[已经解决] [已解決]关于AStar寻路扩展.rb的用法

[复制链接]
头像被屏蔽

Lv4.逐梦者 (禁止发言)

梦石
0
星屑
5701
在线时间
922 小时
注册时间
2013-8-29
帖子
1468
跳转到指定楼层
1
发表于 2020-2-16 01:14:56 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
提示: 作者被禁止或删除 内容自动屏蔽

Lv6.析梦学徒

老鹰

梦石
40
星屑
33412
在线时间
6552 小时
注册时间
2012-5-26
帖子
3178

极短24评委极短23参与极短22参与极短21评委老司机慢点开短篇十吟唱者组别冠军开拓者剧作品鉴家

2
发表于 2020-2-16 10:52:12 | 只看该作者
本帖最后由 百里_飞柳 于 2020-2-16 10:55 编辑

这个是用于做追逐的使用方式,也就是在每一次移动时都必定为了移动到目的地

如果你想做事件移动到指定地点之后,继续执行其他事件指令
请使用 循环-等待1帧(为了同时能更新画面)-事件移动路线AStar(不重复动作)-如果事件x==目的地x并且事件y==目的地y 就跳出循环-结束循环,之后继续执行其他指令

或者使用这个脚本,利用 事件移动路线中的脚本 move2pos(x, y),并且等待移动结束,来将事件移动到指定位置并结束移动
RUBY 代码复制
  1. =begin
  2.  ■ 移動ルートの拡張 for VX Ace (ver 0.0.0.2)
  3.   by 半生
  4.   [url]http://www.tktkgame.com/[/url]
  5.  
  6.  ● 内容
  7.   「プレイヤーに近づく」の座標指定・イベント指定バージョンのような
  8.   ものを追加します。
  9.  
  10.  ● 使用方法
  11.    移動ルートの指定でスクリプトを選択し以下のメソッドを指定します。
  12.  
  13.  ● メソッド
  14.  ・ move2pos(tx, ty [, option])
  15.      指定座標に一歩近づく
  16.      tx, ty : 目標のマップ座標
  17.      option : ハッシュでオプションを指定(省略可)
  18.        :random_rate => 数字を指定するとその%の確率でランダム移動が入ります
  19.        :circumvent  => trueを指定すると微妙に回り込んだりします。
  20.  
  21.  ・ move2pos_wait(tx, ty [, option])
  22.      目的地にたどり着くまで指定座標に近づくを繰り返します。
  23.      「移動できない場合は無視」を無視します。
  24.      tx, ty : 目標のマップ座標
  25.      option : move2posと同じもの+[:distance]オプション
  26.        :distance => 対象と接触したとみなす距離(デフォルト:0)
  27.  
  28.  ・ move2event(event_id [, option])
  29.      指定イベントに一歩近づく
  30.      event_id : 目標のイベントID
  31.      option : move2posと同じ
  32.  
  33.  ・ move2event_wait(event_id [, option])
  34.      指定イベントに接触するまで近づくを繰り返す
  35.      event_id : 目標のマップイベントID
  36.      option : move2posと同じもの+[:distance]オプション
  37.        :distance => 対象と接触したとみなす距離(デフォルト:1)
  38.  
  39.  ・ turn_toward_pos(tx, ty)
  40.      指定座標の方向を向く
  41.      tx, ty : 目標のマップ座標
  42.  
  43.  ・ turn_toward_event(event_id)
  44.      指定イベントの方向を向く
  45.      event_id : 目標のイベントID
  46.  
  47.  
  48.   ■ 更新履歴
  49.     ver 0.0.0.2 (2013/12/29)
  50.      回り込み処理の修正
  51.     ver 0.0.0.1 (2013/12/29)
  52.      公開
  53.  
  54. =end
  55.  
  56. module TkTkGame
  57. end
  58.  
  59. module TkTkGame::MovePlus
  60.   module Extend_GameCharacter
  61.     #--------------------------------------------------------------------------
  62.     # ● 指定位置からの X 距離計算
  63.     #--------------------------------------------------------------------------
  64.     def distance_x_from_target(tx)
  65.       sx = @x - tx
  66.       if $game_map.loop_horizontal?         # 横にループしているとき
  67.         if sx.abs > $game_map.width / 2     # 絶対値がマップの半分より大きい?
  68.           if sx > 0
  69.             sx -= $game_map.width           # マップの幅を引く
  70.           else
  71.             sx += $game_map.width           # マップの幅を足す
  72.           end
  73.         end
  74.       end
  75.       return sx
  76.     end
  77.  
  78.     #--------------------------------------------------------------------------
  79.     # ● 指定位置からの Y 距離計算
  80.     #--------------------------------------------------------------------------
  81.     def distance_y_from_target(ty)
  82.       sy = @y - ty
  83.       if $game_map.loop_vertical?           # 縦にループしているとき
  84.         if sy.abs > $game_map.height / 2    # 絶対値がマップの半分より大きい?
  85.           if sy > 0
  86.             sy -= $game_map.height          # マップの高さを引く
  87.           else
  88.             sy += $game_map.height          # マップの高さを足す
  89.           end
  90.         end
  91.       end
  92.       return sy
  93.     end
  94.  
  95.     #--------------------------------------------------------------------------
  96.     # ● 指定位置からの 歩数計算
  97.     #--------------------------------------------------------------------------
  98.     def steps_from_pos(tx, ty)
  99.       sx = @x - tx
  100.       sy = @y - ty
  101.       if $game_map.loop_horizontal?         # 横にループしているとき
  102.         if sx.abs > $game_map.width / 2     # 絶対値がマップの半分より大きい?
  103.           if sx > 0
  104.             sx -= $game_map.width           # マップの幅を引く
  105.           else
  106.             sx += $game_map.width           # マップの幅を足す
  107.           end
  108.         end
  109.       end
  110.       if $game_map.loop_vertical?           # 縦にループしているとき
  111.         if sy.abs > $game_map.height / 2    # 絶対値がマップの半分より大きい?
  112.           if sy > 0
  113.             sy -= $game_map.height          # マップの高さを引く
  114.           else
  115.             sy += $game_map.height          # マップの高さを足す
  116.           end
  117.         end
  118.       end
  119.       return sx.abs + sy.abs
  120.     end
  121.  
  122.     #--------------------------------------------------------------------------
  123.     # ● 指定座標の方を向く
  124.     #--------------------------------------------------------------------------
  125.     def turn_toward_pos(tx, ty)
  126.       sx = distance_x_from_target(tx)
  127.       sy = distance_y_from_target(ty)
  128.       if sx.abs > sy.abs                    # 横の距離のほうが長い
  129.         sx > 0 ? turn_left : turn_right
  130.       elsif sx.abs < sy.abs                 # 縦の距離のほうが長い
  131.         sy > 0 ? turn_up : turn_down
  132.       end
  133.     end
  134.  
  135.     #--------------------------------------------------------------------------
  136.     # ● 指定イベントの方を向く
  137.     #--------------------------------------------------------------------------
  138.     def turn_toward_event(event_id)
  139.       event = $game_map.events[event_id]
  140.       return unless event.is_a?(Game_Event)
  141.       turn_toward_pos(event.x, event.y)
  142.     end
  143.  
  144.     #--------------------------------------------------------------------------
  145.     # ● 指定位置に近づく
  146.     #--------------------------------------------------------------------------
  147.     # tx,ty: 目標地点の座標
  148.     # oprion: {:random_rate => 5, :circumvent => true}のようにハッシュで指定
  149.     #   [:random_rate] : ランダム移動する確率[%]
  150.     #   [:circumvent] : 進めないときに回り込むかどうか[true or false]
  151.     def move2pos(tx, ty, option={})
  152.       sx = distance_x_from_target(tx)
  153.       sy = distance_y_from_target(ty)
  154.       if sx != 0 or sy != 0
  155.         # 回り込み判定
  156.         if option.key?(:circumvent) and option[:circumvent]
  157.           circumvent = true
  158.         else
  159.           circumvent = false
  160.         end
  161.  
  162.         if @tkgc_reserved_move != nil
  163.           case @tkgc_reserved_move
  164.           when 2,4,6,8
  165.             move_straight(@tkgc_reserved_move)
  166.           else
  167.             @move_succeed = false
  168.           end
  169.           @tkgc_reserved_move = nil
  170.           # 移動成功時はそのまま終了
  171.           return if @move_succeed
  172.         end
  173.  
  174.         # 確率ランダム移動が指定されている場合
  175.         if option.key?(:random_rate)
  176.           if rand(100) < option[:random_rate].to_i
  177.             return move_random
  178.           end
  179.         end
  180.  
  181.         # 近づく処理
  182.         if sx.abs > sy.abs                  # 横の距離のほうが長い
  183.           move_straight(sx > 0 ? 4 : 6)   # 左右方向を優先
  184.           if !@move_succeed
  185.             if sy != 0
  186.               move_straight(sy > 0 ? 8 : 2)
  187.             end
  188.             if !@move_succeed and circumvent
  189.               if sx < 0
  190.                 next_x = @x + 1
  191.                 next_move = 6
  192.               else
  193.                 next_x = @x - 1
  194.                 next_move = 4
  195.               end
  196.               if rand(2) == 0
  197.                 move_straight(8) if passable?(@x, @y, 8) and passable?(@x, @y-1, next_move)
  198.                 move_straight(2) if !@move_succeed and passable?(@x, @y, 2) and passable?(@x, @y+1, next_move)
  199.               else
  200.                 move_straight(2) if passable?(@x, @y, 2) and passable?(@x, @y+1, next_move)
  201.                 move_straight(8) if !@move_succeed and passable?(@x, @y, 8) and passable?(@x, @y-1, next_move)
  202.               end
  203.               if @move_succeed
  204.                 @tkgc_reserved_move = next_move
  205.               end
  206.             end
  207.           end
  208.         else                                # 縦の距離のほうが長いか等しい
  209.           move_straight(sy > 0 ? 8 : 2)      # 上下方向を優先
  210.           if !@move_succeed
  211.             if sx != 0
  212.               move_straight(sx > 0 ? 4 : 6)
  213.             end
  214.             if !@move_succeed and circumvent
  215.               if sy < 0
  216.                 next_y = @y + 1
  217.                 next_move = 2
  218.               else
  219.                 next_y = @y - 1
  220.                 next_move = 8
  221.               end
  222.               if rand(2) == 0
  223.                 move_straight(4) if passable?(@x ,@y, 4) and passable?(@x-1 ,@y, next_move)
  224.                 move_straight(6) if !@move_succeed and  passable?(@x ,@y, 6) and passable?(@x+1, @y, next_move)
  225.               else
  226.                 move_straight(6) if passable?(@x ,@y, 6) and passable?(@x+1 ,@y, next_move)
  227.                 move_straight(4) if !@move_succeed and passable?(@x ,@y, 4) and passable?(@x-1, @y, next_move)
  228.               end
  229.               unless !@move_succeed
  230.                 @tkgc_reserved_move = next_move
  231.               end
  232.             end
  233.           end
  234.         end
  235.       end
  236.     end # Game_Character#move2pos
  237.  
  238.     #--------------------------------------------------------------------------
  239.     # ● 指定位置に近づく(移動完了まで繰り返す)
  240.     #--------------------------------------------------------------------------
  241.     def move2pos_wait(tx, ty, option={})
  242.       if option.key?(:distance)
  243.         distance = option[:distance].to_i
  244.       else
  245.         distance = 0
  246.       end
  247.       move2pos(tx, ty, option)
  248.       sx = distance_x_from_target(tx)
  249.       sy = distance_y_from_target(ty)
  250.       # 移動後に目的地に到着
  251.       if steps_from_pos(tx, ty) <= distance
  252.         @hn_reserved_mov = nil
  253.         @move_succeed = true
  254.       else
  255.         if @move_route.skippable
  256.           @move_route_index -= 1
  257.         end
  258.         @move_succeed = false
  259.       end
  260.     end
  261.  
  262.     #--------------------------------------------------------------------------
  263.     # ● マップイベントに近づく
  264.     #--------------------------------------------------------------------------
  265.     def move2event(event_id, option={})
  266.       event = $game_map.events[event_id]
  267.       return unless event.is_a?(Game_Event)
  268.       move2pos(event.x, event.y, option)
  269.     end
  270.  
  271.     #--------------------------------------------------------------------------
  272.     # ● マップイベントに近づく(移動完了まで繰り返す)
  273.     #--------------------------------------------------------------------------
  274.     def move2event_wait(event_id, option={})
  275.       if option.key?(:distance)
  276.         distance = option[:distance].to_i
  277.       else
  278.         distance = 1
  279.       end
  280.  
  281.       event = $game_map.events[event_id]
  282.       return unless event.is_a?(Game_Event)
  283.       tx = event.x
  284.       ty = event.y
  285.  
  286.       # 指定された距離まで近づいている場合
  287.       if steps_from_pos(tx, ty) <= distance
  288.         turn_toward_pos(tx, ty)
  289.         @move_succeed = true
  290.         @hn_reserved_mov = nil
  291.         return
  292.       end
  293.  
  294.       # 目標イベントに向かって移動
  295.       move2pos(tx, ty, option)
  296.  
  297.  
  298.       # 到達していない場合は繰り返す
  299.       if @move_route.skippable
  300.         @move_route_index -= 1
  301.       end
  302.       @move_succeed = false
  303.     end
  304.  
  305.     # ■ 移動ルート拡張関連パラメータのリセット
  306.     def reset_move2pos_params
  307.       @tkgc_reserved_move = nil
  308.     end
  309.  
  310.     # ■ 移動ルートの強制
  311.     def force_move_route(move_route)
  312.       reset_move2pos_params
  313.       super(move_route)
  314.     end
  315.  
  316.     # ■ 移動タイプ : カスタム
  317.     def update_routine_move
  318.       # 移動ルートの強制が解除されるときに移動予約も解除
  319.       if stopping? and @move_route.list[@move_route_index] == 0 and @move_route_forcing
  320.         reset_move2pos_params
  321.       end
  322.       super
  323.     end
  324.   end
  325. end
  326.  
  327. class Game_Event
  328.   include TkTkGame::MovePlus::Extend_GameCharacter
  329.   alias :_hn_mplus__setup_page :setup_page unless method_defined?(:_hn_mplus__setup_page)
  330.   def setup_page(*args)
  331.     reset_move2pos_params
  332.     _hn_mplus__setup_page(*args)
  333.   end
  334. end
  335.  
  336. class Game_Player
  337.   include TkTkGame::MovePlus::Extend_GameCharacter
  338.   # ■ 指定位置に移動
  339.   alias :_hn_mplus__moveto :moveto unless method_defined?(:_hn_mplus__moveto)
  340.   def moveto(x, y)
  341.     reset_move2pos_params
  342.     _hn_mplus__moveto(x, y)
  343.   end
  344. end

评分

参与人数 2星屑 +100 +1 收起 理由
VIPArcher + 100 认可答案
chanszeman1018 + 1 认可答案

查看全部评分

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-4-27 11:37

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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