Class: AxialGrid

Inherits:
Object
  • Object
show all
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

SquareGrid

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:



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_heightFloat (readonly)

the half of the height of an hexagon

Returns:

  • (Float)

    the current value of half_height



18
19
20
# File 'lib/hex/axial_grid.rb', line 18

def half_height
  @half_height
end

#half_widthFloat (readonly)

the half of the width of an hexagon

Returns:

  • (Float)

    the current value of half_width



18
19
20
# File 'lib/hex/axial_grid.rb', line 18

def half_width
  @half_width
end

#hex_heightFloat (readonly)

the height of an hexagon

Returns:

  • (Float)

    the current value of hex_height



18
19
20
# File 'lib/hex/axial_grid.rb', line 18

def hex_height
  @hex_height
end

#hex_rayFloat (readonly)

the ray of an hexagon

Returns:

  • (Float)

    the current value of hex_ray



18
19
20
# File 'lib/hex/axial_grid.rb', line 18

def hex_ray
  @hex_ray
end

#hex_widthFloat (readonly)

the width of an hexagon

Returns:

  • (Float)

    the current value of hex_width



18
19
20
# File 'lib/hex/axial_grid.rb', line 18

def hex_width
  @hex_width
end

#quarter_heightFloat (readonly)

the quarter of the height of an hexagon

Returns:

  • (Float)

    the current value of quarter_height



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)

Parameters:

  • q (Integer)

    the q coordinate of the hexagon

  • r (Integer)

    the r coordinate of the hexagon

Returns:

  • (AxialHex)

    the hexagon at the requested position. nil if nothing



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)

Parameters:

  • q (Integer)

    the q coordinate of the hexagon

  • r (Integer)

    the r coordinate of the hexagon

  • color (String) (defaults to: nil)

    a colorstring that can be used by ImageMagic

  • border (Boolean) (defaults to: false)

    is the hex on the border of the screen (not fully draw)

  • data (Unknown) (defaults to: nil)

    some data associated with the hexagone. Everything you want, it is up to you.

Returns:



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

#eachObject

Call the block for each Hex in the grid

Examples:

mygrid.each{ |h| p h }


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

Parameters:

  • h (AxialHex)

    the hexagon you want to get surronding hexes

Returns:

  • (Array<AxialHex>)

    all surrounding hexes



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)

Parameters:

  • hex (AxialHex)

    the hexagon containing the position you want to read

Returns:

  • (AxialHex)

    the hexagon at the requested position. nil if nothing



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

Parameters:

  • hex (AxialHex)

    the hexagon you want to add into the grid

Returns:

  • (AxialHex)

    the hexagon you inserted



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

Parameters:

  • element_to_color_hash (Hash)

    see initialize



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_jsonArray

Return the grid as a json string

Returns:

  • (Array)

    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