Browse Source

v0.0.0.0.0.1

master
churchianity 6 years ago
parent
commit
12048fa702
  1. 76
      hex.lua
  2. 104
      main.lua

76
hex.lua

@ -17,8 +17,9 @@
+support for arbitrary maps with gaps via hashmaps-like storage +support for arbitrary maps with gaps via hashmaps-like storage
+support for simple irregular hexagons (horizontal and vertical stretching). +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 -- RESOURCES USED TO DEVELOP THIS LIBRARY
https://redblobgames.com/grid/hexagons - simply amazing. https://redblobgames.com/grid/hexagons - simply amazing.
@ -29,12 +30,12 @@
----- [[ GENERALLY USEFUL FUNCTIONS ]] ----------------------------------------- ----- [[ 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) local function round(n)
return n % 1 >= 0.5 and math.ceil(n) or math.floor(n) return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
end end
---- [[ HEX CONSTANTS ]] -------------------------------------------------------
----- [[ HEX CONSTANTS ]] ------------------------------------------------------
-- all possible vector directions from a given hex by edge -- all possible vector directions from a given hex by edge
local HEX_DIRECTIONS = {vec2( 1 , 0), local HEX_DIRECTIONS = {vec2( 1 , 0),
@ -44,7 +45,7 @@ local HEX_DIRECTIONS = {vec2( 1 , 0),
vec2(-1 , 1), vec2(-1 , 1),
vec2( 0 , 1)} vec2( 0 , 1)}
-- HEX UTILITY FUNCTIONS -------------------------------------------------------
----- [[ HEX UTILITY FUNCTIONS ]] ----------------------------------------------
function hex_equals(a, b) function hex_equals(a, b)
return a.s == b.s and a.t == b.t return a.s == b.s and a.t == b.t
@ -59,13 +60,13 @@ function hex_distance(a, b)
end end
function hex_round(s, t) 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 if sdelta > tdelta and sdelta > zdelta then
rs = -rt - rz 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, 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} 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) 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} orientation = orientation or FLAT}
end end
-- hex to screen -- hex to screen
function hex_to_pixel(hex, layout) 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) return vec2(x + layout.origin.x, y + layout.origin.y)
end end
-- screen to hex -- screen to hex
function pixel_to_hex(pix, layout) 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) return hex_round(s, t)
end end
@ -128,13 +129,22 @@ end
rectmap_init - rectangular map rectmap_init - rectangular map
calling .retrieve(pix) on your map will get the hexagon at that pixel. 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. -- returns parallelogram-shaped map.
function grammap_init(layout, width, height) 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 -- get hex in map from pixel coordinate
retrieve=function(pix) retrieve=function(pix)
@ -159,8 +169,8 @@ end
-- returns triangular map. -- returns triangular map.
function trimap_init(layout, size) function trimap_init(layout, size)
map = {}
mt = {__index={layout=layout,
local map = {}
local mt = {__index={layout=layout,
-- get hex in map from pixel coordinate -- get hex in map from pixel coordinate
retrieve=function(pix) retrieve=function(pix)
@ -173,6 +183,8 @@ function trimap_init(layout, size)
end end
}} }}
setmetatable(map, mt)
for s = 0, size do for s = 0, size do
for t = size - s, size do for t = size - s, size do
map.store(vec2(s, t)) map.store(vec2(s, t))
@ -183,8 +195,8 @@ end
-- returns hexagonal map. length of map is radius * 2 + 1 -- returns hexagonal map. length of map is radius * 2 + 1
function hexmap_init(layout, radius) function hexmap_init(layout, radius)
map = {}
mt = {__index={layout=layout,
local map = {}
local mt = {__index={layout=layout,
-- get hex in map from pixel coordinate -- get hex in map from pixel coordinate
retrieve=function(pix) retrieve=function(pix)
@ -200,8 +212,8 @@ function hexmap_init(layout, radius)
setmetatable(map, mt) setmetatable(map, mt)
for s = -radius, radius do 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 for t = t1, t2 do
table.insert(map, hex_to_pixel(vec2(s, t), layout)) table.insert(map, hex_to_pixel(vec2(s, t), layout))
@ -212,8 +224,8 @@ end
-- returns rectangular map. -- returns rectangular map.
function rectmap_init(layout, width, height) 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 -- get hex in map from pixel coordinate
retrieve=function(pix) retrieve=function(pix)

104
main.lua

@ -8,15 +8,15 @@ require "hex"
----- [[ DUMMY FUNCTIONS ]] ---------------------------------------------------- ----- [[ DUMMY FUNCTIONS ]] ----------------------------------------------------
function show_hex_coords() function show_hex_coords()
gui_scene:action(function()
gui_scene:remove("text")
grid:action(function()
grid:remove("text")
mouse_position = vec2(win:mouse_position().x, win:mouse_position().y) mouse_position = vec2(win:mouse_position().x, win:mouse_position().y)
if mouse_position.x < 268 then if mouse_position.x < 268 then
hex = map.retrieve(mouse_position) hex = map.retrieve(mouse_position)
gui_scene:append(am.translate(win.left + 30, win.top - 10)
^ am.text(string.format("%d,%d", hex.s, hex.t)))
grid:append(am.translate(win.left + 30, win.top - 10)
^ am.text(string.format("%d,%d", hex.s, hex.t)))
end end
end) end)
end end
@ -28,6 +28,23 @@ function rcolor()
1) 1)
end end
SPRITES = {"BoulderHills1.png", "BoulderHills2.png", "BoulderHills2.png",
"Brambles1.png", "Brambles2.png", "Brambles3.png", "Brambles4.png",
"BrownHills1.png", "BrownHills2.png", "BrownHills3.png",
"Grass1.png", "Grass2.png", "Grass3.png", "Grass4.png", "Grass5.png", "Hills1.png", "Hills2.png", "Hills3.png", "Hills4.png", "Hills5.png",
"HillsGreen1.png", "HillsGreen2.png", "HillsGreen3.png",
"LightGrass1.png", "LightGrass2.png", "LightGrass3.png",
"LowHills1.png", "LowHills2.png", "LowHills3.png", "LowHills4.png",
"Mountains1.png", "Mountains2.png", "Mountains3.png",
"Mud1.png", "Mud2.png", "Mud3.png", "Mud4.png", "Mud5.png",
"Orchards1.png", "Orchards2.png", "Orchards3.png", "Orchards4.png",
"PineForest1.png", "PineForest2.png", "PineForest3.png",
"Woods1.png", "Woods2.png", "Woods3.png", "Woods4.png"}
function rsprite()
return string.format("res/%s", SPRITES[math.random(#SPRITES)])
end
----- [[ BLAH BLAH LBAH ]] ----------------------------------------------- ----- [[ BLAH BLAH LBAH ]] -----------------------------------------------
win = am.window { win = am.window {
@ -39,55 +56,52 @@ win = am.window {
----- [[ MAP RENDERING ]] ------------------------------------------------ ----- [[ MAP RENDERING ]] ------------------------------------------------
function map_init(layout)
map = rectmap_init(layout, 45, 31)
map_scene:action(function()
for _,hex in pairs(map) do
if hex_equals(_, vec2(23, 16)) then
print("yay")
else
map_scene:append(am.circle(hex, layout.size.x, rcolor(), 6))
end
end
function grid_init()
grid = am.group()
map_scene:append(am.rect(268, win.top, win.right, win.bottom, vec4(0.4, 0.6, 0.8, 1)))
local coalburner = [[
.........
..kkkkk..
.k.....k.
k..wo...k
k..ooo..k
k...o...k
.k.....k.
..kkkkk..
.........
]]
map_scene:append(am.translate(350, 200) ^ am.scale(10) ^ am.sprite(coalburner))
map_scene:append(am.particles2d({source_pos=vec2(400, win.top),
source_pos_var=vec2(0, 600),
angle=math.pi/4,
start_color=vec4(0.9),
gravity=vec2(100),
start_color_var=rcolor(),
start_size=5}))
--print(win.right - 268)
map = rectmap_init(layout_init(vec2(win.left, win.bottom)), 45, 31)
grid:action(function()
for hex,pix in pairs(map) do
grid:append(am.circle(pix, map.layout.size.x, rcolor(), 6))
end
return true return true
end) end)
grid:append(am.translate(350, 200)
^ am.scale(2)
^ am.sprite("2.png"))
show_hex_coords()
return grid
end end
----- [[ MAIN ]] -----------------------------------------------------------
function toolbar_init()
local toolbar = am.group()
local toolbar_bg = vec4(0.4, 0.6, 0.8, 1)
toolbar:append(am.rect(268, win.top, win.right, win.bottom, toolbar_bg))
toolbar:append(am.particles2d({source_pos=vec2(win.width/2-268, win.top),
source_pos_var=vec2(0, 600),
angle=math.pi/4,
start_color=vec4(0.9),
gravity=vec2(100),
start_color_var=rcolor(),
start_size=3}))
return toolbar
end
map = {}
function game_init()
return am.group{grid_init(), toolbar_init()}
end
----- [[ MAIN ]] -----------------------------------------------------------
gui_scene = am.group()
map_scene = am.group(); map_init(layout_init(vec2(win.left, win.bottom)))
local map = {}
win.scene = am.group{map_scene, gui_scene}
win.scene = game_init()
show_hex_coords(map)
show_hex_coords()
Loading…
Cancel
Save