rhex 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hex/ascii_to_grid.rb +3 -0
- data/lib/hex/axial.rb +62 -14
- data/lib/hex/cube.rb +38 -20
- data/lib/hex/grid.rb +42 -14
- data/lib/hex/grid_to_pic.rb +10 -3
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4495c4b3d933607b7ea7e0b10bc42a122ff74c0c
|
4
|
+
data.tar.gz: ddd8919e2e5507048aba10dcd760f5bc89e862a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de3022c9dc1cb096435ec471bc0adf61769abe5d2a4fae5f86d787e172f5b8e9ec70896543b38e4d31a559d2eea673d7b2189fa4be662ada04d4b1d102daddfe
|
7
|
+
data.tar.gz: c37e4bff873ef76137a4ee0d31164598af1c1ae2d50d53cc89e6f5c7f0828b31bd8ca305f234610615b0a7232324cecdb48d9f64bc12c1743fa9215aff324362
|
data/lib/hex/ascii_to_grid.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
# This module contains the methods relatives to ascii map reading
|
1
2
|
module AsciiToGrid
|
2
3
|
|
4
|
+
# Read an ascii file and load it into the hexagon grid.
|
5
|
+
# - +file_path+ : is 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
|
3
6
|
def read_ascii_file( file_path )
|
4
7
|
File.open( file_path ) do |file|
|
5
8
|
|
data/lib/hex/axial.rb
CHANGED
@@ -2,15 +2,26 @@
|
|
2
2
|
|
3
3
|
require_relative 'cube'
|
4
4
|
|
5
|
+
# This module contains is the container for all hexagon classes.
|
5
6
|
module Hex
|
7
|
+
|
8
|
+
# This class represents an hexagon stored in axial coordinate system.
|
9
|
+
#
|
10
|
+
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates
|
11
|
+
# to understand what an axial coordinates system is
|
6
12
|
class Axial
|
7
13
|
|
8
|
-
attr_reader :q, :r, :val
|
14
|
+
attr_reader :q, :r, :val #:nodoc:
|
9
15
|
|
10
16
|
# Directions around hex from top left clockwise
|
11
|
-
DIRECTIONS = [ [0,-1], [1,-1], [1,0], [0,1], [-1,+1], [-1,0] ]
|
12
|
-
|
13
|
-
# Create
|
17
|
+
DIRECTIONS = [ [0,-1], [1,-1], [1,0], [0,1], [-1,+1], [-1,0] ] #:nodoc:
|
18
|
+
|
19
|
+
# Create an hexagon object
|
20
|
+
# - +q+ and +r+ are the coordinates in the axial coords system
|
21
|
+
# - +val+ : is a value anything you want.
|
22
|
+
# - +border+ is a boolean and mean that the hex is at the border of the map.
|
23
|
+
#
|
24
|
+
# *Returns* : a new Hex::Axial object.
|
14
25
|
def initialize( q, r, val: nil, border: false )
|
15
26
|
@q = q
|
16
27
|
@r = r
|
@@ -18,27 +29,44 @@ module Hex
|
|
18
29
|
@border = border
|
19
30
|
end
|
20
31
|
|
21
|
-
#
|
22
|
-
def ==(h)
|
32
|
+
# Check the equality between two hexagons.
|
33
|
+
def ==(h) #:nodoc:
|
23
34
|
@q==h.q && @r==h.r
|
24
35
|
end
|
25
36
|
|
26
|
-
# Force the border status of the hex
|
37
|
+
# Force the border status of the hex.
|
38
|
+
#
|
39
|
+
# *Returns* : true.
|
27
40
|
def border!
|
28
41
|
@border = true
|
29
42
|
end
|
30
43
|
|
31
|
-
#
|
44
|
+
# Get the border status of the hex.
|
45
|
+
#
|
46
|
+
# *Returns* : true if the hex is at the border of the grid, false otherwise (only if border have been set. Return false otherwise).
|
32
47
|
def border?
|
33
48
|
!@border.nil?
|
34
49
|
end
|
35
50
|
|
36
|
-
# Transform
|
51
|
+
# Transform an axial represented hexagon object to a cube represented hexagon object.
|
52
|
+
#
|
53
|
+
# *Returns* : a new Hex::Cube object.
|
37
54
|
def to_cube
|
38
55
|
Hex::Cube.new(@q,-@q-@r,@r)
|
39
56
|
end
|
40
57
|
|
41
|
-
# From an array of
|
58
|
+
# From an array of hexagons, get the nearest
|
59
|
+
# - +hex_array+ : and array of Hex::Axial objects
|
60
|
+
#
|
61
|
+
# Example
|
62
|
+
#
|
63
|
+
# hext_to_test = Hex::Axial.new( 5, 5 )
|
64
|
+
# nearest_hex = Hex::Axial.new( 5, 6 )
|
65
|
+
# far_hex = Hex::Axial.new( 20, 20 )
|
66
|
+
#
|
67
|
+
# nearest_hex.nearset_hex( [ hext_to_test, far_hex ] ) #=> #<Hex::Axial @q=5, @r=6>
|
68
|
+
#
|
69
|
+
# *Returns* : the nearset hex as a Hex::Axial object.
|
42
70
|
def nearest_hex( hex_array )
|
43
71
|
nearest_hex = nil
|
44
72
|
current_distance = nil
|
@@ -53,26 +81,46 @@ module Hex
|
|
53
81
|
nearest_hex
|
54
82
|
end
|
55
83
|
|
56
|
-
|
84
|
+
##
|
85
|
+
# Compute the distance (in hex) between two hexagons
|
86
|
+
# - +h+ : an Hex::Axial object
|
87
|
+
#
|
88
|
+
# Example
|
89
|
+
#
|
90
|
+
# h1 = Hex::Axial.new( 5, 5 )
|
91
|
+
# h2 = Hex::Axial.new( 20, 20 )
|
92
|
+
#
|
93
|
+
# h1.distance( h2 ) #=> 30
|
94
|
+
#
|
95
|
+
# Hex::Axial.new( 5, 5 ).distance( Hex::Axial.new( 5, 5 ) ) #=> 0
|
96
|
+
# Hex::Axial.new( 5, 5 ).distance( Hex::Axial.new( 5, 1 ) ) #=> 1
|
97
|
+
#
|
98
|
+
# *Returns* : the distance between the two hexes as an integer.
|
57
99
|
def distance( h )
|
58
100
|
to_cube.distance(h.to_cube)
|
59
101
|
end
|
60
102
|
|
61
103
|
# Get all hexagons surrounding the current hexagon
|
104
|
+
#
|
105
|
+
# *Returns* : an array of Hex::Axial.
|
62
106
|
def get_surrounding_hexs
|
63
107
|
# puts self.inspect, self.q.inspect, self.r.inspect
|
64
108
|
DIRECTIONS.map{ |e| Hex::Axial.new( @q+e[0], @r+e[1] ) }
|
65
109
|
end
|
66
110
|
|
67
111
|
# Check if an hexagon is around another hexagon
|
112
|
+
#
|
113
|
+
# *Returns* : true if the hexagon is adjacent to the other, false otherwise. Note, h.hex_surrounding_hex?( h ) == false
|
68
114
|
def hex_surrounding_hex?(hex)
|
69
115
|
distance(hex)==1
|
70
116
|
end
|
71
117
|
|
72
118
|
# Round an hexagon coordinates (useful after pixel to axial coordinate transformation)
|
73
|
-
#
|
74
|
-
#
|
75
|
-
|
119
|
+
#
|
120
|
+
# *Returns* : an Hex::Axial with coords rounded.
|
121
|
+
def round
|
122
|
+
to_cube.round.to_axial
|
123
|
+
end
|
76
124
|
|
77
125
|
end
|
78
126
|
end
|
data/lib/hex/cube.rb
CHANGED
@@ -1,39 +1,57 @@
|
|
1
1
|
module Hex
|
2
|
+
|
3
|
+
# This class represents an hexagon stored in a cube coordinate system.
|
4
|
+
#
|
5
|
+
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates
|
6
|
+
# to understand what a cube coordinates system is
|
7
|
+
# The cube class is only for computation.
|
8
|
+
# It is not intended to be used directly in your program.
|
2
9
|
class Cube
|
3
10
|
|
4
|
-
attr_reader :x,:y,:z
|
11
|
+
attr_reader :x,:y,:z #:nodoc:
|
5
12
|
|
13
|
+
# Create an hexagon object
|
14
|
+
# - +x+, +y+, +z+ are the coordinates in the axial coords system
|
15
|
+
#
|
16
|
+
# *Returns* : a new Hex::Cube object.
|
6
17
|
def initialize(x,y,z)
|
7
18
|
@x = x
|
8
19
|
@y = y
|
9
20
|
@z = z
|
10
21
|
end
|
11
22
|
|
12
|
-
#
|
23
|
+
# Transform a cube represented hexagon to an Hexagon::Axial represented hexagon
|
24
|
+
#
|
25
|
+
# *Returns* : a new Hex::Axial object.
|
13
26
|
def to_axial
|
14
27
|
Hex::Axial.new(@x,@z)
|
15
28
|
end
|
16
29
|
|
17
|
-
# Round
|
18
|
-
# def round
|
19
|
-
# rx=@x.round(0)
|
20
|
-
# ry=@y.round(0)
|
21
|
-
# rz=@z.round(0)
|
22
|
-
#
|
23
|
-
# x_diff=(rx-@x).abs
|
24
|
-
# y_diff=(ry-@y).abs
|
25
|
-
# z_diff=(rz-@z).abs
|
30
|
+
# Round the float coordinates to integer coordinates.
|
26
31
|
#
|
27
|
-
#
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
# rz = -rx-ry
|
33
|
-
# end
|
34
|
-
# Hex::Cube.new(rx,ry,rz)
|
35
|
-
# end
|
32
|
+
# *Returns* : a new Hex::Cube object.
|
33
|
+
def round
|
34
|
+
rx=@x.round(0)
|
35
|
+
ry=@y.round(0)
|
36
|
+
rz=@z.round(0)
|
36
37
|
|
38
|
+
x_diff=(rx-@x).abs
|
39
|
+
y_diff=(ry-@y).abs
|
40
|
+
z_diff=(rz-@z).abs
|
41
|
+
|
42
|
+
if x_diff > y_diff and x_diff > z_diff
|
43
|
+
rx = -ry-rz
|
44
|
+
elsif y_diff > z_diff
|
45
|
+
ry = -rx-rz
|
46
|
+
else
|
47
|
+
rz = -rx-ry
|
48
|
+
end
|
49
|
+
Hex::Cube.new(rx,ry,rz)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Compute the distance between two hexagons (in hexagons)
|
53
|
+
#
|
54
|
+
# *Returns* : an integer : the distance between hex in hexagons.
|
37
55
|
def distance(h)
|
38
56
|
[(@x - h.x).abs, (@y - h.y).abs, (@z - h.z).abs].max
|
39
57
|
end
|
data/lib/hex/grid.rb
CHANGED
@@ -3,11 +3,30 @@ require_relative 'grid_to_pic'
|
|
3
3
|
require_relative 'ascii_to_grid'
|
4
4
|
|
5
5
|
module Hex
|
6
|
+
|
7
|
+
# This class represents a grid of hexagons stored in an axial coordinate system.
|
8
|
+
#
|
9
|
+
# Please read http://www.redblobgames.com/grids/hexagons/#coordinates to understand what an axial coordinates system is.
|
6
10
|
class Grid
|
7
11
|
|
8
12
|
include GridToPic
|
9
13
|
include AsciiToGrid
|
10
14
|
|
15
|
+
# Create an hexagon object
|
16
|
+
# - +hex_ray+ is the size of an hexagon. Please read : http://www.redblobgames.com/grids/hexagons/#basics for information about the size of an hexagon.
|
17
|
+
# - +element_to_color_hash+ : is a hash that relate val (see Hex::Axial.new) to a color. This is used to dump your grid to a bitmap field.
|
18
|
+
#
|
19
|
+
# Example
|
20
|
+
#
|
21
|
+
# @g = Hex::Grid.new(
|
22
|
+
# element_to_color_hash: {
|
23
|
+
# m: :brown, g: :green, w: :blue
|
24
|
+
# }
|
25
|
+
# )
|
26
|
+
#
|
27
|
+
# Assuming you want all hex with a value of m are drawn in brown,g in green, etc ... (see GridToPic for drawin a grid)
|
28
|
+
#
|
29
|
+
# *Returns* : a new Hex::Grid object.
|
11
30
|
def initialize( hex_ray: 16, element_to_color_hash: {} )
|
12
31
|
@hexes={}
|
13
32
|
@element_to_color_hash = element_to_color_hash
|
@@ -16,39 +35,48 @@ module Hex
|
|
16
35
|
end
|
17
36
|
|
18
37
|
# Create an hexagon at a given position (q, r)
|
19
|
-
#
|
38
|
+
#
|
39
|
+
# You can set a value for the hexagon and set the hex as a border hex or not
|
40
|
+
#
|
41
|
+
# *Returns* : an Hex::Axial object.
|
20
42
|
def cset( q, r, val: nil, border: false )
|
21
43
|
@hexes[ [ q, r ] ] = Hex::Axial.new( q, r, val: val, border: border )
|
22
44
|
end
|
23
45
|
|
24
|
-
#
|
46
|
+
# Same method, but accept an hexagon instead of (q, r) coords
|
47
|
+
#
|
48
|
+
# *Returns* : the created Hex::Axial object.
|
25
49
|
def hset( hex, val: nil, border: false )
|
26
50
|
@hexes[ [ hex.q, hex.r ] ] = Hex::Axial.new( hex.q, hex.r, val: val, border: border )
|
27
51
|
end
|
28
52
|
|
29
53
|
# Get the hexagon at a given position (q, r)
|
54
|
+
#
|
55
|
+
# *Returns* : the created Hex::Axial object.
|
30
56
|
def cget( q, r )
|
31
57
|
@hexes[ [ q, r ] ]
|
32
58
|
end
|
33
59
|
|
34
|
-
#
|
60
|
+
# Same method, but accept an hexagon instead of (q, r) coords
|
61
|
+
#
|
62
|
+
# *Returns* : the created Hex::Axial object.
|
35
63
|
def hget( hex )
|
36
64
|
@hexes[ [ hex.q, hex.r ] ]
|
37
65
|
end
|
38
66
|
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
# Hex::Axial.new(q, r).round
|
49
|
-
# end
|
67
|
+
# Get the hexagon at (x,y) coordinate.
|
68
|
+
#
|
69
|
+
# *Returns* : the Hex::Axial object at x, y pos.
|
70
|
+
def hex_at_xy(x, y)
|
71
|
+
q = (x * Math.sqrt(3)/3.0 - y/3.0) / @hex_ray
|
72
|
+
r = y * 2.0/3.0 / @hex_ray
|
73
|
+
hex = Hex::Axial.new(q, r).round
|
74
|
+
hget( hex )
|
75
|
+
end
|
50
76
|
|
51
77
|
# Give the position of an hexagon object in pixel.
|
78
|
+
#
|
79
|
+
# *Returns* : an array of x, y positions.
|
52
80
|
def to_xy( hex )
|
53
81
|
tmp_q = hex.q - ( hex.r/2.0 ).floor
|
54
82
|
x = @hex_ray * Math.sqrt(3) * ( tmp_q + hex.r/2.0 )
|
data/lib/hex/grid_to_pic.rb
CHANGED
@@ -7,10 +7,17 @@ rescue Gem::LoadError
|
|
7
7
|
puts 'Caution : Rmagick is not installed'
|
8
8
|
end
|
9
9
|
|
10
|
+
# This module contain methods to draw a grid to a picture file.
|
11
|
+
# It is included in Grid.
|
10
12
|
module GridToPic
|
11
13
|
|
12
|
-
attr_reader :hex_height, :hex_width
|
14
|
+
attr_reader :hex_height, :hex_width #:nodoc:
|
13
15
|
|
16
|
+
# Draw a picture of the hexagon grid
|
17
|
+
# - +pic_name+ : the name of the picture file (can be *.bmp, *.png, *.jpg)
|
18
|
+
# - +exit_on_error+ : by default, if you call this method and rmagic is not installed, the program exit with an error. You can disable it and make the program continue.
|
19
|
+
#
|
20
|
+
# *Returns* : true if the file was created successfully, false otherwise.
|
14
21
|
def to_pic( pic_name, exit_on_error = true )
|
15
22
|
unless defined?( Magick::Image ) && defined?( Magick::HatchFill ) && defined?( Magick::Draw )
|
16
23
|
puts 'Rmagick is not installed !!! You can\'t dump hex grid to pic'
|
@@ -31,6 +38,8 @@ module GridToPic
|
|
31
38
|
canvas.write( pic_name )
|
32
39
|
end
|
33
40
|
|
41
|
+
private
|
42
|
+
|
34
43
|
def set_hex_dimensions
|
35
44
|
|
36
45
|
@hex_height = @hex_ray * 2.0
|
@@ -41,8 +50,6 @@ module GridToPic
|
|
41
50
|
|
42
51
|
end
|
43
52
|
|
44
|
-
private
|
45
|
-
|
46
53
|
def get_color( hex )
|
47
54
|
color = @element_to_color_hash[ hex.val ]
|
48
55
|
color ? color : :white
|
metadata
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rhex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.2
|
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: 2015-12-
|
11
|
+
date: 2015-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
|
-
A library
|
15
|
-
|
16
|
-
|
14
|
+
A library providing hexagons management and hexagonal grids for ruby.
|
15
|
+
Misc methods (surrounding hexes, nearest hex, distance between hexes).
|
16
|
+
Can also create an hex grid from an ascii file and dump an hex grid to a picture.
|
17
17
|
email:
|
18
18
|
executables: []
|
19
19
|
extensions: []
|