Class: SquareGrid

Inherits:
AxialGrid show all
Includes:
AsciiToGrid, GridToPic
Defined in:
lib/hex/square_grid.rb

Overview

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)

Author:

  • Cédric ZUGER

Instance Attribute Summary

Attributes inherited from AxialGrid

#half_height, #half_width, #hex_height, #hex_ray, #hex_width, #quarter_height

Instance Method Summary collapse

Methods included from GridToPic

#to_pic, #to_rmagick_image, #to_xy

Methods included from AsciiToGrid

#read_ascii_file

Methods inherited from AxialGrid

#each, #h_surrounding_hexes, #hget, #hset, #set_element_to_color_hash, #to_json

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: {}) ⇒ SquareGrid

Create an axial hexagon grid

Examples:

@g = Hex::Grid.new(
  element_to_color_hash: {
    m: :brown, g: :green, w: :blue
  }
)
Assuming you want all hex with a color of m are drawn in brown,g in green, etc ... (see GridToPic for drawing a grid)

Parameters:

  • hex_ray (Integer) (defaults to: 16)

    the size of an hexagon.

  • element_to_color_hash (Hash) (defaults to: {})

    a hash that relate color (see BaseHex::Axial.new) to a color. This is used to dump your grid to a bitmap field



27
28
29
30
31
# File 'lib/hex/square_grid.rb', line 27

def initialize( hex_ray: 16, element_to_color_hash: {} )
  super( hex_ray: hex_ray )
  @element_to_color_hash = element_to_color_hash
  set_hex_dimensions
end

Instance Method Details

#cget(q, r) ⇒ AxialHex

Get the hexagon at a given position (q, r)

Parameters:

  • q (Integer)

    the col coordinate of the hexagon

  • r (Integer)

    the r coordinate of the hexagon

Returns:

  • (AxialHex)

    the hexagon at the requested position. nil if nothing



54
55
56
# File 'lib/hex/square_grid.rb', line 54

def cget( q, r )
  hget( even_q_to_axial_hex( 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 col 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:



43
44
45
# File 'lib/hex/square_grid.rb', line 43

def cset( q, r, color: nil, border: false, data: nil )
  hset( even_q_to_axial_hex( q, r, color: color, border: border, data: data ) )
end