rhex 2.0.3 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d90bcb5012b56d62255885e20da656a2b6be751
4
- data.tar.gz: b2e452c8f88b2157229e31d2e9d8cf8ef6c6625e
3
+ metadata.gz: 6c2fae8bfcec85bb6d40cdd93c5b3fb9aa4b0c6f
4
+ data.tar.gz: 941154bdc0bae0a5cd8b57ac08d1afdbb6c211b7
5
5
  SHA512:
6
- metadata.gz: 8f8a15e4917422316690266295573c076ab615c88051af523b5026dc7f0641825e49872966406636f431a0040bcdbba5631c9deb55ff59b64656d82f7288a656
7
- data.tar.gz: cab8437bf7021c059691908bed06d8ecd2d3b812c10cb39bbf98caf0cfb9d2c31c4b72df198e649daab80d72b8b2ec0b2fd24efbfe4e3f9b4399373ff4495513
6
+ metadata.gz: 1d5c99ed7299f9c35f3b896151c6900bbc77bca5cfc9bcf20adfb99b04b44c07439bdb6d9a018eda9278c2e257145e774efadf6d300b93229e66a8115d58e61c
7
+ data.tar.gz: b2bdb4cf22ddaa34757d79644353967032d4eb316b52a1ce600018a9e1fec878d1e839c816a359a6ed786bfd4f6f4e072941db754d88e98026da6ee95952f971
@@ -1,6 +1,6 @@
1
1
  require_relative 'axial_hex'
2
- require_relative 'modules/ascii_to_grid'
3
2
  require_relative 'modules/grid_to_pic'
3
+ require_relative 'modules/ascii_to_grid_flat'
4
4
 
5
5
  # This class represents a grid of hexagons stored in an axial coordinate system.
6
6
  #
@@ -17,6 +17,8 @@ require_relative 'modules/grid_to_pic'
17
17
  #
18
18
  class AxialGrid
19
19
 
20
+ include AsciiToGridFlat
21
+
20
22
  attr_reader :hex_ray, :hex_height, :hex_width, :quarter_height, :half_width
21
23
 
22
24
  # Create an axial hexagon grid
@@ -97,21 +99,15 @@ class AxialGrid
97
99
  #
98
100
  # @return [Array<AxialHex>] all surrounding hexes
99
101
  def h_surrounding_hexes( h )
100
- h.surrounding_hexes.map{ |sh| hget( sh ) }
102
+ h.surrounding_hexes.map{ |sh| hget( sh ) }.compact
101
103
  end
102
104
 
103
- # Return the grid as a hash object
104
- #
105
- # @return [Hash] the grid as a hash object
106
- def to_hash
107
- h = @hexes.clone
108
- # p hash
109
- h.each do |k, v|
110
- # p k
111
- h[ k ] = v.to_hash
112
- end
113
- # p h
114
- h
105
+ # Return the grid as a json string
106
+ #
107
+ # @return [Array] the grid as a json string
108
+ def to_json
109
+ a = @hexes.map{ |e| { q: e[0][0], r: e[0][1], c: e[1].color, b: e[1].border } }
110
+ a.to_json
115
111
  end
116
112
 
117
113
  end
@@ -9,8 +9,8 @@ class BaseHex
9
9
  attr_accessor :color, :border, :data
10
10
 
11
11
  def initialize( color = nil, border = nil, data = nil )
12
- @color = color if color
13
- @border = border if border
12
+ @color = color
13
+ @border = border
14
14
  @data = data
15
15
  end
16
16
  end
@@ -0,0 +1,77 @@
1
+ # This module contains the methods relatives to ascii map reading
2
+ module AsciiToGridFlat
3
+
4
+ # 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.
5
+ #
6
+ # @param file_path [String] the name of the ascii file to read. For how to create this file, please see : https://github.com/czuger/rhex#reading-a-grid-from-an-ascii-file
7
+ def read_ascii_file_flat_topped_odd( file_path )
8
+ File.open( file_path ) do |file|
9
+
10
+ odd = 0
11
+ base_q = 0
12
+ line_q = 0
13
+
14
+ file.each_line do |line|
15
+ elements = line.split
16
+ elements.each_with_index do |element, index|
17
+ # puts "element = %c, index = %d || q = %d, r = %d" % [element, index, index*2 + odd, base_q - index]
18
+ # cset( index*2 + odd, base_q - index, color: element.to_sym, border: nil )
19
+ q = index*2 + odd
20
+ r = base_q - index
21
+ @hexes[ [ q, r ] ] = AxialHex.new( q, r, color: element.to_sym )
22
+ end
23
+
24
+ odd = ( odd.odd? ? 0 : 1 )
25
+
26
+ line_q += 1
27
+ if line_q >= 2
28
+ line_q = 0
29
+ base_q += 1
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+
36
+
37
+ # Write an ascii file representing an axial hex grid as an flat topped (odd-q) hex representation.
38
+ #
39
+ # @param file_path [String] the name of the ascii file to read. For how to create this file, please see : https://github.com/czuger/rhex#reading-a-grid-from-an-ascii-file
40
+ def write_ascii_file_flat_topped_odd( file_path )
41
+ File.open( file_path, 'w' ) do |file|
42
+
43
+ base_r = 0
44
+ base_q = 0
45
+ line_q = 0
46
+ line = []
47
+
48
+
49
+ loop do
50
+ r = base_r
51
+ q = base_q
52
+ odd = ( base_q == 0 ? '' : ' ' )
53
+
54
+ while( ( hex = cget( q, r ) ) ) do
55
+ line << hex.color
56
+ q += 2
57
+ r -= 1
58
+ end
59
+
60
+ break if line.empty?
61
+
62
+ file.puts( odd + line.join( ' ' ) )
63
+
64
+ base_q = ( base_q == 0 ? 1 : 0 )
65
+
66
+ line_q += 1
67
+ if line_q >= 2
68
+ line_q = 0
69
+ base_r += 1
70
+ end
71
+ line = []
72
+ end
73
+
74
+ end
75
+ end
76
+
77
+ end
@@ -1,5 +1,6 @@
1
1
  require_relative 'axial_grid'
2
2
  require_relative 'cube_hex'
3
+ require_relative 'modules/ascii_to_grid'
3
4
 
4
5
  # This class represents a grid of hexagons stored in an axial coordinate system but manage the conversion to a square representation (what finally you want)
5
6
  #
@@ -29,37 +30,37 @@ class SquareGrid < AxialGrid
29
30
  set_hex_dimensions
30
31
  end
31
32
 
32
- # Create an hexagon at a given position (col, row)
33
+ # Create an hexagon at a given position (q, r)
33
34
  #
34
- # @param col [Integer] the col coordinate of the hexagon
35
- # @param row [Integer] the row coordinate of the hexagon
35
+ # @param q [Integer] the col coordinate of the hexagon
36
+ # @param r [Integer] the r coordinate of the hexagon
36
37
  # @param color [String] a colorstring that can be used by ImageMagic
37
38
  # @param border [Boolean] is the hex on the border of the screen (not fully draw)
38
39
  # @param data [Unknown] some data associated with the hexagone. Everything you want, it is up to you
39
40
  #
40
41
  # @return [AxialHex] an hexagon
41
42
  #
42
- def cset( col, row, color: nil, border: false, data: nil )
43
- hset( even_q_to_axial_hex( col, row, color: color, border: border, data: data ) )
43
+ def cset( q, r, color: nil, border: false, data: nil )
44
+ hset( even_q_to_axial_hex( q, r, color: color, border: border, data: data ) )
44
45
  end
45
46
 
46
- # Get the hexagon at a given position (col, row)
47
+ # Get the hexagon at a given position (q, r)
47
48
  #
48
- # @param col [Integer] the col coordinate of the hexagon
49
- # @param row [Integer] the row coordinate of the hexagon
49
+ # @param q [Integer] the col coordinate of the hexagon
50
+ # @param r [Integer] the r coordinate of the hexagon
50
51
  #
51
52
  # @return [AxialHex] the hexagon at the requested position. nil if nothing
52
53
  #
53
- def cget( col, row )
54
- hget( even_q_to_axial_hex( col, row ) )
54
+ def cget( q, r )
55
+ hget( even_q_to_axial_hex( q, r ) )
55
56
  end
56
57
 
57
58
  private
58
59
 
59
- def even_q_to_axial_hex( col, row, color: nil, border: false, data: nil )
60
+ def even_q_to_axial_hex( q, r, color: nil, border: false, data: nil )
60
61
  # convert odd-r offset to cube
61
- x = col - (row - (row&1)) / 2
62
- z = row
62
+ x = q - (r - (r&1)) / 2
63
+ z = r
63
64
  y = -x-z
64
65
 
65
66
  tmp_cube = CubeHex.new( x, y, z, color: color, border: border, data: data )
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhex
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cédric ZUGER
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-05 00:00:00.000000000 Z
11
+ date: 2019-07-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  A library providing hexagons management and hexagonal grids for ruby.
@@ -24,6 +24,7 @@ files:
24
24
  - lib/hex/base_hex.rb
25
25
  - lib/hex/cube_hex.rb
26
26
  - lib/hex/modules/ascii_to_grid.rb
27
+ - lib/hex/modules/ascii_to_grid_flat.rb
27
28
  - lib/hex/modules/grid_to_pic.rb
28
29
  - lib/hex/square_grid.rb
29
30
  - lib/rhex.rb
@@ -37,17 +38,17 @@ require_paths:
37
38
  - lib
38
39
  required_ruby_version: !ruby/object:Gem::Requirement
39
40
  requirements:
40
- - - '>='
41
+ - - ">="
41
42
  - !ruby/object:Gem::Version
42
- version: 2.0.0
43
+ version: 2.3.6
43
44
  required_rubygems_version: !ruby/object:Gem::Requirement
44
45
  requirements:
45
- - - '>='
46
+ - - ">="
46
47
  - !ruby/object:Gem::Version
47
48
  version: '0'
48
49
  requirements: []
49
50
  rubyforge_project:
50
- rubygems_version: 2.4.8
51
+ rubygems_version: 2.6.13
51
52
  signing_key:
52
53
  specification_version: 4
53
54
  summary: Ruby HEXagonal grid