lib/ray/rect.rb in ray-0.1.0.pre1 vs lib/ray/rect.rb in ray-0.1.0
- old
+ new
@@ -1,11 +1,17 @@
module Ray
class Rect
+ class << self
+ alias :[] :new
+ end
+
def inspect
- "#<#{self.class} {{#{x}, #{y}}, {#{w}, #{h}}}>"
+ "(#{pos}, #{size})"
end
+ alias :to_s :inspect
+
# @return [true, false] True if the receiver is inside the rect.
# (false if they just collide)
def inside?(rect)
rect = rect.to_rect
(x >= rect.x) && (y >= rect.y) &&
@@ -52,10 +58,15 @@
# @return [Ray::Vector2] bottom right corner
def bottom_right
Ray::Vector2[x + w, y + h]
end
+ # @return [Ray::Vector2]
+ def center
+ Ray::Vector2[x + w / 2, y + h / 2]
+ end
+
# @return [true, false] True if the two rects are equal
def ==(rect)
return false unless rect.is_a? Rect
x == rect.x && y == rect.y && w == rect.w && h == rect.h
end
@@ -89,17 +100,17 @@
alias :h= :height=
end
end
class Array
- # Converts an Array to a rect
+ # @return [Ray::Rect] Converts an array to a rect.
def to_rect
Ray::Rect.new(*self)
end
end
class Hash
- # Converts a Hash to a rect
+ # @return [Ray::Rect] Converts a hash into a rect.
def to_rect
Ray::Rect.new(self)
end
end