Project1

标题: 求一个NPC接近NPC的脚本 [打印本页]

作者: 5810707    时间: 2011-7-21 19:40
标题: 求一个NPC接近NPC的脚本
如题.在详细说明下。就是指定的NPC接近指定的NPC指定的步数。就类似雨设置NPC行走方式里面的接近主角。
但是我要的是接近指定的NPCdsu_plus_rewardpost_czw
作者: 月夜神音    时间: 2011-7-21 19:52
  1. #===============================================================
  2. # ● [VX] ? Improved & Special Move Commands ? □
  3. #--------------------------------------------------------------
  4. # ? by Woratana [[email protected]]
  5. # ? Released on: 20/03/2008
  6. # ? Version: 1.0
  7. #--------------------------------------------------------------
  8. =begin
  9. □=====□=====□=====□=====□=====□=====□=====□=====□=====□
  10.                    + COMMANDS LIST +
  11. □=====□=====□=====□=====□=====□=====□=====□=====□=====□
  12. ################################
  13. ● IMPROVED MOVE COMMANDS ●

  14. □ SAFE JUMP:
  15. Don't Jump if its destination is OUT OF THE SCREEN (or) NOT PASSABLE.
  16. (Old jump will not check before jump)

  17. □ IMPROVED RANDOM MOVE
  18. Find the movable direction before move.
  19. (Old random move will just skip to move in that frame, if its destination is not passable.)

  20. □ IMPROVED COLLIDE_WITH_CHARACTERS CHECK
  21. 'Same as Characters' events are able to walk on other 'Below Characters' events.

  22. ● Note: If you don't want one of this improved command, just delete its part.
  23. (I put all of their command name above their script part)
  24. ################################

  25. ################################
  26. ● SPECIAL(NEW) MOVE COMMANDS ●

  27. ++[HOW TO USE]++
  28. ● Open 'Move Route' window, (or event command 'Set Move Route')
  29. Click 'Script...' and type the move command you want...

  30. □ MOVE TOWARD POSITION X/Y
  31. move_toward_pos(x,y)

  32. □ MOVE AWAY FROM POSITION X/Y
  33. move_away_from_pos(x,y)

  34. □ MOVE TOWARD EVENT ID
  35. move_toward_event(id)

  36. □ MOVE AWAY FROM EVENT ID
  37. move_away_from_event(id)

  38. □ TURN TOWARD POSITION X/Y
  39. turn_toward_pos(x,y)

  40. □ TURN AWAY FROM POSITION X/Y
  41. turn_away_from_pos(x,y)

  42. □ TURN TOWARD EVENT ID
  43. turn_toward_event(id)

  44. □ TURN AWAY FROM EVENT ID
  45. turn_away_from_event(id)
  46. ################################
  47. =end
  48. class Game_Character
  49.   ##################################################################
  50.   # IMPROVED MOVE COMMANDS
  51.   ##################################################################
  52.   #-------------------------------------------------------------
  53.   # SAFE JUMP
  54.   #-------------------------------------------------------------
  55.   def jump(x_plus, y_plus)
  56.     if x_plus.abs > y_plus.abs            # 横の距離のほうが長い
  57.       x_plus < 0 ? turn_left : turn_right
  58.     elsif x_plus.abs > y_plus.abs         # 縦の距離のほうが長い
  59.       y_plus < 0 ? turn_up : turn_down
  60.     end
  61.     new_x = @x + x_plus
  62.     new_y = @y + y_plus
  63.     if (x_plus == 0 and y_plus == 0) or passable?(new_x, new_y)
  64.       @x += x_plus
  65.       @y += y_plus
  66.       distance = Math.sqrt(x_plus * x_plus + y_plus * y_plus).round
  67.       @jump_peak = 10 + distance - @move_speed
  68.       @jump_count = @jump_peak * 2
  69.       @stop_count = 0
  70.       straighten
  71.     end
  72.   end
  73.   
  74.   #-------------------------------------------------------------
  75.   # IMPROVED RANDOM MOVE
  76.   #-------------------------------------------------------------
  77.   def move_random
  78.     safe = false
  79.     checked = []
  80.     while safe == false
  81.       break if checked.include?(0) and checked.include?(1) and
  82.       checked.include?(2) and checked.include?(3)
  83.       case rand(4)
  84.       when 0; return if checked.include?(0); checked.push 0
  85.         if passable?(@x, @y + 1)
  86.           safe = true; move_down(false)
  87.         end
  88.       when 1; return if checked.include?(1); checked.push 1
  89.         if passable?(@x - 1, @y)
  90.           safe = true; move_left(false)
  91.         end
  92.       when 2; return if checked.include?(2); checked.push 2
  93.         if passable?(@x + 1, @y)
  94.           safe = true; move_right(false)
  95.         end
  96.       when 3; return if checked.include?(3); checked.push 3
  97.         if passable?(@x - 1, @y)
  98.           safe = true; move_up(false)
  99.         end
  100.       end
  101.     end
  102.   end
  103.   
  104.   #----------------------------------------------------------------------
  105.   # IMPROVED COLLIDE_WITH_CHARACTERS CHECK
  106.   #----------------------------------------------------------------------
  107.   def collide_with_characters?(x, y)
  108.     for event in $game_map.events_xy(x, y)          # Matches event position
  109.       unless event.through                          # Passage OFF?
  110.         return true if event.priority_type == 1     # Target is normal char
  111.       end
  112.     end
  113.     if @priority_type == 1                          # Self is normal char
  114.       return true if $game_player.pos_nt?(x, y)     # Matches player position
  115.       return true if $game_map.boat.pos_nt?(x, y)   # Matches boat position
  116.       return true if $game_map.ship.pos_nt?(x, y)   # Matches ship position
  117.     end
  118.     return false
  119.   end
  120.   
  121.   ##################################################################
  122.   # SPECIAL(NEW) MOVE COMMANDS
  123.   ##################################################################
  124.   #--------------------------------------------------------------------------
  125.   # * Move toward Position
  126.   #--------------------------------------------------------------------------
  127.   def move_toward_pos(x,y)
  128.     sx = distance_x_from_pos(x)
  129.     sy = distance_y_from_pos(y)
  130.     if sx != 0 or sy != 0
  131.       if sx.abs > sy.abs                  # Horizontal distance is longer
  132.         sx > 0 ? move_left : move_right   # Prioritize left-right
  133.         if @move_failed and sy != 0
  134.           sy > 0 ? move_up : move_down
  135.         end
  136.       else                                # Vertical distance is longer
  137.         sy > 0 ? move_up : move_down      # Prioritize up-down
  138.         if @move_failed and sx != 0
  139.           sx > 0 ? move_left : move_right
  140.         end
  141.       end
  142.     end
  143.   end
  144.   #--------------------------------------------------------------------------
  145.   # * Move away from Position
  146.   #--------------------------------------------------------------------------
  147.   def move_away_from_pos(x,y)
  148.     sx = distance_x_from_pos(x)
  149.     sy = distance_y_from_pos(y)
  150.     if sx != 0 or sy != 0
  151.       if sx.abs > sy.abs                  # Horizontal distance is longer
  152.         sx > 0 ? move_right : move_left   # Prioritize left-right
  153.         if @move_failed and sy != 0
  154.           sy > 0 ? move_down : move_up
  155.         end
  156.       else                                # Vertical distance is longer
  157.         sy > 0 ? move_down : move_up      # Prioritize up-down
  158.         if @move_failed and sx != 0
  159.           sx > 0 ? move_right : move_left
  160.         end
  161.       end
  162.     end
  163.   end
  164.   #--------------------------------------------------------------------------
  165.   # * Move toward Event
  166.   #--------------------------------------------------------------------------
  167.   def move_toward_event(id)
  168.     move_toward_pos($game_map.events[id].x,$game_map.events[id].y)
  169.   end
  170.   #--------------------------------------------------------------------------
  171.   # * Move away from Event
  172.   #--------------------------------------------------------------------------
  173.   def move_away_from_event(id)
  174.     move_away_from_pos($game_map.events[id].x,$game_map.events[id].y)
  175.   end
  176.   #--------------------------------------------------------------------------
  177.   # * Turn toward Position
  178.   #--------------------------------------------------------------------------
  179.   def turn_toward_pos(x,y)
  180.     sx = distance_x_from_pos(x)
  181.     sy = distance_y_from_pos(y)
  182.     if sx.abs > sy.abs                    # Horizontal distance is longer
  183.       sx > 0 ? turn_left : turn_right
  184.     elsif sx.abs < sy.abs                 # Vertical distance is longer
  185.       sy > 0 ? turn_up : turn_down
  186.     end
  187.   end
  188.   #--------------------------------------------------------------------------
  189.   # * Turn away from Position
  190.   #--------------------------------------------------------------------------
  191.   def turn_away_from_pos(x,y)
  192.     sx = distance_x_from_pos(x)
  193.     sy = distance_y_from_pos(y)
  194.     if sx.abs > sy.abs                    # Horizontal distance is longer
  195.       sx > 0 ? turn_right : turn_left
  196.     elsif sx.abs < sy.abs                 # Vertical distance is longer
  197.       sy > 0 ? turn_down : turn_up
  198.     end
  199.   end
  200.   #--------------------------------------------------------------------------
  201.   # * Turn toward Event
  202.   #--------------------------------------------------------------------------
  203.   def turn_toward_event(id)
  204.     turn_toward_pos($game_map.events[id].x,$game_map.events[id].y)
  205.   end
  206.   #--------------------------------------------------------------------------
  207.   # * Turn away from Event
  208.   #--------------------------------------------------------------------------
  209.   def turn_away_from_event(id)
  210.     turn_away_from_pos($game_map.events[id].x,$game_map.events[id].y)
  211.   end
  212.   #--------------------------------------------------------------------------
  213.   # * Calculate X Distance From Event
  214.   #--------------------------------------------------------------------------
  215.   def distance_x_from_pos(x)
  216.     sx = @x - x
  217.     if $game_map.loop_horizontal?         # When looping horizontally
  218.       if sx.abs > $game_map.width / 2     # Larger than half the map width?
  219.         sx -= $game_map.width             # Subtract map width
  220.       end
  221.     end
  222.     return sx
  223.   end
  224.   #--------------------------------------------------------------------------
  225.   # * Calculate Y Distance From Event
  226.   #--------------------------------------------------------------------------
  227.   def distance_y_from_pos(y)
  228.     sy = @y - y
  229.     if $game_map.loop_vertical?           # When looping vertically
  230.       if sy.abs > $game_map.height / 2    # Larger than half the map height?
  231.         sy -= $game_map.height            # Subtract map height
  232.       end
  233.     end
  234.     return sy
  235.   end
  236. end
复制代码
使用方法:move_toward_event(事件ID)




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