Class: Pixelart::GeneratorEx

Inherits:
Object
  • Object
show all
Defined in:
lib/punks/pixelart/generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheet, image_class:) ⇒ GeneratorEx

Returns a new instance of GeneratorEx.



29
30
31
32
33
34
# File 'lib/punks/pixelart/generator.rb', line 29

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

  puts "  [punkfactory] 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: Image) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/punks/pixelart/generator.rb', line 5

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

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

.use(sheet, image_class: Image) ⇒ Object

check - allow more sheets - why? why not?



17
18
19
# File 'lib/punks/pixelart/generator.rb', line 17

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

Instance Method Details

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/punks/pixelart/generator.rb', line 109

def generate_image( *values, style: nil, patch: nil )
  ## check: rename patch to more/extras/foreign or ... - why? why not?

   recs = to_recs( *values, style: style, patch: patch )

   punk = @image_class.new( @sheet.image.tile_width,
                            @sheet.image.tile_height  )

   recs.each do |rec|
     ## note: quick hack - allow "inline" raw images for now - why? why not?
     ##         pass through as-is
     img = if rec.is_a?( Image )
             rec
           else
             @sheet.image[ rec.id ]
           end
     punk.compose!( img )
   end

   punk
end

#normalize_gender(str) ⇒ Object



24
# File 'lib/punks/pixelart/generator.rb', line 24

def normalize_gender( str )  SpritesheetEx.normalize_gender( str ); end

#normalize_key(str) ⇒ Object



23
# File 'lib/punks/pixelart/generator.rb', line 23

def normalize_key( str )     SpritesheetEx.normalize_key( str ); end

#normalize_name(str) ⇒ Object



26
# File 'lib/punks/pixelart/generator.rb', line 26

def normalize_name( str )    SpritesheetEx.normalize_name( str ); end

#normalize_size(str) ⇒ Object



25
# File 'lib/punks/pixelart/generator.rb', line 25

def normalize_size( str )    SpritesheetEx.normalize_size( str ); end

#spritesheetObject Also known as: sheet



36
# File 'lib/punks/pixelart/generator.rb', line 36

def spritesheet() @sheet; end

#to_recs(*values, style: nil, patch: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/punks/pixelart/generator.rb', line 40

def to_recs( *values, style: nil, patch: nil )

     recs = []

     archetype_name  = values[0]

     if archetype_name.is_a?( Image )
        archetype = archetype_name
        recs << archetype
          ### for now assume (support only)
          ##    large & male (l/m) for "inline/patch" archetypes - why? why not?
          ##    change male to unisex - why? why not?  (note: for now unisex is not doing a backup lookup using male/female)
          attribute_gender = 'm'
          attribute_size   = 'l'
     elsif patch && img=patch[ normalize_key(archetype_name) ]
        archetype = img
        recs << archetype
        if archetype_name.downcase.index( 'female' )
         ## note: attribute lookup requires gender from archetype!!!!
         ## quick & dirty hack for now
         ##    if name incl. female (automagically) switch to f(emale)/s(mall)
          attribute_gender = 'f'
          attribute_size   = 's'
        else
          ##    large & male (l/m) for "inline/patch" archetypes - why? why not?
          ##    change male to unisex - why? why not?  (note: for now unisex is not doing a backup lookup using male/female)
          attribute_gender = 'm'
          attribute_size   = 'l'
        end
     else ## assume it's a string
       ### todo/fix:  check for nil/not found!!!!
       ## todo/check/fix:  assert meta record returned is archetype NOT attribute!!!!
       archetype  = @sheet.find_meta_by( name: archetype_name )
       if archetype.nil?
         puts "!! ERROR -  archetype >#{archetype}< not found; sorry"
         exit 1
       end
       recs << archetype
       attribute_gender = archetype.gender
       attribute_size   = archetype.size
     end


     attribute_names  = values[1..-1]
     attribute_names.each do |attribute_name|
        ## note: quick hack - allow "inline" raw images for now - why? why not?
        ##         pass through as-is
       if attribute_name.is_a?( Image )
         recs << attribute_name
       elsif patch && img=patch[ normalize_key(attribute_name) ]
         recs << img
       else
         rec = @sheet.find_meta_by( name: attribute_name,
                                    gender: attribute_gender,
                                    size:   attribute_size,
                                    style:  style )
         if rec.nil?
           puts "!! ERROR - attribute >#{attribute_name}< for (#{attribute_gender}+#{attribute_size}) not found; sorry"
           exit 1
         end

         recs << rec
        end
     end

     recs
end