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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, width:, height:, colour: Colour::BLACK, outline: nil, thickness: 1, roundness: 0, segments: 4) ⇒ Rectangle

Creates a new instance of Rectangle.

Examples:

Basic usage

rectangle = Rectangle.new(x: 10, y: 20, width: 32, height: 32, colour: Colour::GREEN)

Parameters:

  • x (Float)
  • y (Float)
  • width (Float)
  • height (Float)
  • colour (Colour) (defaults to: Colour::BLACK)
  • outline (Colour) (defaults to: nil)
  • thickness (Float) (defaults to: 1)

    Must be greater than 0

  • roundness (Float) (defaults to: 0)

    Must be within (0.0..1.0)

  • segments (Integer) (defaults to: 4)

    Must be greater than 0

Raises:

  • (ArgumentError)


27
28
29
30
# File 'mrb_doc/models/rectangle.rb', line 27

def initialize(x:, y:, width:, height:, colour: Colour::BLACK, outline: nil, thickness: 1, roundness: 0, segments: 4)
  # mrb_Rectangle_initialize
  # src/mruby_integration/models/rectangle.cpp
end

Instance Attribute Details

#colourColour

Returns:



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

def colour
  @colour
end

#heightFloat

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/rectangle.rb', line 3

def height
  @height
end

#outlineColour

Returns:



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

def outline
  @outline
end

#roundnessFloat

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/rectangle.rb', line 3

def roundness
  @roundness
end

#segmentsInteger

Returns:

  • (Integer)


6
7
8
# File 'mrb_doc/models/rectangle.rb', line 6

def segments
  @segments
end

#thicknessFloat

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/rectangle.rb', line 3

def thickness
  @thickness
end

#widthFloat

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/rectangle.rb', line 3

def width
  @width
end

#xFloat

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/rectangle.rb', line 3

def x
  @x
end

#yFloat

Returns:

  • (Float)


3
4
5
# File 'mrb_doc/models/rectangle.rb', line 3

def y
  @y
end

Class Method Details

.[](x, y, width, height, colour = Colour::BLACK) ⇒ Rectangle

Shorthand for initializing a new Rectangle.

Examples:

Basic usage

source = Rectangle[8, 16, 24, 32]
p source.to_h

# => {
#      x: 8,
#      y: 16,
#      width: 24,
#      height: 32,
#    }

Parameters:

  • y (Float)
  • x (Float)
  • width (Float)
  • height (Float)
  • colour (Colour) (defaults to: Colour::BLACK)

Returns:



25
26
27
# File 'src/ruby/models/rectangle.rb', line 25

def self.[](x, y, width, height, colour = Colour::BLACK)
  new(x: x, y: y, width: width, height: height, colour: colour)
end

Instance Method Details

#begin_scissoringnil

Start scissoring within the Rectangle, only drawing that happens within the bounds of the Rectangle will actually be drawn to the screen.

Examples:

Basic usage

portal = Rectangle.new(x: 100, y: 100, width: 50, height: 75, colour: Colour::BLUE)
portal.begin_scissoring
begin
  # Drawing code here
ensure
  portal.end_scissoring
end

Returns:

  • (nil)


72
73
74
75
76
# File 'mrb_doc/models/rectangle.rb', line 72

def begin_scissoring
  # mrb_Rectangle_begin_scissoring
  # src/mruby_integration/models/rectangle.cpp
  nil
end

#drawnil

Draws the Rectangle.

If outline is specified then the rectangle will be drawn with an outline of that Colour. You can specify the thickness to make it thicker.

if roundness is specified then the rectangle will be drawn with rounded corners.

Examples:

Basic usage

rectangle = Rectangle.new(x: 100, y: 100, width: 50, height: 60, colour:Colour::RED)
rectangle.draw

rectangle_with_roundness = Rectangle.new(x: 100, y: 100, width: 50, height: 60,, colour:Colour::RED, roundness: 0.25)
rectangle_with_roundness.draw

rectangle_with_outline = Rectangle.new(x: 100, y: 100, width: 50, height: 60, colour:Colour::RED, outline: Colour::BLUE, thickness: 3)
rectangle_with_outline.draw

rectangle_with_roundness_and_outline = Rectangle.new(x: 100, y: 100, width: 50, height: 60, colour:Colour::RED, roundness: 0.5, outline: Colour::BLUE, thickness: 3)
rectangle_with_roundness_and_outline.draw

Returns:

  • (nil)


53
54
55
56
57
# File 'mrb_doc/models/rectangle.rb', line 53

def draw
  # mrb_Rectangle_draw
  # src/mruby_integration/models/rectangle.cpp
  nil
end

#end_scissoringnil

Ends scissoring within the Rectangle.

Examples:

Basic usage

portal = Rectangle.new(x: 100, y: 100, width: 50, height: 75, colour: Colour::BLUE)
portal.begin_scissoring
begin
  # Drawing code here
ensure
  portal.end_scissoring
end

Returns:

  • (nil)


90
91
92
93
94
# File 'mrb_doc/models/rectangle.rb', line 90

def end_scissoring
  # mrb_Rectangle_end_scissoring
  # src/mruby_integration/models/rectangle.cpp
  nil
end

#overlaps?(other) ⇒ Boolean

Returns true if the Vector2 is contained inside this Rectangle or is on the border of.

Examples:

Basic usage

hitbox = Rectangle.new(x: 10, y: 10, width: 10, height: 10)
position = Vector2.new(x: 5, y: 5)

puts hitbox.overlaps?(position)
# => false

# The position changes to inside the rectangle...
position = Vector2.new(x: 15, y: 15)

puts hitbox.overlaps?(position)
# => true

# The position changes to the corner of the rectangle...
position = Vector2.new(x: 10, y: 10)

puts hitbox.overlaps?(position)
# => true

# The position changes to far outside rectangle...
position = Vector2.new(x: 100, y: 100)

puts hitbox.overlaps?(position)
# => false

Basic Rectangle usage

wall = Rectangle.new(x: 10, y: 10, width: 10, height: 10)
player = Rectangle.new(x: 0, y: 0, width: 10, height: 10)

puts player.overlaps?(wall)
# => false

# The players moves to inside the rectangle...
player.x = 5
player.y = 5

puts player.overlaps?(wall)
# => true

# The player moves far away from the wall...
player.x = 100
player.y = 100

puts player.overlaps?(wall)
# => false

Parameters:

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)

    If passed anything other than a Vector2 or Rectangle



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'src/ruby/models/rectangle.rb', line 136

def overlaps?(other)
  case other
  when Vector2
    other.x.between?(x, x + width) && other.y.between?(y, y + height)

  when Rectangle
    x < (other.x + other.width) &&
      (x + width) > other.x &&
      y < (other.y + other.height) &&
      (y + height) > other.y

  else
    raise ArgumentError, "Must pass in a Vector2 or Rectangle" unless other.is_a?(Vector2)
  end
end

#scissor(&block) ⇒ nil

Scissors within the Rectangle, only drawing that happens within the bounds of the Rectangle will actually be drawn to the screen.

Examples:

Basic usage

portal = Rectangle.new(x: 100, y: 100, width: 50, height: 75, colour: Colour::BLUE)
portal.scissor do
  # Drawing code here
end

Returns:

  • (nil)


77
78
79
80
81
82
# File 'src/ruby/models/rectangle.rb', line 77

def scissor(&block)
  begin_scissoring
  block.call
ensure
  end_scissoring
end

#to_hHash

Return the Rectangle represented by a Hash.

Examples:

Basic usage

rectangle = Rectangle.new(x: 50, y: 60, width: 32, height: 32, colour: Colour[128, 0, 255, 255])

p circle.to_h
# => {
#      x: 50,
#      y: 60,
#      width: 32,
#      height: 32,
#      colour: {
#        red: 128,
#        green: 0,
#        blue: 255,
#        alpha: 255,
#      },
#      outline: nil,
#      thickness: 1,
#      roundness: 0,
#      segments: 6,
#    }

Returns:

  • (Hash)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'src/ruby/models/rectangle.rb', line 53

def to_h
  {
    x: x,
    y: y,
    width: width,
    height: height,
    colour: colour.to_h,
    outline: outline.to_h,
    thickness: thickness,
    roundness: roundness,
    segments: segments
  }
end