Class: GosuEnhanced::Region

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/gosu_enhanced/region.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pos, size) ⇒ Region

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 Points.



24
25
26
27
28
29
# 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

Instance Attribute Details

#positionObject (readonly)

Top left corner



14
15
16
# File 'lib/gosu_enhanced/region.rb', line 14

def position
  @position
end

#sizeObject (readonly)

Overall Width and Height



16
17
18
# File 'lib/gosu_enhanced/region.rb', line 16

def size
  @size
end

Instance Method Details

#contains?(col, row = nil) ⇒ Boolean

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

Returns:

  • (Boolean)


33
34
35
36
37
38
# 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) ⇒ Object

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



57
58
59
# File 'lib/gosu_enhanced/region.rb', line 57

def draw(surface, z_order, colour)
  surface.draw_rectangle(position, size, z_order, colour)
end

#dupObject

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



51
52
53
# File 'lib/gosu_enhanced/region.rb', line 51

def dup
  Region.new(position, size)
end

#leftObject

Return the leftmost co-ordinate



46
47
48
# File 'lib/gosu_enhanced/region.rb', line 46

def left
  position.x
end

#to_sObject

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



62
63
64
# File 'lib/gosu_enhanced/region.rb', line 62

def to_s
  "<GosuEnhanced::Region: #{position}, #{size}>"
end

#topObject

Return the topmost co-ordinate



41
42
43
# File 'lib/gosu_enhanced/region.rb', line 41

def top
  position.y
end