Browse Source

drawing shadows!

master
churchianity 6 years ago
parent
commit
7ea54c123b
  1. 41
      hex.lua
  2. 22
      main.lua

41
hex.lua

@ -6,6 +6,13 @@ 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
----- [[ UI FUNCTIONS ]] -------------------------------------------------------
----- [[ HEX CONSTANTS & UTILITY FUNCTIONS ]] ---------------------------------- ----- [[ HEX CONSTANTS & UTILITY FUNCTIONS ]] ----------------------------------
-- all possible vector directions from a given hex by edge -- all possible vector directions from a given hex by edge
@ -41,7 +48,7 @@ end
function cube_round(x, y, z) function cube_round(x, y, z)
local rx = round(x) local rx = round(x)
local ry = round(y) local ry = round(y)
local rz = round(z)
local rz = round(z) or round(-x - y)
local xdelta = math.abs(rx - x) local xdelta = math.abs(rx - x)
local ydelta = math.abs(ry - y) local ydelta = math.abs(ry - y)
@ -50,7 +57,7 @@ function cube_round(x, y, z)
if xdelta > ydelta and xdelta > zdelta then if xdelta > ydelta and xdelta > zdelta then
rx = -ry - rz rx = -ry - rz
elseif ydelta > zdelta then elseif ydelta > zdelta then
rx = -ry - rz
ry = -rx - rz
else else
rz = -rx - ry rz = -rx - ry
end end
@ -109,7 +116,7 @@ function hex_corners(hex, layout)
end end
function cube_to_offset(cube) function cube_to_offset(cube)
return vec2(cube.x, -cube.x - cube.y + (cube.x + (cube.x % 2)) / 2)
end end
function offset_to_cube(off) function offset_to_cube(off)
@ -171,9 +178,9 @@ function parallelogram_map(width, height)
setmetatable(map, mt) setmetatable(map, mt)
for s = 0, width do
for t = 0, height do
map[vec2(s, t)] = true
for i = 0, width do
for j = 0, height do
map[vec2(i, -j)] = true
end end
end end
return map return map
@ -186,9 +193,9 @@ function triangular_map(size)
setmetatable(map, mt) setmetatable(map, mt)
for s = 0, size do
for t = size - s, size do
map[vec2(s, t)] = true
for i = 0, size do
for j = size - s, size do
map[vec2(i, j)] = true
end end
end end
return map return map
@ -201,12 +208,12 @@ function hexagonal_map(radius)
setmetatable(map, mt) setmetatable(map, mt)
for s = -radius, radius do
local t1 = math.max(-radius, -s - radius)
local t2 = math.min(radius, -s + radius)
for i = -radius, radius do
local j1 = math.max(-radius, -i - radius)
local j2 = math.min(radius, -i + radius)
for t = t1, t2 do
map[vec2(s, t)] = true
for j = j1, j2 do
map[vec2(i, j)] = true
end end
end end
return map return map
@ -219,9 +226,9 @@ function rectangular_map(width, height)
setmetatable(map, mt) setmetatable(map, mt)
for s = 0, width do
for t = 0, height do
map[vec2(s, t - math.floor(s/2))] = true
for i = 0, width do
for j = 0, height do
map[vec2(i, -j - math.floor(i/2))] = true
end end
end end
return map return map

22
main.lua

@ -15,7 +15,7 @@ local win = am.window{
resizable = false resizable = false
} }
local layout = layout(vec2(-268, win.bottom))
local layout = layout(vec2(-268, win.top - 10))
local map = rectangular_map(45, 31) local map = rectangular_map(45, 31)
local world = am.group{}:tag"world" local world = am.group{}:tag"world"
@ -59,7 +59,7 @@ KwkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
KkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK KkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
]] ]]
----- [[ FUNCTIONS ]] ----------------------------------------------------------
----- FUNCTIONS ----------------------------------------------------------
function show_axes() function show_axes()
local xaxis = am.line(vec2(win.left, 0), vec2(win.right, 0)) local xaxis = am.line(vec2(win.left, 0), vec2(win.right, 0))
@ -71,11 +71,11 @@ function show_hex_coords()
win.scene:action(function() win.scene:action(function()
win.scene:remove("coords") win.scene:remove("coords")
local mouse = axial_to_doubled(pixel_to_hex(win:mouse_position(), layout))
local mouse = cube_to_offset(pixel_to_cube(win:mouse_position(), layout))
if mouse.x > 0 and mouse.x < 45 and mouse.y < 0 and mouse.y > -63 then
if mouse.x > 0 and mouse.x < 45 and mouse.y > 0 and mouse.y < 31 then
local coords = am.group{ local coords = am.group{
am.translate(win.left + 30, win.top - 10)
am.translate(win.right - 25, win.top - 10)
^ am.text(string.format("%d,%d", mouse.x, mouse.y)):tag"coords"} ^ am.text(string.format("%d,%d", mouse.x, mouse.y)):tag"coords"}
win.scene:append(coords) win.scene:append(coords)
end end
@ -84,8 +84,16 @@ end
function init() function init()
for hex,_ in pairs(map) do for hex,_ in pairs(map) do
local pix = hex_to_pixel(hex, layout)
world:append(am.circle(pix, 11, rhue(1), 6))
local pix = cube_to_pixel(hex, layout)
local helper = cube_to_offset(hex)
local r = math.random()
local g = math.random()
local b = math.random()
local a = 1 - ((helper.x + 23)^2)/500 + ((helper.y + 16)^2)/500
local color = vec4(r, g, b, a)
local tag = tostring(hex.x, hex.y)
world:append(am.circle(pix, layout.size.x, color, 6):tag(tag))
end end
win.scene = world win.scene = world

Loading…
Cancel
Save