Project1

标题: 路径寻址怎样应用到事件上? [打印本页]

作者: woodytt    时间: 2011-8-2 14:16
标题: 路径寻址怎样应用到事件上?
路径寻址怎样应用到事件上?
脚本如下:
  1. #==============================================================================
  2. #  ■ 路径寻址
  3. #==============================================================================
  4. # Near Fantastica
  5. # 版本 1
  6. # 29.11.05
  7. #==============================================================================
  8. # Lets the Player or Event draw a path from an desonation to the source. This
  9. # method is very fast and because the pathfinding is imbedded into the Game
  10. # Character the pathfinding can be interrupted or redrawn at any time.
  11. #==============================================================================
  12. # Player :: $game_player.find_path(x,y)
  13. # Event Script Call :: self.event.find_path(x,y)
  14. # Event Movement Script Call :: self.find_path(x,y)
  15. #==============================================================================
  16. # [VX] Simple Mouse System Note: I edited the method
  17. # character.passable?(x, y, direction) to character.passable?(x, y)
  18. # according to change of this method in VX.
  19. #------------------------------------------------------------------------------

  20. class Game_Character
  21.   #--------------------------------------------------------------------------
  22.   alias nf_pf_game_character_initialize initialize
  23.   alias nf_pf_game_character_update update
  24.   #--------------------------------------------------------------------------
  25.   attr_accessor :map
  26.   attr_accessor :runpath
  27.   #--------------------------------------------------------------------------
  28.   def initialize
  29.     nf_pf_game_character_initialize
  30.     @map = nil
  31.     @runpath = false
  32.   end
  33.   #--------------------------------------------------------------------------
  34.   def update
  35.     run_path if @runpath == true
  36.     nf_pf_game_character_update
  37.   end
  38.   #--------------------------------------------------------------------------
  39.   def run_path
  40.     return if moving?
  41.     step = @map[@x,@y]
  42.     if step == 1
  43.       @map = nil
  44.       @runpath = false
  45.       return
  46.     end
  47.     dir = rand(2)
  48.     case dir
  49.     when 0
  50.       move_right if @map[@x+1,@y] == step - 1 and step != 0
  51.       move_down if @map[@x,@y+1] == step - 1 and step != 0
  52.       move_left if @map[@x-1,@y] == step -1 and step != 0
  53.       move_up if @map[@x,@y-1] == step - 1 and step != 0
  54.     when 1
  55.       move_up if @map[@x,@y-1] == step - 1 and step != 0
  56.       move_left if @map[@x-1,@y] == step -1 and step != 0
  57.       move_down if @map[@x,@y+1] == step - 1 and step != 0
  58.       move_right if @map[@x+1,@y] == step - 1 and step != 0
  59.     end
  60.   end
  61.   #--------------------------------------------------------------------------
  62.   def find_path(x,y)
  63.     sx, sy = @x, @y
  64.     result = setup_map(sx,sy,x,y)
  65.     @runpath = result[0]
  66.     @map = result[1]
  67.     @map[sx,sy] = result[2] if result[2] != nil
  68.   end
  69.   #--------------------------------------------------------------------------
  70.   def clear_path
  71.     @map = nil
  72.     @runpath = false
  73.   end
  74.   #--------------------------------------------------------------------------
  75.   def setup_map(sx,sy,ex,ey)
  76.     map = Table.new($game_map.width, $game_map.height)
  77.     map[ex,ey] = 1
  78.     old_positions = []
  79.     new_positions = []
  80.     old_positions.push([ex, ey])
  81.     depth = 2
  82.     depth.upto(100){|step|
  83.       loop do
  84.         break if old_positions[0] == nil
  85.         x,y = old_positions.shift
  86.         return [true, map, step] if x == sx and y+1 == sy
  87.         if $game_player.passable?(x, y) and map[x,y + 1] == 0
  88.           map[x,y + 1] = step
  89.           new_positions.push([x,y + 1])
  90.         end
  91.         return [true, map, step] if x-1 == sx and y == sy
  92.         if $game_player.passable?(x, y) and map[x - 1,y] == 0
  93.           map[x - 1,y] = step
  94.           new_positions.push([x - 1,y])
  95.         end
  96.         return [true, map, step] if x+1 == sx and y == sy
  97.         if $game_player.passable?(x, y) and map[x + 1,y] == 0
  98.           map[x + 1,y] = step
  99.           new_positions.push([x + 1,y])
  100.         end
  101.         return [true, map, step] if x == sx and y-1 == sy
  102.         if $game_player.passable?(x, y) and map[x,y - 1] == 0
  103.           map[x,y - 1] = step
  104.           new_positions.push([x,y - 1])
  105.         end
  106.       end
  107.       old_positions = new_positions
  108.       new_positions = []
  109.     }
  110.     return [false, nil, nil]
  111.   end
  112. end
  113.   
  114. class Game_Map
  115.   #--------------------------------------------------------------------------
  116.   alias pf_game_map_setup setup
  117.   #--------------------------------------------------------------------------
  118.   def setup(map_id)
  119.     pf_game_map_setup(map_id)
  120.     $game_player.clear_path
  121.   end
  122. end
  123.   
  124. class Game_Player
  125.   #--------------------------------------------------------------------------
  126.   alias pf_game_player_update update
  127.   #--------------------------------------------------------------------------
  128.   def update
  129.     $game_player.clear_path if Input.dir4 != 0
  130.     pf_game_player_update
  131.   end
  132. end
  133.   
  134. class Interpreter
  135.   #--------------------------------------------------------------------------
  136.   def event
  137.     return $game_map.events[@event_id]
  138.   end
  139. end
复制代码
在事件指令RGSS里写:$game_event(1,0).clear_path
                               $game_event(1,0).find_path(8, 12)
结果脚本错误。请高手解惑!


woodytt于2011-8-2 14:31补充以下内容:
解决了!!!把clear_path和find_path(8, 12)直接写到“事件指令——设定移动路线”里就行了


woodytt于2011-8-2 14:32补充以下内容:
可以做成很多类型的游戏啦dsu_plus_rewardpost_czw
作者: fux2    时间: 2011-8-2 17:22
既然这个脚本是作用在Character类上面的,你可以试试在事件"移动路线"里设定脚本"find_path(x,y)"
当然,这只是猜测,不行再问吧.




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