1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# frozen_string_literal: true
# A Tileset :) TODO
class Tileset
attr_reader :tileset,
:columns, :rows,
:width, :height
def initialize(directory, width, height)
puts directory
@width = width
@height = height
if directory[-1] == '/'
directory = "#{directory}*"
elsif directory[-1] != '*'
directory = "#{directory}/*"
end
puts directory
images = Dir[directory].sort
factors = divisors_of(images.length)
puts factors
@columns = factors[factors.length / 2]
@rows = factors[(factors.length / 2) - 1]
@tileset = Array.new(@columns, nil) { Array.new(@rows, nil) }
images.each_with_index do |image, index|
x = index % @columns
puts "Image #{index} is #{image}"
#@tileset[x] = [] if @tileset[x].nil?
@tileset[x][((index - x) / @columns)] = image
#puts @tileset[x][((index - x) / 23)]
end
end
def create_image(column:, row:, x: 0, y: 0)
Image.new(tileset[column][row],
width: width,
height: height,
x: x,
y: y)
end
private
def divisors_of(num)
(1..num).select { |n| (num % n).zero? }
end
end
|