Class: Punk::Hair::Maker

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

Overview

rename to Factory or Studio or Generator or .. such - why? why not?

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheet, colors:) ⇒ Maker

Returns a new instance of Maker.



76
77
78
79
# File 'lib/punkmaker/hair.rb', line 76

def initialize( sheet, colors: ) 
   @sheet = sheet
   @colors = colors
end

Instance Attribute Details

#colorsObject (readonly)

Returns the value of attribute colors.



74
75
76
# File 'lib/punkmaker/hair.rb', line 74

def colors
  @colors
end

#sheetObject (readonly)

Returns the value of attribute sheet.



74
75
76
# File 'lib/punkmaker/hair.rb', line 74

def sheet
  @sheet
end

Class Method Details

.read(image_path, meta_path, width:, height:, colors:) ⇒ Object



67
68
69
70
71
72
# File 'lib/punkmaker/hair.rb', line 67

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

Instance Method Details

#find_by(name:) ⇒ Object



80
# File 'lib/punkmaker/hair.rb', line 80

def find_by( name: )  @sheet.find_by( name: name ); end

#make(style, color: 'black') ⇒ Object

change name to colorize - why? why not?



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/punkmaker/hair.rb', line 83

def make( style, color: 'black' )   ## change name to colorize - why? why not?

    ## pass-through  shavedhead (no colors - alpha only) - why? why not?

    hair = @sheet.find_by( name: style )
    if hair.nil?
      puts "!! ERROR - unknow hair style >#{style}<; sorry"
      exit 1
    end
     
    color = parse_hair_color( color )

    color_map = Hair.derive_color_map( color )
    hair.change_colors( color_map )
end

#parse_hair_color(color) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/punkmaker/hair.rb', line 97

def parse_hair_color( color )
  if color.is_a?( String ) 
    @colors[ color ] || Color.from_hex( color )  
  else ## assume color is integer - assert - why? why not?

    color
  end      
end