Module: AsciiToGrid

Included in:
SquareGrid
Defined in:
lib/hex/modules/ascii_to_grid.rb

Overview

This module contains the methods relatives to ascii map reading

Instance Method Summary collapse

Instance Method Details

#read_ascii_file(file_path) ⇒ Object

Read an ascii file and load it into the hexagon grid.

Parameters:

See Also:



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
35
36
37
38
# File 'lib/hex/modules/ascii_to_grid.rb', line 10

def read_ascii_file( file_path )
  File.open( file_path ) do |file|

    r = max_r = 0

    file.each_line do |line|
      elements = line.split
      q = 0
      elements.each do |element|
        # puts "r = #{r} q = #{q}, test = #{( r == 0 || q == 0 )}"
        border = true if ( r == 0 || q == 0 )
        # shifted_q = q - ( r/2 )
        shifted_q = q
        cset( shifted_q, r, color: element.to_sym, border: border )
        q += 1
      end
      r += 1
      max_r = [ max_r, r ].max
    end

    0.upto( max_r - 1 ).each do |row|
      maxq = @hexes.map{ |key, e| e.q if e.r == row }.compact.max
      hex = cget( maxq, row )
      hex.border = true if hex
    end

    @hexes.each{ |key, e| e.border = true if e.r == ( max_r - 1 ) }
  end
end