Advertisement
Vlue

Proximity

Aug 5th, 2012
10,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.02 KB | None | 0 0
  1. ##Proximity Detection Script v1.5a
  2. #
  3. #Usage: Functions mainly used to activate events when within a certain range!
  4. #
  5. #Functions:
  6. #  Proxy.inprox?(@event_id, distance, los)
  7. #    -distance is the range of detection and los is whether to use line of sight,
  8. #     both have default values
  9. #  Proxy.inprox_d?(@event_id, distance, los)
  10. #    -same as original but only checks in the direction the event is facing
  11. #  Proxy.inprox_r?(@event_id, width, height)
  12. #    -checks a rectangle of width/height around the event (odd values best)
  13. #
  14. # All calls return true if within range and false when not
  15. #
  16. #Examples:
  17. # Proxy.inprox?(@event_id)
  18. # Proxy.inprox?(@event_id,10)
  19. # Proxy.inprox?(@event_id,5,false)
  20. # Proxy.inprox_d?(@event_id,6)
  21. # Proxy.inprox_r?(@event_id,3,3)
  22. #
  23. #------#
  24. #-- Script by: V.M of D.T
  25. #
  26. #- Questions or comments can be:
  27. #    given by email: sumptuaryspade@live.ca
  28. #    provided on facebook: http://www.facebook.com/DaimoniousTailsGames
  29. #   All my other scripts and projects can be found here: http://daimonioustails.weebly.com/
  30. #
  31. #--- Free to use in any project, commercial or non-commercial, with credit given
  32. # - - Though a donation's always a nice way to say thank you~ (I also accept actual thank you's)
  33.  
  34.  
  35. module Proxy
  36.   #Default radius of detection:
  37.   PROXYRANGE = 4
  38.   #Region for transparent obstacles
  39.   REGION = 20
  40.   #Switch to be turned on to pause Proximity
  41.   PAUSE_SWITCH = 100
  42.   #----#
  43.   def self.inprox?(id, distance = PROXYRANGE, los = true, second_id = nil)
  44.     return if $game_switches[PAUSE_SWITCH]
  45.     x = $game_map.events[id].x
  46.     y = $game_map.events[id].y
  47.     if !second_id
  48.       x2 = $game_player.x; y2 = $game_player.y
  49.     else
  50.       x2 = $game_map.events[second_id].x;y2 = $game_map.events[second_id].y
  51.     end
  52.     x_d = x - x2; y_d = y - y2
  53.     x_d *= -1 if x_d < 0; y_d *= -1 if y_d < 0
  54.     t_d = x_d + y_d
  55.     return false if t_d > distance
  56.     return false if !line_of_sight($game_player.x,$game_player.y,x,y) and los
  57.     return true
  58.   end
  59.   def self.inprox_d?(id, distance = PROXYRANGE, los = true)
  60.     if self.inprox?(id, distance, los) then else return false end
  61.     x1 = $game_player.x; x2 = $game_map.events[id].x
  62.     y1 = $game_player.y; y2 = $game_map.events[id].y
  63.     x1 > x2 ? xx = x1 - x2 : xx = x2 - x1
  64.     y1 > y2 ? yy = y1 - y2 : yy = y2 - y1
  65.     case $game_map.events[id].direction
  66.     when 2
  67.       if $game_player.y > $game_map.events[id].y then
  68.         if yy >= xx then return true end end
  69.     when 4
  70.       if $game_player.x < $game_map.events[id].x then
  71.         if xx >= yy then return true end end
  72.     when 6
  73.       if $game_player.x > $game_map.events[id].x then
  74.         if xx >= yy then return true end end
  75.     when 8
  76.       if $game_player.y < $game_map.events[id].y then
  77.         if yy >= xx then return true end end
  78.     end
  79.     return false
  80.   end
  81.   def self.inprox_r?(id, width, height)
  82.     return if $game_switches[PAUSE_SWITCH]
  83.     width % 2 == 0 ? hwidth = width / 2 : hwidth = (width - 1) / 2
  84.     height % 2 == 0 ? hheight = height / 2 : hheight = (height - 1) / 2
  85.     x = $game_map.events[id].x - hwidth
  86.     y = $game_map.events[id].y - hheight
  87.     if $game_player.x >= x and $game_player.x < (x + width)
  88.       if $game_player.y >= y and $game_player.y < (y + height)
  89.         return true
  90.       end
  91.     end
  92.     return false
  93.   end
  94.   def self.line_of_sight(x,y,x2,y2)
  95.     tile_array = []
  96.     x_d = x - x2; y_d = y - y2
  97.     x_d *= -1 if x_d < 0
  98.     y_d *= -1 if y_d < 0
  99.     t_d = x_d + y_d
  100.     t_d.to_i.times do |i|
  101.       x_distance = x - x2
  102.       y_distance = y - y2
  103.       x_distance *= -1 if x_distance < 0
  104.       y_distance *= -1 if y_distance < 0
  105.       if x_distance > y_distance or y_distance == 0
  106.         x < x2 ? x += 1 : x -= 1
  107.       elsif
  108.         y < y2 ? y += 1 : y -= 1 or x_distance == 0
  109.       end
  110.       tile_array.push([x,y])
  111.     end
  112.     tile_array.each do |cord|
  113.       next if $game_map.region_id(cord[0],cord[1]) == REGION
  114.       return false if !$game_map.check_passage(cord[0], cord[1], 0x002)
  115.     end
  116.     return true
  117.   end
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement