|
|
@ -17,8 +17,9 @@ |
|
|
|
+support for arbitrary maps with gaps via hashmaps-like storage |
|
|
|
+support for simple irregular hexagons (horizontal and vertical stretching). |
|
|
|
|
|
|
|
classes are used sparsely. maps implement a few constructors, for storing |
|
|
|
your maps elsewhere. |
|
|
|
classes are used sparsely. maps implement a few constructors for storing |
|
|
|
your maps elsewhere, and should be the only field that is necessarily |
|
|
|
visible outside the library. |
|
|
|
|
|
|
|
-- RESOURCES USED TO DEVELOP THIS LIBRARY |
|
|
|
https://redblobgames.com/grid/hexagons - simply amazing. |
|
|
@ -29,12 +30,12 @@ |
|
|
|
|
|
|
|
----- [[ GENERALLY USEFUL FUNCTIONS ]] ----------------------------------------- |
|
|
|
|
|
|
|
-- just incase you don't already have a rounding function. |
|
|
|
-- rounds numbers. would've been cool to have math.round in lua. |
|
|
|
local function round(n) |
|
|
|
return n % 1 >= 0.5 and math.ceil(n) or math.floor(n) |
|
|
|
end |
|
|
|
|
|
|
|
---- [[ HEX CONSTANTS ]] ------------------------------------------------------- |
|
|
|
----- [[ HEX CONSTANTS ]] ------------------------------------------------------ |
|
|
|
|
|
|
|
-- all possible vector directions from a given hex by edge |
|
|
|
local HEX_DIRECTIONS = {vec2( 1 , 0), |
|
|
@ -44,7 +45,7 @@ local HEX_DIRECTIONS = {vec2( 1 , 0), |
|
|
|
vec2(-1 , 1), |
|
|
|
vec2( 0 , 1)} |
|
|
|
|
|
|
|
-- HEX UTILITY FUNCTIONS ------------------------------------------------------- |
|
|
|
----- [[ HEX UTILITY FUNCTIONS ]] ---------------------------------------------- |
|
|
|
|
|
|
|
function hex_equals(a, b) |
|
|
|
return a.s == b.s and a.t == b.t |
|
|
@ -59,13 +60,13 @@ function hex_distance(a, b) |
|
|
|
end |
|
|
|
|
|
|
|
function hex_round(s, t) |
|
|
|
rs = round(s) |
|
|
|
rt = round(t) |
|
|
|
rz = round(-s - t) |
|
|
|
local rs = round(s) |
|
|
|
local rt = round(t) |
|
|
|
local rz = round(-s - t) |
|
|
|
|
|
|
|
sdelta = math.abs(rs - s) |
|
|
|
tdelta = math.abs(rt - t) |
|
|
|
zdelta = math.abs(rz - (-s - t)) |
|
|
|
local sdelta = math.abs(rs - s) |
|
|
|
local tdelta = math.abs(rt - t) |
|
|
|
local zdelta = math.abs(rz - (-s - t)) |
|
|
|
|
|
|
|
if sdelta > tdelta and sdelta > zdelta then |
|
|
|
rs = -rt - rz |
|
|
@ -88,31 +89,31 @@ local FLAT = {3.0/2.0, 0.0, 3.0^0.5/2.0, 3.0^0.5, |
|
|
|
local POINTY = {3.0^0.5, 3.0^0.5/2.0, 0.0, 3.0/2.0, |
|
|
|
3.0^0.5/3.0, -1.0/3.0, 0.0, 2.0/3.0} |
|
|
|
|
|
|
|
-- layout. |
|
|
|
-- stores layout information that does not pertain to map shape |
|
|
|
function layout_init(origin, size, orientation) |
|
|
|
return {origin = origin or vec2(0), |
|
|
|
size = size or vec2(11), |
|
|
|
return {origin = origin or vec2(0), |
|
|
|
size = size or vec2(11), |
|
|
|
orientation = orientation or FLAT} |
|
|
|
end |
|
|
|
|
|
|
|
-- hex to screen |
|
|
|
function hex_to_pixel(hex, layout) |
|
|
|
M = layout.orientation |
|
|
|
local M = layout.orientation |
|
|
|
|
|
|
|
x = (M[1] * hex.s + M[2] * hex.t) * layout.size.x |
|
|
|
y = (M[3] * hex.s + M[4] * hex.t) * layout.size.y |
|
|
|
local x = (M[1] * hex.s + M[2] * hex.t) * layout.size.x |
|
|
|
local y = (M[3] * hex.s + M[4] * hex.t) * layout.size.y |
|
|
|
|
|
|
|
return vec2(x + layout.origin.x, y + layout.origin.y) |
|
|
|
end |
|
|
|
|
|
|
|
-- screen to hex |
|
|
|
function pixel_to_hex(pix, layout) |
|
|
|
M = layout.orientation |
|
|
|
local M = layout.orientation |
|
|
|
|
|
|
|
pix = (pix - layout.origin) / layout.size |
|
|
|
local pix = (pix - layout.origin) / layout.size |
|
|
|
|
|
|
|
s = M[5] * pix.x + M[6] * pix.y |
|
|
|
t = M[7] * pix.x + M[8] * pix.y |
|
|
|
local s = M[5] * pix.x + M[6] * pix.y |
|
|
|
local t = M[7] * pix.x + M[8] * pix.y |
|
|
|
|
|
|
|
return hex_round(s, t) |
|
|
|
end |
|
|
@ -128,13 +129,22 @@ end |
|
|
|
rectmap_init - rectangular map |
|
|
|
|
|
|
|
calling .retrieve(pix) on your map will get the hexagon at that pixel. |
|
|
|
calling .store(hex) on your map will store the hex as pixel coords. |
|
|
|
calling .store(hex) on your map will store that hex as pixel coords. |
|
|
|
|
|
|
|
maps store coordinates like this: |
|
|
|
|
|
|
|
map[hex] = hex_to_pixel(hex) |
|
|
|
|
|
|
|
this means you should be able to get all the information you need about |
|
|
|
various coordinates completely within the map 'class', without calling |
|
|
|
any internal functions. indeed, *map_init, map.retrieve, and map.store |
|
|
|
is all you need. |
|
|
|
]] |
|
|
|
|
|
|
|
-- returns parallelogram-shaped map. |
|
|
|
function grammap_init(layout, width, height) |
|
|
|
map = {} |
|
|
|
mt = {__index={layout=layout, |
|
|
|
local map = {} |
|
|
|
local mt = {__index={layout=layout, |
|
|
|
|
|
|
|
-- get hex in map from pixel coordinate |
|
|
|
retrieve=function(pix) |
|
|
@ -159,8 +169,8 @@ end |
|
|
|
|
|
|
|
-- returns triangular map. |
|
|
|
function trimap_init(layout, size) |
|
|
|
map = {} |
|
|
|
mt = {__index={layout=layout, |
|
|
|
local map = {} |
|
|
|
local mt = {__index={layout=layout, |
|
|
|
|
|
|
|
-- get hex in map from pixel coordinate |
|
|
|
retrieve=function(pix) |
|
|
@ -173,6 +183,8 @@ function trimap_init(layout, size) |
|
|
|
end |
|
|
|
}} |
|
|
|
|
|
|
|
setmetatable(map, mt) |
|
|
|
|
|
|
|
for s = 0, size do |
|
|
|
for t = size - s, size do |
|
|
|
map.store(vec2(s, t)) |
|
|
@ -183,8 +195,8 @@ end |
|
|
|
|
|
|
|
-- returns hexagonal map. length of map is radius * 2 + 1 |
|
|
|
function hexmap_init(layout, radius) |
|
|
|
map = {} |
|
|
|
mt = {__index={layout=layout, |
|
|
|
local map = {} |
|
|
|
local mt = {__index={layout=layout, |
|
|
|
|
|
|
|
-- get hex in map from pixel coordinate |
|
|
|
retrieve=function(pix) |
|
|
@ -200,8 +212,8 @@ function hexmap_init(layout, radius) |
|
|
|
setmetatable(map, mt) |
|
|
|
|
|
|
|
for s = -radius, radius do |
|
|
|
t1 = math.max(-radius, -s - radius) |
|
|
|
t2 = math.min(radius, -s + radius) |
|
|
|
local t1 = math.max(-radius, -s - radius) |
|
|
|
local t2 = math.min(radius, -s + radius) |
|
|
|
|
|
|
|
for t = t1, t2 do |
|
|
|
table.insert(map, hex_to_pixel(vec2(s, t), layout)) |
|
|
@ -212,8 +224,8 @@ end |
|
|
|
|
|
|
|
-- returns rectangular map. |
|
|
|
function rectmap_init(layout, width, height) |
|
|
|
map = {} |
|
|
|
mt = {__index={layout=layout, |
|
|
|
local map = {} |
|
|
|
local mt = {__index={layout=layout, |
|
|
|
|
|
|
|
-- get hex in map from pixel coordinate |
|
|
|
retrieve=function(pix) |
|
|
|