class GosuEnhanced::Region

Hold a rectangular region specified by a Point and a Size Most functions are delegated to the constituent Point and Size

Attributes

position[R]

Top left corner

size[R]

Overall Width and Height

Public Class Methods

new(pos, size) click to toggle source

Create a new region with specified pos as top left corner and size as width and height. The stored positions are copies of the passed position and size to avoid aliasing.

Alternatively, can be initialized with 2 +Point+s.

# File lib/gosu_enhanced/region.rb, line 24
def initialize(pos, size)
  size = Size.new(size.x - pos.x, size.y - pos.y) if size.respond_to? :x

  @position = pos.dup
  @size     = size.dup
end

Public Instance Methods

contains?(col, row = nil) click to toggle source

Return whether the region contains the specified row and col Alternatively, can be passed a Point

# File lib/gosu_enhanced/region.rb, line 33
def contains?(col, row = nil)
  return contains_point?(col) if col.respond_to? :x

  col.between?(left, left + width - 1) &&
    row.between?(top, top + height - 1)
end
draw(surface, z_order, colour) click to toggle source

Draw a rectangle on the specified surface at the specified z_order and with the specified colour

# File lib/gosu_enhanced/region.rb, line 57
def draw(surface, z_order, colour)
  surface.draw_rectangle(position, size, z_order, colour)
end
dup() click to toggle source

Duplicate a Region, must be done explicitly to avoid aliasing.

# File lib/gosu_enhanced/region.rb, line 51
def dup
  Region.new(position, size)
end
left() click to toggle source

Return the leftmost co-ordinate

# File lib/gosu_enhanced/region.rb, line 46
def left
  position.x
end
to_s() click to toggle source

Return a string representation of the region's position and size

# File lib/gosu_enhanced/region.rb, line 62
def to_s
  "<GosuEnhanced::Region: #{position}, #{size}>"
end
top() click to toggle source

Return the topmost co-ordinate

# File lib/gosu_enhanced/region.rb, line 41
def top
  position.y
end

Private Instance Methods

contains_point?(pt) click to toggle source

Return whether the passed Point pt is contained within the current Region.

# File lib/gosu_enhanced/region.rb, line 70
def contains_point?(pt)
  pt.x.between?(left, left + width - 1) &&
    pt.y.between?(top, top + height - 1)
end