lib/ruby2d/square.rb in ruby2d-0.11.3 vs lib/ruby2d/square.rb in ruby2d-0.12.0
- old
+ new
@@ -1,37 +1,38 @@
+# frozen_string_literal: true
+
# Ruby2D::Square
module Ruby2D
+ # A square
class Square < Rectangle
-
attr_reader :size
- def initialize(opts = {})
- @x = opts[:x] || 0
- @y = opts[:y] || 0
- @z = opts[:z] || 0
- @width = @height = @size = opts[:size] || 100
- self.color = opts[:color] || 'white'
- self.color.opacity = opts[:opacity] if opts[:opacity]
- update_coords(@x, @y, @size, @size)
- add
+ # Create an square
+ # @param [Numeric] x
+ # @param [Numeric] y
+ # @param [Numeric] size is width and height
+ # @param [Numeric] z
+ # @param [String, Array] color A single colour or an array of exactly 4 colours
+ # @param [Numeric] opacity Opacity of the image when rendering
+ # @raise [ArgumentError] if an array of colours does not have 4 entries
+ def initialize(x: 0, y: 0, size: 100, z: 0, color: nil, colour: nil, opacity: nil)
+ @size = size
+ super(x: x, y: y, width: size, height: size, z: z,
+ color: color, colour: colour, opacity: opacity)
end
# Set the size of the square
- def size=(s)
- self.width = self.height = @size = s
+ def size=(size)
+ self.width = self.height = @size = size
end
- def self.draw(opts = {})
- ext_draw([
- opts[:x] , opts[:y] , opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
- opts[:x] + opts[:size], opts[:y] , opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
- opts[:x] + opts[:size], opts[:y] + opts[:size], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3],
- opts[:x] , opts[:y] + opts[:size], opts[:color][3][0], opts[:color][3][1], opts[:color][3][2], opts[:color][3][3]
- ])
+ def self.draw(x:, y:, size:, color:)
+ super(x: x, y: y,
+ width: size, height: size,
+ color: color)
end
# Make the inherited width and height attribute accessors private
private :width=, :height=
-
end
end