Class: Rectangle

Inherits:
Object
  • Object
show all
Defined in:
src/ruby/models/rectangle.rb,
mrb_doc/models/rectangle.rb

Overview

The Rectangle class is used for drawing and also collision checking.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, width, height) ⇒ Rectangle

Creates a new instance of Rectangle

Parameters:

  • x (Integer)
  • y (Integer)
  • width (Integer)
  • height (Integer)


8
9
10
11
12
# File 'mrb_doc/models/rectangle.rb', line 8

def initialize(x, y, width, height)
  # mrb_Rectangle_initialize
  # src/mruby_integration/models/rectangle.cpp
  Rectangle.new
end

Instance Attribute Details

#heightInteger

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/rectangle.rb', line 4

def height
  @height
end

#widthInteger

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/rectangle.rb', line 4

def width
  @width
end

#xInteger

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/rectangle.rb', line 4

def x
  @x
end

#yInteger

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/rectangle.rb', line 4

def y
  @y
end

Instance Method Details

#draw(origin: Vector2::ZERO, rotation: 0, outline: false, thickness: 2, rounded: false, radius: 0.5, segments: 8, colour: BLACK) ⇒ Rectangle

Draws a rectangle in several configurations

Parameters:

  • origin (Vector2) (defaults to: Vector2::ZERO)
  • rotation (Float) (defaults to: 0)

    only usable when outline and rounded are false

  • outline (Boolean) (defaults to: false)
  • thickness (Integer) (defaults to: 2)

    only used when outline is true

  • rounded (Boolean) (defaults to: false)
  • radius (Float) (defaults to: 0.5)

    A value between 0.0 and 1.0 and only used when rounded is true

Returns:

Raises:

  • (ArgumentError)

    If the radius is out of bounds



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'src/ruby/models/rectangle.rb', line 26

def draw(
  origin: Vector2::ZERO,
  rotation: 0,    # only usable when outline and rounded are false
  outline: false,
  thickness: 2,   # only used when outline is true
  rounded: false,
  radius: 0.5,    # only used when rounded is true
  segments: 8,    # only used when rounded is true
  colour: BLACK
)

  if rounded
    unless (0..1).include?(radius)
      raise ArgumentError, "Radius must fall between 0 and 1, you gave me #{radius}"
    end

    if outline
      draw_rectangle_rounded_lines(self, radius, segments, thickness, colour)
    else
      draw_rectangle_rounded(self, radius, segments, colour)
    end
  else
    if outline
      draw_rectangle_lines_ex(self, thickness, colour)
    else
      draw_rectangle_pro(self, origin, rotation, colour)
    end
  end
end

#to_hHash

Return the object represented by a Hash

Returns:

  • (Hash)


8
9
10
11
12
13
14
15
# File 'src/ruby/models/rectangle.rb', line 8

def to_h
  {
    x: x,
    y: y,
    width: width,
    height: height,
  }
end