Class: RubySprites::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/lash-sprites/block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Block.



6
7
8
9
10
11
# File 'lib/lash-sprites/block.rb', line 6

def initialize(x, y, width, height)
  @x = x
  @y = y
  @width = width
  @height = height
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



4
5
6
# File 'lib/lash-sprites/block.rb', line 4

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



4
5
6
# File 'lib/lash-sprites/block.rb', line 4

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



4
5
6
# File 'lib/lash-sprites/block.rb', line 4

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



4
5
6
# File 'lib/lash-sprites/block.rb', line 4

def y
  @y
end

Instance Method Details

#areaObject



29
30
31
32
# File 'lib/lash-sprites/block.rb', line 29

def area
  @area = @width * @height if @area.nil?
  return @area
end

#fits?(img) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/lash-sprites/block.rb', line 13

def fits?(img)
  return img.width <= @width && img.height <= @height
end

#split(img) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lash-sprites/block.rb', line 17

def split(img)
  blocks = []
  if (@width - img.width) * img.height > (@height - img.height) * img.width
    blocks.push Block.new(@x + img.width, @y, @width - img.width, @height) if @width != img.width
    blocks.push Block.new(@x, @y + img.height, img.width, @height - img.height) if @height != img.height
  else
    blocks.push Block.new(@x + img.width, @y, @width - img.width, img.height) if @width != img.width
    blocks.push Block.new(@x, @y + img.height, @width, @height - img.height) if @height != img.height
  end
  return blocks
end