Class: AxialGrid
- Inherits:
-
Object
- Object
- AxialGrid
- Includes:
- AsciiToGridFlat
- Defined in:
- lib/hex/axial_grid.rb
Overview
This class represents a grid of hexagons stored in an axial coordinate system.
Please read www.redblobgames.com/grids/hexagons/#coordinates to understand what an axial coordinates system is.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#half_height ⇒ Float
readonly
the half of the height of an hexagon.
-
#half_width ⇒ Float
readonly
the half of the width of an hexagon.
-
#hex_height ⇒ Float
readonly
the height of an hexagon.
-
#hex_ray ⇒ Float
readonly
the ray of an hexagon.
-
#hex_width ⇒ Float
readonly
the width of an hexagon.
-
#quarter_height ⇒ Float
readonly
the quarter of the height of an hexagon.
Instance Method Summary collapse
-
#cget(q, r) ⇒ AxialHex
Get the hexagon at a given position (q, r).
-
#cset(q, r, color: nil, border: false, data: nil) ⇒ AxialHex
Create an hexagon at a given position (q, r).
-
#each ⇒ Object
Call the block for each Hex in the grid.
-
#h_surrounding_hexes(h) ⇒ Array<AxialHex>
Return all surrounding hexes from grid.
-
#hget(hex) ⇒ AxialHex
Get the hexagon at a given position (q, r).
-
#hset(hex) ⇒ AxialHex
Insert an hexagon into the grid.
-
#initialize(hex_ray: 16, element_to_color_hash: {}) ⇒ AxialGrid
constructor
Create an axial hexagon grid.
-
#set_element_to_color_hash(element_to_color_hash) ⇒ Object
Set the hex color to color conversion hash.
-
#to_json ⇒ Array
Return the grid as a json string.
Methods included from AsciiToGridFlat
#read_ascii_file_flat_topped_odd, #write_ascii_file_flat_topped_odd
Constructor Details
#initialize(hex_ray: 16, element_to_color_hash: {}) ⇒ AxialGrid
Create an axial hexagon grid
28 29 30 31 32 |
# File 'lib/hex/axial_grid.rb', line 28 def initialize( hex_ray: 16, element_to_color_hash: {} ) @hexes={} @element_to_color_hash = element_to_color_hash @hex_ray = hex_ray end |
Instance Attribute Details
#half_height ⇒ Float (readonly)
the half of the height of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def half_height @half_height end |
#half_width ⇒ Float (readonly)
the half of the width of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def half_width @half_width end |
#hex_height ⇒ Float (readonly)
the height of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def hex_height @hex_height end |
#hex_ray ⇒ Float (readonly)
the ray of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def hex_ray @hex_ray end |
#hex_width ⇒ Float (readonly)
the width of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def hex_width @hex_width end |
#quarter_height ⇒ Float (readonly)
the quarter of the height of an hexagon
18 19 20 |
# File 'lib/hex/axial_grid.rb', line 18 def quarter_height @quarter_height end |
Instance Method Details
#cget(q, r) ⇒ AxialHex
Get the hexagon at a given position (q, r)
73 74 75 |
# File 'lib/hex/axial_grid.rb', line 73 def cget( q, r ) @hexes[ [ q, r ] ] end |
#cset(q, r, color: nil, border: false, data: nil) ⇒ AxialHex
Create an hexagon at a given position (q, r)
52 53 54 |
# File 'lib/hex/axial_grid.rb', line 52 def cset( q, r, color: nil, border: false, data: nil ) @hexes[ [ q, r ] ] = AxialHex.new( q, r, color: color, border: border, data: data ) end |
#each ⇒ Object
Call the block for each Hex in the grid
92 93 94 |
# File 'lib/hex/axial_grid.rb', line 92 def each @hexes.sort.each{ |h| yield h[1] } end |
#h_surrounding_hexes(h) ⇒ Array<AxialHex>
Return all surrounding hexes from grid
101 102 103 |
# File 'lib/hex/axial_grid.rb', line 101 def h_surrounding_hexes( h ) h.surrounding_hexes.map{ |sh| hget( sh ) }.compact end |
#hget(hex) ⇒ AxialHex
Get the hexagon at a given position (q, r)
83 84 85 |
# File 'lib/hex/axial_grid.rb', line 83 def hget( hex ) @hexes[ [ hex.q, hex.r ] ] end |
#hset(hex) ⇒ AxialHex
Insert an hexagon into the grid
62 63 64 |
# File 'lib/hex/axial_grid.rb', line 62 def hset( hex ) @hexes[ [ hex.q, hex.r ] ] = hex end |
#set_element_to_color_hash(element_to_color_hash) ⇒ Object
Set the hex color to color conversion hash
38 39 40 |
# File 'lib/hex/axial_grid.rb', line 38 def set_element_to_color_hash( element_to_color_hash ) @element_to_color_hash = element_to_color_hash end |
#to_json ⇒ Array
Return the grid as a json string
108 109 110 111 |
# File 'lib/hex/axial_grid.rb', line 108 def to_json a = @hexes.map{ |e| { q: e[0][0], r: e[0][1], c: e[1].color, b: e[1].border } } a.to_json end |