Class: Artfactory

Inherits:
Object
  • Object
show all
Defined in:
lib/artfactory/artfactory.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheet, image_class:) ⇒ Artfactory

Returns a new instance of Artfactory.



28
29
30
31
32
33
# File 'lib/artfactory/artfactory.rb', line 28

def initialize( sheet, image_class: )
  @sheet       = sheet
  @image_class = image_class

  puts "  [artfactory] using image class >#{@image_class.name}< for #{@sheet.image.tile_width}x#{@sheet.image.tile_height} images"
end

Class Method Details

.read(image_path = "./spritesheet.png", meta_path = "./spritesheet.csv", width: 24, height: 24, image_class: Pixelart::Image) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/artfactory/artfactory.rb', line 10

def self.read( image_path="./spritesheet.png",
               meta_path="./spritesheet.csv",
               width: 24,
               height: 24,
               image_class: Pixelart::Image )

   sheet = Pixelart::Spritesheet.read( image_path,
                                       meta_path,
                                       width: width, height: height )
   new( sheet, image_class: image_class )
end

.use(sheet, image_class: Pixelart::Image) ⇒ Object

check - allow more sheets - why? why not?



22
23
24
# File 'lib/artfactory/artfactory.rb', line 22

def self.use( sheet, image_class: Pixelart::Image )    ### check - allow more sheets - why? why not?
    new( sheet, image_class: image_class )
end

Instance Method Details

#generate_image(*values, before: nil) ⇒ Object Also known as: generate



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/artfactory/artfactory.rb', line 57

def generate_image( *values, before: nil )
   recs = to_recs( *values )

   ## note: first construct/generate image on transparent background
   ##          add background if present as LAST step
   img = @image_class.new( @sheet.image.tile_width,
                           @sheet.image.tile_height )

   recs.each do |rec|
     ## note: before call(back) MUST change image INPLACE!!!!
     before.call( img, rec )   if before
     img.compose!( @sheet.image[ rec.id ] )
   end

   img
end

#spritesheetObject Also known as: sheet



35
# File 'lib/artfactory/artfactory.rb', line 35

def spritesheet() @sheet; end

#to_recs(*values) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/artfactory/artfactory.rb', line 39

def to_recs( *values )
     recs       = []

     names = values

     names.each do |name|
       rec = @sheet.find_meta_by( name: name )
       if rec.nil?
          puts "!! ERROR - attribute (metadata record) >#{name}< not found; sorry"
          exit 1
       end
       recs << rec
     end

     recs
end