Browse Source

shadows! shading! colors!

master
churchianity 6 years ago
parent
commit
d8d3378a65
  1. 58
      hex.lua
  2. 50
      main.lua

58
hex.lua

@ -6,13 +6,6 @@ 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
@ -62,7 +55,7 @@ function cube_round(x, y, z)
rz = -rx - ry rz = -rx - ry
end end
return vec3(rx, ry, rz)
return vec2(rx, ry)
end end
----- [[ LAYOUT, ORIENTATION & COORDINATE CONVERSION ]] ----------------------- ----- [[ LAYOUT, ORIENTATION & COORDINATE CONVERSION ]] -----------------------
@ -88,10 +81,10 @@ end
function cube_to_pixel(cube, layout) function cube_to_pixel(cube, layout)
local M = layout.orientation.M local M = layout.orientation.M
local x = (M[1][1] * cube.x + M[1][2] * cube.y) * layout.size.x
local y = (M[2][1] * cube.x + M[2][2] * cube.y) * layout.size.y
local x = (M[1][1] * cube[1] + M[1][2] * cube[2]) * layout.size[1]
local y = (M[2][1] * cube[1] + M[2][2] * cube[2]) * layout.size[2]
return vec2(x + layout.origin.x, y + layout.origin.y)
return vec2(x + layout.origin[1], y + layout.origin[2])
end end
-- screen to hex -- screen to hex
@ -100,35 +93,30 @@ function pixel_to_cube(pix, layout)
local pix = (pix - layout.origin) / layout.size local pix = (pix - layout.origin) / layout.size
local s = W[1][1] * pix.x + W[1][2] * pix.y
local t = W[2][1] * pix.x + W[2][2] * pix.y
local s = W[1][1] * pix[1] + W[1][2] * pix[2]
local t = W[2][1] * pix[1] + W[2][2] * pix[2]
return cube_round(s, t, -s - t) return cube_round(s, t, -s - t)
end end
-- TODO test, learn am.draw
function hex_corner_offset(corner, layout) function hex_corner_offset(corner, layout)
local angle = 2.0 * math.pi * layout.orientation.start_angle + corner / 6 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))
return vec2(layout.size[1] * math.cos(angle),
layout.size[2] * math.sin(angle))
end end
-- TODO this thing
function hex_corners(hex, layout) function hex_corners(hex, layout)
local corners = {} local corners = {}
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)
return vec2(cube[1], -cube[1] - cube[2] + (cube[1] + (cube[1] % 2)) / 2)
end end
function offset_to_cube(off) function offset_to_cube(off)
end
function cube_to_doubled(cube)
return vec2(cube.x, 2 * (-cube.x - cube.y) + cube.x)
end
function doubled_to_cube(dbl)
return vec2(dbl.x, (dbl.y - dbl.x) / 2)
return vec2(off[1], off[2] - off[1] * (off[1] % 2) / 2)
end end
----- [[ MAP STORAGE & RETRIEVAL ]] -------------------------------------------- ----- [[ MAP STORAGE & RETRIEVAL ]] --------------------------------------------
@ -236,4 +224,26 @@ end
----- [[ TESTS ]] -------------------------------------------------------------- ----- [[ TESTS ]] --------------------------------------------------------------
function test_all()
test_rectangular_map()
end
function test_rectangular_map()
local map = rectangular_map(8, 5)
local layout = layout()
for hex,_ in pairs(map) do
print(cube_to_pixel(hex, layout))
end
for i = 0, 10 do
local mouse = pixel_to_cube(vec2(math.random(map.width) * 22,
math.random(map.height) * 10),
layout)
print(mouse)
end
end

50
main.lua

@ -1,11 +1,11 @@
----- [[ WARZONE 2 - HEXAGONAL GRID RESOURCE BASED TOWER DEFENSE GAME]] --------
--[[ author@churchianity.ca
]]
----- WARZONE 2 - HEXAGONAL GRID RESOURCE BASED TOWER DEFENSE GAME -------------
-- author@churchianity.ca
require"hex" require"hex"
require"util" require"util"
------ [[ GLOBALS ]] -----------------------------------------------------------
------ GLOBALS -----------------------------------------------------------------
local win = am.window{ local win = am.window{
-- BASE RESOLUTION = 3/4 * WXGA Standard 16:10 -- BASE RESOLUTION = 3/4 * WXGA Standard 16:10
@ -19,8 +19,6 @@ 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"
----- [[ SPRITES ]] ------------------------------------------------------------
-- modified ethan shoonover solarized colortheme -- modified ethan shoonover solarized colortheme
am.ascii_color_map = { am.ascii_color_map = {
E = vec4( 22/255, 22/255, 29/255, 1), -- eigengrau E = vec4( 22/255, 22/255, 29/255, 1), -- eigengrau
@ -70,32 +68,49 @@ end
function show_hex_coords() function show_hex_coords()
win.scene:action(function() win.scene:action(function()
win.scene:remove("coords") win.scene:remove("coords")
win.scene:remove("select")
local mouse = cube_to_offset(pixel_to_cube(win:mouse_position(), layout))
local hex = pixel_to_cube(win:mouse_position(), layout)
local mouse = cube_to_offset(hex)
if mouse.x > 0 and mouse.x < 45 and mouse.y > 0 and mouse.y < 31 then
if mouse.x > 0 and mouse.x < map.width and
mouse.y > 0 and mouse.y < map.height then
local coords = am.group{ local coords = am.group{
am.translate(win.right - 25, 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)
local mask = vec4(1, 1, 1, 0.2)
local pix = cube_to_pixel(hex, layout)
world:append(am.circle(pix, layout.size.x, mask, 6):tag"select")
end end
end) end)
end end
function init() function init()
world:action(coroutine.create(function()
local gui = am.rect(win.left, win.top, -268, win.bottom):tag"gui"
world:append(gui)
for hex,_ in pairs(map) do for hex,_ in pairs(map) do
local pix = cube_to_pixel(hex, layout) 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))
local off = cube_to_offset(hex)
local tag = tostring(hex)
local color
if off.x == 0 or off.x == map.width or
off.y == 0 or off.y == map.height then
color = rhue(0.3)
else
color = rhue(1 - math.max(((off.x-23)/30)^2, ((off.y-16)/20)^2))
end end
world"gui".color = vec4(0, 43/255, 54/255, am.frame_time / 20)
world:prepend(am.circle(pix, layout.size.x, color, 6):tag(tag))
am.wait(am.delay(0.01))
end
end))
win.scene = world win.scene = world
end end
@ -103,3 +118,4 @@ end
init() init()
show_hex_coords() show_hex_coords()
Loading…
Cancel
Save