1. #==============================================================================
2. # ** Modules.View Range (5.0) By Near Fantastica & SephirothSpawn
3. #------------------------------------------------------------------------------
4. # * Description :
5. #
6. # The View Range Module is used to test objects position in comparision with
7. # another object. It can test if an object is within a defined circular
8. # range of another object or a rectangular defined region. It can also tell
9. # you this distances between objects.
10. #------------------------------------------------------------------------------
11. # * Syntax :
12. #
13. # Test if object is within defined range from another object :
14. # - VR.in_range?(object1, object2, range)
15. # - VR.in_range?(object1x, object1y, object2x, object2y, range)
16. #
17. # Test if object is within defined rectanlge :
18. # - VR.in_rect_range?(object, <args>)
19. # - VR.in_rect_range?(x, y, <args>)
20. #
21. # args can either be : x, y, width, height or Rect.new(x, y, width, height)
22. #
23. # Test range between objects :
24. # - range = VR.range(object1, object2, <integer>)
25. # - range = VR.range(object1x, object1y, object2x, object2y, <integer>)
26. #
27. # integer : if true, returns integer ; if false, returns float
28. #==============================================================================
29.
30. MACL::Loaded << 'Modules.View Range'
31.
32. #==============================================================================
33. # ** View Range
34. #==============================================================================
35.
36. module VR
37. #----------------------------------------------------------------------------
38. # * Within Range Test
39. #----------------------------------------------------------------------------
40. def VR.in_range?(*args)
41. # If 3 Arguments (Element, Object, Range)
42. if args.size == 3
43. x = (args[0].x - args[1].x) ** 2
44. y = (args[0].y - args[1].y) ** 2
45. r = args[2]
46. # If 5 Arguments (Elementx, Elementy, Objectx, Objecty, Range)
47. elsif args.size == 5
48. x = (args[0] - args[2]) ** 2
49. y = (args[1] - args[3]) ** 2
50. r = args[4]
51. else
52. p 'Wrong Defined Number of Arguments'
53. return
54. end
55. return (x + y) <= (r * r)
56. end
57. #----------------------------------------------------------------------------
58. # * Within Rect Range Test
59. #----------------------------------------------------------------------------
60. def VR.in_rect_range?(*args)
61. # If 2 Arguments (Object, Rect)
62. if args.size == 2
63. x_, y_ = args[0].x, args[0].y
64. x, y, w, h = args[1].x, args[1].y, args[1].width, args[1].height
65. # If 3 Arguments (Objectx, Objecty, Rect)
66. elsif args.size == 3
67. x_, y_ = args[0], args[1]
68. x, y, w, h = args[2].x, args[2].y, args[2].width, args[2].height
69. # If 5 Arguments (Object, Rectx, Recty, Rectwidth, Rectheight)
70. elsif args.size == 5
71. x_, y_ = args[0].x, args[0].y
72. x, y, w, h = args[1], args[2], args[3], args[4]
73. # If 6 Arguments (Objectx, Objecty, Rectx, Recty, Rectwidth, Rectheight)
74. elsif args.size == 6
75. x_, y_, x, y, w, h = *args
76. else
77. p 'Wrong Defined Number of Arguments'
78. return
79. end
80. # Return Object Within Rect
81. return x_.between?(x, x + w) && y_.between?(y, y + h)
82. end
83. #----------------------------------------------------------------------------
84. # * Range
85. #----------------------------------------------------------------------------
86. def VR.range(*args)
87. # If 2 Arguments (Element, Object)
88. if args.size == 2
89. x = (args[0].x - args[1].x) * (args[0].x - args[1].x)
90. y = (args[0].y - args[1].y) * (args[0].y - args[1].y)
91. integer = true
92. # If 3 Arguments (Element, Object, Integer
93. elsif args.size == 3
94. x = (args[0].x - args[1].x) * (args[0].x - args[1].x)
95. y = (args[0].y - args[1].y) * (args[0].y - args[1].y)
96. integer = args[2]
97. # If 4 Arguments (Elementx, Elementy, Objectx, Objecty)
98. elsif args.size == 4
99. x = (args[0] - args[2]) * (args[0] - args[2])
100. y = (args[1] - args[3]) * (args[1] - args[3])
101. integer = true
102. # If 5 Arguments (Elementx, Elementy, Objectx, Objecty, integer)
103. elsif args.size == 5
104. x = (args[0] - args[2]) * (args[0] - args[2])
105. y = (args[1] - args[3]) * (args[1] - args[3])
106. integer = args[4]
107. else
108. p 'Wrong Defined Number of Arguments'
109. return
110. end
111. r = Math.sqrt(x + y)
112. return integer ? r.to_i : r
113. end
114. end