Browse Source

better naming conventions - cleaner main

master
churchianity 6 years ago
parent
commit
8640051f9f
  1. 71
      hex.lua
  2. 191
      main.lua
  3. 54
      sprites.lua
  4. 38
      util.lua

71
hex.lua

@ -1,7 +1,7 @@
--[[============================================================================ --[[============================================================================
----- GENERALLY USEFUL FUNCTIONS ----- ----- GENERALLY USEFUL FUNCTIONS -----
==============================================================================]]
============================================================================]]--
-- rounds numbers. would've been cool to have math.round in lua. -- rounds numbers. would've been cool to have math.round in lua.
local function round(n) local function round(n)
@ -10,7 +10,7 @@ end
--[[============================================================================ --[[============================================================================
----- HEX CONSTANTS AND UTILITY FUNCTIONS ----- ----- HEX CONSTANTS AND UTILITY FUNCTIONS -----
==============================================================================]]
============================================================================]]--
-- all possible vector directions from a given hex by edge -- all possible vector directions from a given hex by edge
local CUBE_DIRECTIONS = {vec2( 1 , 0), local CUBE_DIRECTIONS = {vec2( 1 , 0),
@ -30,15 +30,17 @@ function cube_neighbour(hex, direction)
return hex + CUBE_DIRECTIONS[(6 + (direction % 6)) % 6 + 1] return hex + CUBE_DIRECTIONS[(6 + (direction % 6)) % 6 + 1]
end end
-- TODO cube rotations
-- return cube coords at location 60deg away to the left; counter-clockwise
function cube_rotate_left(hex) function cube_rotate_left(hex)
return vec2(hex.x + hex.y, -hex.x)
end end
-- return cube coords at location 60deg away to the right; clockwise
function cube_rotate_right(hex) function cube_rotate_right(hex)
return vec2(-hex.y, hex.x + hex.y)
end end
-- rounds a float coordinate trio |x, y, z| to nearest integer coordinate trio -- rounds a float coordinate trio |x, y, z| to nearest integer coordinate trio
-- only ever used internally; no need to use a vector.
local function cube_round(x, y, z) local function cube_round(x, y, z)
local rx = round(x) local rx = round(x)
local ry = round(y) local ry = round(y)
@ -46,7 +48,7 @@ local function cube_round(x, y, z)
local xdelta = math.abs(rx - x) local xdelta = math.abs(rx - x)
local ydelta = math.abs(ry - y) local ydelta = math.abs(ry - y)
local zdelta = math.abs(rz - z)
local zdelta = math.abs(rz - z or round(-x - y))
if xdelta > ydelta and xdelta > zdelta then if xdelta > ydelta and xdelta > zdelta then
rx = -ry - rz rx = -ry - rz
@ -55,30 +57,29 @@ local function cube_round(x, y, z)
else else
rz = -rx - ry rz = -rx - ry
end end
return vec2(rx, ry) return vec2(rx, ry)
end end
--[[============================================================================ --[[============================================================================
----- ORIENTATION & LAYOUT ----- ----- ORIENTATION & LAYOUT -----
==============================================================================]]
============================================================================]]--
-- forward & inverse matrices used for the flat orientation.
-- forward & inverse matrices used for the flat orientation
local FLAT = {M = mat2(3.0/2.0, 0.0, 3.0^0.5/2.0, 3.0^0.5 ), local FLAT = {M = mat2(3.0/2.0, 0.0, 3.0^0.5/2.0, 3.0^0.5 ),
W = mat2(2.0/3.0, 0.0, -1.0/3.0 , 3.0^0.5/3.0), W = mat2(2.0/3.0, 0.0, -1.0/3.0 , 3.0^0.5/3.0),
start_angle = 0.0} start_angle = 0.0}
-- forward & inverse matrices used for the pointy orientation.
local POINTY = {M = mat2(3.0^0.5, 3.0^0.5/2.0, 0.0, 3.0/2.0),
-- forward & inverse matrices used for the pointy orientation
local POINTY = {M = mat2(3.0^0.5, 3.0^0.5/2.0, 0.0, 3.0/2.0),
W = mat2(3.0^0.5/3.0, -1.0/3.0, 0.0, 2.0/3.0), W = mat2(3.0^0.5/3.0, -1.0/3.0, 0.0, 2.0/3.0),
start_angle = 0.5} start_angle = 0.5}
-- stores layout: information that does not pertain to map shape
-- stores layout: information that does not pertain to map shape
function layout(origin, size, orientation) function layout(origin, size, orientation)
return {origin = origin or vec2(0), return {origin = origin or vec2(0),
size = size or vec2(11), size = size or vec2(11),
orientation = orientation or FLAT} orientation = orientation or FLAT}
end
end
-- hex to screen -- hex to screen
function cube_to_pixel(cube, layout) function cube_to_pixel(cube, layout)
@ -99,13 +100,13 @@ function pixel_to_cube(pix, layout)
local s = W[1][1] * pix[1] + W[1][2] * pix[2] local s = W[1][1] * pix[1] + W[1][2] * pix[2]
local t = W[2][1] * pix[1] + W[2][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 -- 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[1] * math.cos(angle),
return vec2(layout.size[1] * math.cos(angle),
layout.size[2] * math.sin(angle)) layout.size[2] * math.sin(angle))
end end
@ -114,25 +115,34 @@ function hex_corners(hex, layout)
local corners = {} local corners = {}
end end
--
-- offset coordinates are prettier to look at
function cube_to_offset(cube) function cube_to_offset(cube)
return vec2(cube[1], -cube[1] - cube[2] + (cube[1] + (cube[1] % 2)) / 2) return vec2(cube[1], -cube[1] - cube[2] + (cube[1] + (cube[1] % 2)) / 2)
end end
--
-- back to cube coordinates
function offset_to_cube(off) function offset_to_cube(off)
return vec2(off[1], off[2] - off[1] * (off[1] % 2) / 2) return vec2(off[1], off[2] - off[1] * (off[1] % 2) / 2)
end end
--[[============================================================================ --[[============================================================================
----- MAPS & STORAGE ----- ----- MAPS & STORAGE -----
==============================================================================]]
-- information about the maps' dimensions are stored in a metatable, so you can
-- retrieve details about arbitrary maps after they are created.
MAPS STORE CUBE COORDINATES. MAPS STORE CUBE COORDINATES. MAPS STORE CUBE COOR
This means, you are not to draw using the coordinates stored in your map.
You are to draw using the cube_to_pixel of those coordinates.
If you wish to draw a hexagon to the screen, you must first use cube_to_pixel
to retrieve the center of the hexagon on each set of cube coordinates stored
in your map.
-- TODO make all functions work regardless of layout. as it stands, they kind
-- of do, just not always nicely.
Information about the maps' dimensions are stored in a metatable, so you can
retrieve details about arbitrary maps after they are created.
TODO make all functions work regardless of layout. as it stands, they kind
of do, just not always nicely.
============================================================================]]--
-- returns ordered ring-shaped map of |radius| from |center|. -- returns ordered ring-shaped map of |radius| from |center|.
function ring_map(center, radius) function ring_map(center, radius)
@ -141,12 +151,12 @@ function ring_map(center, radius)
setmetatable(map, mt) setmetatable(map, mt)
local walk = center + HEX_DIRECTIONS[6] * radius
local walk = center + CUBE_DIRECTIONS[6] * radius
for i = 1, 6 do for i = 1, 6 do
for j = 1, radius do for j = 1, radius do
table.insert(map, walk) table.insert(map, walk)
walk = hex_neighbour(walk, i)
walk = cube_neighbour(walk, i)
end end
end end
return map return map
@ -162,7 +172,7 @@ function spiral_map(center, radius)
setmetatable(map, mt) setmetatable(map, mt)
for i = 1, radius do for i = 1, radius do
table.append(map, hex_ring_map(center, i))
table.append(map, ring_map(center, i))
end end
return map return map
end end
@ -230,9 +240,20 @@ function rectangular_map(width, height)
return map return map
end end
--[[============================================================================
----- PATHFINDING -----
============================================================================]]--
--[[============================================================================ --[[============================================================================
----- TESTS ----- ----- TESTS -----
==============================================================================]]
============================================================================]]--
function test_all() function test_all()
print("it works trust me") print("it works trust me")

191
main.lua

@ -1,86 +1,177 @@
require"hex" require"hex"
require"util" require"util"
require"sprites"
--[[============================================================================ --[[============================================================================
----- GLOBALS -----
==============================================================================]]
local win = am.window{
-- base resolution = 3/4 * WXGA standard 16:10
width = 1280 * 3 / 4, -- 960px
height = 800 * 3 / 4, -- 600px
clear_color = vec4(0.01, 34/255, 45/255, 0),
title = "Warzone 2: Electric Boogaloo",
resizable = false,
}
----- COLOR CONSTANTS -----
============================================================================]]--
local EIGENGRAU = vec4(0.08, 0.08, 0.11, 1)
-- Ethan Schoonover Solarized Colorscheme
local BASE03 = vec4(0 , 0.16, 0.21, 1)
local BASE02 = vec4(0.02, 0.21, 0.25, 1)
local BASE01 = vec4(0.34, 0.43, 0.45, 1)
local BASE00 = vec4(0.39, 0.48, 0.51, 1)
local BASE0 = vec4(0.51, 0.58, 0.58, 1)
local BASE1 = vec4(0.57, 0.63, 0.63, 1)
local BASE2 = vec4(0.93, 0.90, 0.83, 1)
local BASE3 = vec4(0.99, 0.96, 0.89, 1)
local YELLOW = vec4(0.70, 0.53, 0 , 1)
local ORANGE = vec4(0.79, 0.29, 0.08, 1)
local RED = vec4(0.86, 0.19, 0.18, 1)
local MAGENTA = vec4(0.82, 0.21, 0.50, 1)
local VIOLET = vec4(0.42, 0.44, 0.76, 1)
local BLUE = vec4(0.14, 0.54, 0.82, 1)
local CYAN = vec4(0.16, 0.63, 0.59, 1)
local GREEN = vec4(0.52, 0.60, 0 , 1)
am.ascii_color_map =
{
E = EIGENGRAU,
K = BASE03,
k = BASE02,
L = BASE01,
l = BASE00,
s = BASE0,
S = BASE1,
w = BASE2,
W = BASE3,
y = YELLOW,
o = ORANGE,
r = RED,
m = MAGENTA,
v = VIOLET,
b = BLUE,
c = CYAN,
g = GREEN
}
--[[============================================================================
----- WINDOW SETUP -----
============================================================================]]--
local win = am.window
{ -- base resolution = 3/4 * WXGA standard 16:10
width = 1280 * 3/4, -- 960px
height = 800 * 3/4, -- 600px
clear_color = BASE03
}
--[[============================================================================
============================================================================]]--
local map = rectangular_map(45, 31)
local layout = layout(vec2(-268, win.top - 10)) local layout = layout(vec2(-268, win.top - 10))
local map
local world = am.group{}:tag"world"
--[[============================================================================ --[[============================================================================
----- FUNCTIONS -----
==============================================================================]]
----- SCENE GRAPH / NODES -----
============================================================================]]--
local panel; local world; local game; --[[
panel
|
#------> game ------> win.scene
|
world
--==========================================================================]]--
local backdrop; local menu; local title; --[[
backdrop
|
#------> title ------> win.scene
|
menu
--[[============================================================================
----- FUNCTIONS -----
============================================================================]]--
function keep_time()
local offset = am.current_time()
function show_hex_coords()
world:action(function() world:action(function()
world:remove("coords")
world:remove("select")
world:remove("time")
local time_str = string.format("%.2f", am.current_time() - offset)
world:append(
am.translate(-374, win.top - 10)
^ am.text(time_str):tag"time")
end)
end
-- TODO refactor to something like - poll-mouse or mouse-hover event
function show_coords()
game:action(function()
game:remove("coords")
game:remove("select")
local hex = pixel_to_cube(win:mouse_position(), layout) local hex = pixel_to_cube(win:mouse_position(), layout)
local mouse = cube_to_offset(hex) local mouse = cube_to_offset(hex)
if mouse.x > 0 and mouse.x < map.width and
if mouse.x > 0 and mouse.x < map.width and
mouse.y > 0 and mouse.y < map.height then mouse.y > 0 and mouse.y < map.height then
local text = am.text(string.format("%d,%d", mouse.x, mouse.y))
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"}
world:append(coords)
local mask = vec4(1, 1, 1, 0.2)
world:append(coords)
local color = vec4(1, 1, 1, 0.2)
local pix = cube_to_pixel(hex, layout) local pix = cube_to_pixel(hex, layout)
world:append(am.circle(pix, layout.size.x, mask, 6):tag"select")
end
world:append(am.circle(pix, layout.size.x, color, 6):tag"select")
end
end) end)
end end
function world_init()
function title_init()
backdrop = am.group{}:tag"backdrop"
menu = am.group{}:tag"menu"
title = am.group{menu, backdrop}:tag"title"
end
function game_init()
world = am.group{}:tag"world"
panel = am.group{}:tag"panel"
game = am.group{world, panel}:tag"game"
local hexes = {}
for cube,_ in pairs(map) do
hexes[math.perlin(cube)] = cube_to_pixel(cube, layout)
end
world:action(coroutine.create(function() world:action(coroutine.create(function()
-- init guiblock
local bg = am.rect(win.left, win.top, -268, win.bottom):tag"bg"
world:append(bg)
-- init map
map = rectangular_map(45, 31)
for hex,elevation in pairs(map) do
local pix = cube_to_pixel(hex, layout)
panel:append(am.rect(win.left, win.top, -268, win.bottom):tag"bg")
for noise, hex in pairs(hexes) do
local off = cube_to_offset(hex) local off = cube_to_offset(hex)
local tag = tostring(hex) local tag = tostring(hex)
local color
local mask
-- testing noise with color
color = vec4(1, 1, 1, (elevation + 1) / 2)
-- determine cell shading mask based on map position -- determine cell shading mask based on map position
--mask = vec4(0, 0, 0, math.max(((off.x-23)/30)^2, ((off.y-16)/20)^2))
--color = color - mask
local mask = vec4(0, 0, 0, math.max(((off.x-23)/30)^2,
((off.y-16)/20)^2))
-- determine cell color based on noise
local color = vec4(math.random()) - mask
world"bg".color = vec4(0, 43/255, 54/255, am.frame_time)
world:prepend(am.circle(pix, layout.size.x, color, 6):tag(tag))
panel"bg".color = BASE03/am.frame_time
world:prepend(am.circle(hex, 11, color, 6):tag(tag))
am.wait(am.delay(0.01)) am.wait(am.delay(0.01))
end end
show_hex_coords()
show_coords()
keep_time()
end)) end))
win.scene = world
win.scene = game
end end
--[[============================================================================ --[[============================================================================
----- MAIN ----- ----- MAIN -----
==============================================================================]]
world_init()
============================================================================]]--
game_init()

54
sprites.lua

@ -0,0 +1,54 @@
grass1 =
[[
......gggggggggg......
.....ggggKggggggg.....
....ggggggKggggggg....
....gggggggggggggg....
...gggggggggggggggg...
...gggggggggggggggg...
..gggggggggggggggggg..
..gggggggggggggggggg..
.ggggggggggkggggggggg.
ggggggggggmmgggggggggg
.gggggggggggggggggggg.
..ggggggggglgggggggg..
..gggggggglggggggggg..
...gggggggLgggggggg...
...gggggggggggggggg...
....ggggggsggggggg....
.....ggggggsgggggg....
.....ggggggSggggg.....
......gggggggggg......
]]
titlebutton =
[[
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
KwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwK
KwkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
KwkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
KwkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
KwkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
Kwkkk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kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
KwkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
KwkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
KwkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
KwkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
KkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkK
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
]]
-- modified ethan shoonover solarized colortheme
am.ascii_color_map = {
E = vec4( 22/255, 22/255, 29/255, 1), -- eigengrau
K = vec4( 0, 43/255, 54/255, 1), -- dark navy
k = vec4( 7/255, 54/255, 66/255, 1), -- navy
L = vec4( 88/255, 110/255, 117/255, 1), -- gray1
l = vec4(101/255, 123/255, 131/255, 1), -- gray2
s = vec4(131/255, 148/255, 150/255, 1), -- gray3
S = vec4(147/255, 161/255, 161/255, 1), -- gray4
w = vec4(238/255, 232/255, 213/255, 1), -- bone
W = vec4(253/255, 246/255, 227/255, 1), -- white
y = vec4(181/255, 137/255, 0, 1), -- yellow
o = vec4(203/255, 75/255, 22/255, 1), -- orange
r = vec4(220/255, 50/255, 47/255, 1), -- red
m = vec4(211/255, 54/255, 130/255, 1), -- magenta
v = vec4(108/255, 113/255, 196/255, 1), -- violet
b = vec4( 38/255, 139/255, 210/255, 1), -- blue
c = vec4( 42/255, 161/255, 152/255, 1), -- cyan
g = vec4(133/255, 153/255, 0, 1) -- green
}
Loading…
Cancel
Save