Module: AsciiToGridFlat
- Included in:
- AxialGrid
- Defined in:
- lib/hex/modules/ascii_to_grid_flat.rb
Overview
This module contains the methods relatives to ascii map reading
Instance Method Summary collapse
-
#read_ascii_file_flat_topped_odd(file_path) ⇒ Object
Read an ascii file and load it into the hexagon grid.
-
#write_ascii_file_flat_topped_odd(file_path) ⇒ Object
Write an ascii file representing an axial hex grid as an flat topped (odd-q) hex representation.
Instance Method Details
#read_ascii_file_flat_topped_odd(file_path) ⇒ Object
Read an ascii file and load it into the hexagon grid. The input grid is supposed to be odd flat topped (odd-q) and will be stored into an axial representation.
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 |
# File 'lib/hex/modules/ascii_to_grid_flat.rb', line 7 def read_ascii_file_flat_topped_odd( file_path ) File.open( file_path ) do |file| odd = 0 base_q = 0 line_q = 0 file.each_line do |line| elements = line.split elements.each_with_index do |element, index| # puts "element = %c, index = %d || q = %d, r = %d" % [element, index, index*2 + odd, base_q - index] # cset( index*2 + odd, base_q - index, color: element.to_sym, border: nil ) q = index*2 + odd r = base_q - index @hexes[ [ q, r ] ] = AxialHex.new( q, r, color: element.to_sym ) end odd = ( odd.odd? ? 0 : 1 ) line_q += 1 if line_q >= 2 line_q = 0 base_q += 1 end end end end |
#write_ascii_file_flat_topped_odd(file_path) ⇒ Object
Write an ascii file representing an axial hex grid as an flat topped (odd-q) hex representation.
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 |
# File 'lib/hex/modules/ascii_to_grid_flat.rb', line 40 def write_ascii_file_flat_topped_odd( file_path ) File.open( file_path, 'w' ) do |file| base_r = 0 base_q = 0 line_q = 0 line = [] loop do r = base_r q = base_q odd = ( base_q == 0 ? '' : ' ' ) while( ( hex = cget( q, r ) ) ) do line << hex.color q += 2 r -= 1 end break if line.empty? file.puts( odd + line.join( ' ' ) ) base_q = ( base_q == 0 ? 1 : 0 ) line_q += 1 if line_q >= 2 line_q = 0 base_r += 1 end line = [] end end end |