Class: CubeHex

Inherits:
BaseHex show all
Defined in:
lib/hex/cube_hex.rb

Overview

This class represents an hexagon stored in a cube coordinate system.

Please read www.redblobgames.com/grids/hexagons/#coordinates to understand what a cube coordinates system is The cube class is only for computation. It is not intended to be used directly in your program.

Instance Attribute Summary collapse

Attributes inherited from BaseHex

#border, #color, #data, #is, #the, #your

Instance Method Summary collapse

Constructor Details

#initialize(x, y, z, color: nil, border: nil, data: nil) ⇒ CubeHex

Create an hexagon object



24
25
26
27
28
29
30
# File 'lib/hex/cube_hex.rb', line 24

def initialize( x, y, z, color: nil, border: nil, data: nil )
  @x = x
  @y = y
  @z = z

  super( color, border, data )
end

Instance Attribute Details

#xInteger (readonly)

the x coordinate of the cube representation of the hexagon



14
15
16
# File 'lib/hex/cube_hex.rb', line 14

def x
  @x
end

#yInteger (readonly)

the y coordinate of the cube representation of the hexagon



14
15
16
# File 'lib/hex/cube_hex.rb', line 14

def y
  @y
end

#zInteger (readonly)

the z coordinate of the cube representation of the hexagon



14
15
16
# File 'lib/hex/cube_hex.rb', line 14

def z
  @z
end

Instance Method Details

#distance(h) ⇒ Ingteger

Compute the distance between two hexagons (in hexagons)



67
68
69
# File 'lib/hex/cube_hex.rb', line 67

def distance(h)
  [(@x - h.x).abs, (@y - h.y).abs, (@z - h.z).abs].max
end

#roundAxialCube

Round the float coordinates to integer coordinates



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hex/cube_hex.rb', line 44

def round
  rx=@x.round(0)
  ry=@y.round(0)
  rz=@z.round(0)

  x_diff=(rx-@x).abs
  y_diff=(ry-@y).abs
  z_diff=(rz-@z).abs

  if x_diff > y_diff and x_diff > z_diff
    rx = -ry-rz
  elsif y_diff > z_diff
    ry = -rx-rz
  else
    rz = -rx-ry
  end
  CubeHex.new(rx, ry, rz, color: @color, border: @border, data: @data)
end

#to_axialAxialHex

Transform a cube represented hexagon to an Hexagon::Axial represented hexagon



36
37
38
# File 'lib/hex/cube_hex.rb', line 36

def to_axial
  AxialHex.new(@x, @z, color: @color, border: @border, data: @data)
end