Class: Mooncats::Design

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Design

Returns a new instance of Design.



64
65
66
# File 'lib/mooncats/design.rb', line 64

def initialize( data )
 @data = data
end

Class Method Details

.find(num) ⇒ Object

pass in design index number (0 to 127)



6
7
8
9
10
11
12
13
14
15
# File 'lib/mooncats/design.rb', line 6

def self.find( num )   ## pass in design index number (0 to 127)
  ## todo: add cache (memoize) - why? why not?
  design = parse( DESIGNS[ num ] )

  puts "    design ##{num} (#{design.width}x#{design.height})"
  ## pp design.data
  ## puts

  design
end

.parse(str) ⇒ Object



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
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mooncats/design.rb', line 23

def self.parse( str )
  ## support original "classic" compact single-line format
  ##   e.g. 00011111100000000.01113333310000000.13533333331110000....
  ##  note: this format needs to get rotated by 90 degree!!!
  if str.start_with?( /[0-5]+\.[0-5]+\.[0-5]+/ )  ## quick (and dirty) heuristic check
    data = str.split('.')

    ## note: map colors encoded as a string to an array of integers - why? why not?
    ##  e.g. "00011111133344411"
    ##          =>
    ##       [0,0,0,1,1,1,1,1,1,3,3,3,4,4,4,1,1]
    data = data.map do |row|
             row.chars.map do |color|
                color.to_i
             end
           end
    data = data.transpose   ## note: rotate by 90 degree!!!!!
  else  ## assume "modern" pixelart format
     ## todo/check: delegate to pixelart parse or such - why? why not?

     data = []
     str.each_line do |line|
       line = line.strip
       next if line.empty?               ## skipping empty line in pixel art source
       next if line.start_with?( '#' )   ## skipping comment line in pixel art source

       ## note: allow multiple spaces or tabs to separate pixel codes
       data << line.split( /[ \t]+/)
     end
     ## todo/check: change to use strings (instead of nummbers) in the future?
     ##                 why? why not?  stay "compatible" to pixel art format/machinery?
     data = data.map do |row|
       row.map do |color|
          color.to_i
       end
     end
  end
  new( data )
end

.read(path) ⇒ Object



18
19
20
21
# File 'lib/mooncats/design.rb', line 18

def self.read( path )
  text = File.open( path, 'r:utf-8') { |f| f.read }
  parse( text )
end

Instance Method Details

#each(&blk) ⇒ Object



78
79
80
# File 'lib/mooncats/design.rb', line 78

def each( &blk )
  @data.each { |row| blk.call( row ) }
end

#each_with_index(&blk) ⇒ Object



75
76
77
# File 'lib/mooncats/design.rb', line 75

def each_with_index( &blk )
  @data.each_with_index { |row, y| blk.call( row, y ) }
end

#heightObject



72
# File 'lib/mooncats/design.rb', line 72

def height() @data.size; end

#to_txtObject



84
85
86
87
88
89
90
91
92
# File 'lib/mooncats/design.rb', line 84

def to_txt
  buf = String.new('')

  @data.each do |row|
    buf << row.join( ' ' )
    buf << "\n"
  end
  buf
end

#widthObject



68
69
70
71
# File 'lib/mooncats/design.rb', line 68

def width
  ## todo/check: use/find max - why? why not? lets you you used "unbalanced" / shortcut lines too
  @data[0].size
end