|
@ -28,19 +28,17 @@ local HEX_DIRECTIONS = {vec2( 1 , 0), |
|
|
vec2(-1 , 1), |
|
|
vec2(-1 , 1), |
|
|
vec2( 0 , 1)} |
|
|
vec2( 0 , 1)} |
|
|
|
|
|
|
|
|
-- return hex vector direction via index |direction|. |
|
|
|
|
|
|
|
|
-- return hex vector direction via integer index |direction|. |
|
|
function hex_direction(direction) |
|
|
function hex_direction(direction) |
|
|
return HEX_DIRECTIONS[direction] |
|
|
|
|
|
|
|
|
return HEX_DIRECTIONS[(6 + (direction % 6)) % 6 + 1] |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
-- return hexagon adjacent to |hex| in |direction| |
|
|
|
|
|
|
|
|
-- return hexagon adjacent to |hex| in integer index |direction|. |
|
|
function hex_neighbour(hex, direction) |
|
|
function hex_neighbour(hex, direction) |
|
|
return hex + HEX_DIRECTION[direction] |
|
|
|
|
|
|
|
|
return hex + HEX_DIRECTIONS[(6 + (direction % 6)) % 6 + 1] |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
-- rounds hexes. without this, pixel_to_hex returns fractional coordinates. |
|
|
-- rounds hexes. without this, pixel_to_hex returns fractional coordinates. |
|
|
-- using single coordinates instead of a vector, because this should only |
|
|
|
|
|
-- ever be called internally. |
|
|
|
|
|
function hex_round(s, t) |
|
|
function hex_round(s, t) |
|
|
local rs = round(s) |
|
|
local rs = round(s) |
|
|
local rt = round(t) |
|
|
local rt = round(t) |
|
@ -65,11 +63,13 @@ end |
|
|
|
|
|
|
|
|
-- forward & inverse matrices used for the flat orientation. |
|
|
-- forward & inverse matrices used for the flat orientation. |
|
|
local FLAT = {M = mat2(3.0/2.0, 0.0, 3.0^0.5/2.0, 3.0^0.5 ), |
|
|
local FLAT = {M = mat2(3.0/2.0, 0.0, 3.0^0.5/2.0, 3.0^0.5 ), |
|
|
W = mat2(2.0/3.0, 0.0, -1.0/3.0 , 3.0^0.5/3.0)} |
|
|
|
|
|
|
|
|
W = mat2(2.0/3.0, 0.0, -1.0/3.0 , 3.0^0.5/3.0), |
|
|
|
|
|
start_angle = 0.0} |
|
|
|
|
|
|
|
|
-- forward & inverse matrices used for the pointy orientation. |
|
|
-- forward & inverse matrices used for the pointy orientation. |
|
|
local POINTY = {M = mat2(3.0^0.5, 3.0^0.5/2.0, 0.0, 3.0/2.0), |
|
|
local POINTY = {M = mat2(3.0^0.5, 3.0^0.5/2.0, 0.0, 3.0/2.0), |
|
|
W = mat2(3.0^0.5/3.0, -1.0/3.0, 0.0, 2.0/3.0)} |
|
|
|
|
|
|
|
|
W = mat2(3.0^0.5/3.0, -1.0/3.0, 0.0, 2.0/3.0), |
|
|
|
|
|
start_angle = 0.5} |
|
|
|
|
|
|
|
|
-- TODO encapsulate hex_to_pixel and pixel_to_hex in layout table. |
|
|
-- TODO encapsulate hex_to_pixel and pixel_to_hex in layout table. |
|
|
-- stores layout information that does not pertain to map shape |
|
|
-- stores layout information that does not pertain to map shape |
|
@ -101,11 +101,46 @@ function pixel_to_hex(pix, layout) |
|
|
return hex_round(s, t) |
|
|
return hex_round(s, t) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
-- TODO test |
|
|
|
|
|
function hex_corner_offset(layout, corner) |
|
|
|
|
|
local angle = 2.0 * math.pi * layout.orientation.start_angle + corner / 6 |
|
|
|
|
|
return vec2(layout.size.x * math.cos(angle), layout.size.y * math.sin(angle)) |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
-- TODO make do stuff |
|
|
|
|
|
function hex_corners(layout, hex) |
|
|
|
|
|
local corners = {} |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
----- [[ MAP STORAGE & RETRIEVAL ]] -------------------------------------------- |
|
|
----- [[ MAP STORAGE & RETRIEVAL ]] -------------------------------------------- |
|
|
--[[ |
|
|
--[[ |
|
|
]] |
|
|
]] |
|
|
-- TODO make all functions work regardless of layout. |
|
|
-- TODO make all functions work regardless of layout. |
|
|
|
|
|
|
|
|
|
|
|
-- returns ordered ring-shaped map of |radius| from |center|. |
|
|
|
|
|
function hex_ring_map(center, radius) |
|
|
|
|
|
local map = {} |
|
|
|
|
|
local walk = center + HEX_DIRECTIONS[6] * radius |
|
|
|
|
|
|
|
|
|
|
|
for i = 1, 6 do |
|
|
|
|
|
for j = 1, radius do |
|
|
|
|
|
table.insert(map, walk) |
|
|
|
|
|
walk = hex_neighbour(walk, i) |
|
|
|
|
|
end |
|
|
|
|
|
end |
|
|
|
|
|
return map |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
-- returns ordered hexagonal map of |radius| rings from |center|. |
|
|
|
|
|
function hex_spiral_map(center, radius) |
|
|
|
|
|
local map = {center} |
|
|
|
|
|
|
|
|
|
|
|
for i = 1, radius do |
|
|
|
|
|
table.append(map, hex_ring_map(center, i)) |
|
|
|
|
|
end |
|
|
|
|
|
return map |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
-- returns unordered parallelogram-shaped map of |width| and |height|. |
|
|
-- returns unordered parallelogram-shaped map of |width| and |height|. |
|
|
function hex_parallelogram_map(width, height) |
|
|
function hex_parallelogram_map(width, height) |
|
|
local map = {} |
|
|
local map = {} |
|
@ -163,7 +198,7 @@ function hex_rectangular_map(width, height) |
|
|
|
|
|
|
|
|
for s = 0, width do |
|
|
for s = 0, width do |
|
|
for t = 0, height do |
|
|
for t = 0, height do |
|
|
map[vec2(s, t)] = true |
|
|
|
|
|
|
|
|
map[vec2(s, t - math.floor(s/2))] = true |
|
|
end |
|
|
end |
|
|
end |
|
|
end |
|
|
return map |
|
|
return map |
|
|