From 7ea54c123baf69e4eb0dea2bf05063ce7cd3be17 Mon Sep 17 00:00:00 2001 From: churchianity Date: Wed, 20 Feb 2019 12:50:47 -0500 Subject: [PATCH] drawing shadows! --- hex.lua | 41 ++++++++++++++++++++++++----------------- main.lua | 22 +++++++++++++++------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/hex.lua b/hex.lua index fead051..972491d 100644 --- a/hex.lua +++ b/hex.lua @@ -6,6 +6,13 @@ local function round(n) return n % 1 >= 0.5 and math.ceil(n) or math.floor(n) end +----- [[ UI FUNCTIONS ]] ------------------------------------------------------- + + + + + + ----- [[ HEX CONSTANTS & UTILITY FUNCTIONS ]] ---------------------------------- -- all possible vector directions from a given hex by edge @@ -41,7 +48,7 @@ end function cube_round(x, y, z) local rx = round(x) local ry = round(y) - local rz = round(z) + local rz = round(z) or round(-x - y) local xdelta = math.abs(rx - x) local ydelta = math.abs(ry - y) @@ -50,7 +57,7 @@ function cube_round(x, y, z) if xdelta > ydelta and xdelta > zdelta then rx = -ry - rz elseif ydelta > zdelta then - rx = -ry - rz + ry = -rx - rz else rz = -rx - ry end @@ -109,7 +116,7 @@ function hex_corners(hex, layout) end function cube_to_offset(cube) - + return vec2(cube.x, -cube.x - cube.y + (cube.x + (cube.x % 2)) / 2) end function offset_to_cube(off) @@ -171,9 +178,9 @@ function parallelogram_map(width, height) 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 return map @@ -186,9 +193,9 @@ function triangular_map(size) 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 return map @@ -201,12 +208,12 @@ function hexagonal_map(radius) 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 return map @@ -219,9 +226,9 @@ function rectangular_map(width, height) 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 return map diff --git a/main.lua b/main.lua index 622741b..95c8566 100644 --- a/main.lua +++ b/main.lua @@ -15,7 +15,7 @@ local win = am.window{ resizable = false } -local layout = layout(vec2(-268, win.bottom)) +local layout = layout(vec2(-268, win.top - 10)) local map = rectangular_map(45, 31) local world = am.group{}:tag"world" @@ -59,7 +59,7 @@ KwkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK KkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK ]] ------ [[ FUNCTIONS ]] ---------------------------------------------------------- +----- FUNCTIONS ---------------------------------------------------------- function show_axes() 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: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{ - 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"} win.scene:append(coords) end @@ -84,8 +84,16 @@ end function init() 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 win.scene = world