Browse Source

recovered catastrophic data loss

master
churchianity 6 years ago
parent
commit
ec5482bc16
  1. 81
      hex.lua
  2. 48
      main.lua

81
hex.lua

@ -4,16 +4,16 @@
this is a hexagonal grid library for amulet/lua. this is a hexagonal grid library for amulet/lua.
it uses axial coordinates or cube/hex coordinates when necessary. it uses axial coordinates or cube/hex coordinates when necessary.
by amulet convention, hexes are either vec2(s, t) or vec3(s, t, z) by amulet convention, hexes are either vec2(s, t) or vec3(s, t, z)
but nearly always the former.
but nearly always the former.
-- RESOURCES USED TO DEVELOP THIS LIBRARY, AND FOR WHICH I AM GRATEFUL -- RESOURCES USED TO DEVELOP THIS LIBRARY, AND FOR WHICH I AM GRATEFUL
https://redblobgames.com/grid/hexagons - simply amazing.
https://redblobgames.com/grid/hexagons - simply amazing.
http://amulet.xyz/doc - amulet documentation http://amulet.xyz/doc - amulet documentation
]] ]]
----- [[ 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)
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
@ -21,26 +21,24 @@ end
----- [[ 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
local HEX_DIRECTIONS = {vec2( 1 , 0),
vec2( 1 , -1),
local HEX_DIRECTIONS = {vec2( 1 , 0),
vec2( 1 , -1),
vec2( 0 , -1), vec2( 0 , -1),
vec2(-1 , 0),
vec2(-1 , 1),
vec2(-1 , 0),
vec2(-1 , 1),
vec2( 0 , 1)} vec2( 0 , 1)}
-- return hex vector direction via index |direction|.
-- return hex vector direction via integer index |direction|.
function hex_direction(direction) function hex_direction(direction)
return HEX_DIRECTIONS[direction]
return HEX_DIRECTIONS[(6 + (direction % 6)) % 6 + 1]
end end
-- return hexagon adjacent to |hex| in |direction|
-- return hexagon adjacent to |hex| in integer index |direction|.
function hex_neighbour(hex, direction) function hex_neighbour(hex, direction)
return hex + HEX_DIRECTION[direction]
return hex + HEX_DIRECTIONS[(6 + (direction % 6)) % 6 + 1]
end end
-- rounds hexes. without this, pixel_to_hex returns fractional coordinates. -- rounds hexes. without this, pixel_to_hex returns fractional coordinates.
-- using single coordinates instead of a vector, because this should only
-- ever be called internally.
function hex_round(s, t) function hex_round(s, t)
local rs = round(s) local rs = round(s)
local rt = round(t) local rt = round(t)
@ -65,11 +63,13 @@ end
-- 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}
-- forward & inverse matrices used for the pointy orientation. -- 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), 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}
-- TODO encapsulate hex_to_pixel and pixel_to_hex in layout table. -- TODO encapsulate hex_to_pixel and pixel_to_hex in layout table.
-- stores layout information that does not pertain to map shape -- stores layout information that does not pertain to map shape
@ -82,7 +82,7 @@ end
-- hex to screen -- hex to screen
function hex_to_pixel(hex, layout) function hex_to_pixel(hex, layout)
local M = layout.orientation.M local M = layout.orientation.M
local x = (M[1][1] * hex.s + M[1][2] * hex.t) * layout.size.x local x = (M[1][1] * hex.s + M[1][2] * hex.t) * layout.size.x
local y = (M[2][1] * hex.s + M[2][2] * hex.t) * layout.size.y local y = (M[2][1] * hex.s + M[2][2] * hex.t) * layout.size.y
@ -101,18 +101,53 @@ function pixel_to_hex(pix, layout)
return hex_round(s, t) return hex_round(s, t)
end end
-- TODO test
function hex_corner_offset(layout, corner)
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))
end
-- TODO make do stuff
function hex_corners(layout, hex)
local corners = {}
end
----- [[ MAP STORAGE & RETRIEVAL ]] -------------------------------------------- ----- [[ MAP STORAGE & RETRIEVAL ]] --------------------------------------------
--[[ --[[
]] ]]
-- TODO make all functions work regardless of layout. -- TODO make all functions work regardless of layout.
-- returns ordered ring-shaped map of |radius| from |center|.
function hex_ring_map(center, radius)
local map = {}
local walk = center + HEX_DIRECTIONS[6] * radius
for i = 1, 6 do
for j = 1, radius do
table.insert(map, walk)
walk = hex_neighbour(walk, i)
end
end
return map
end
-- returns ordered hexagonal map of |radius| rings from |center|.
function hex_spiral_map(center, radius)
local map = {center}
for i = 1, radius do
table.append(map, hex_ring_map(center, i))
end
return map
end
-- returns unordered parallelogram-shaped map of |width| and |height|. -- returns unordered parallelogram-shaped map of |width| and |height|.
function hex_parallelogram_map(width, height) function hex_parallelogram_map(width, height)
local map = {} local map = {}
local mt = {__index={width=width, height=height}} local mt = {__index={width=width, height=height}}
setmetatable(map, mt) setmetatable(map, mt)
for s = 0, width do for s = 0, width do
for t = 0, height do for t = 0, height do
map[vec2(s, t)] = true map[vec2(s, t)] = true
@ -125,7 +160,7 @@ end
function hex_triangular_map(size) function hex_triangular_map(size)
local map = {} local map = {}
local mt = {__index={size=size}} local mt = {__index={size=size}}
setmetatable(map, mt) setmetatable(map, mt)
for s = 0, size do for s = 0, size do
@ -140,7 +175,7 @@ end
function hex_hexagonal_map(radius) function hex_hexagonal_map(radius)
local map = {} local map = {}
local mt = {__index={radius=radius}} local mt = {__index={radius=radius}}
setmetatable(map, mt) setmetatable(map, mt)
for s = -radius, radius do for s = -radius, radius do
@ -158,12 +193,12 @@ end
function hex_rectangular_map(width, height) function hex_rectangular_map(width, height)
local map = {} local map = {}
local mt = {__index={width=width, height=height}} local mt = {__index={width=width, height=height}}
setmetatable(map, mt) setmetatable(map, mt)
for s = 0, width do for s = 0, width do
for t = 0, height do for t = 0, height do
map[vec2(s, t)] = true
map[vec2(s, t - math.floor(s/2))] = true
end end
end end
return map return map

48
main.lua

@ -1,23 +1,24 @@
----- [[ WARZONE 2 - HEXAGONAL GRID RESOURCE BASED TOWER DEFENSE GAME]] -------- ----- [[ WARZONE 2 - HEXAGONAL GRID RESOURCE BASED TOWER DEFENSE GAME]] --------
--[[ author@churchianity.ca --[[ author@churchianity.ca
]] ]]
require "hex" require "hex"
require "util" require "util"
local world
local guibgcolor = vec4(0.5, 0.5, 0.2, 0)
local win = am.window{ local win = am.window{
-- BASE RESOLUTION = 3/4 * WXGA Standard 16:10 -- BASE RESOLUTION = 3/4 * WXGA Standard 16:10
width = 1280 * 3 / 4, -- 960px width = 1280 * 3 / 4, -- 960px
height = 800 * 3 / 4, -- 600px height = 800 * 3 / 4, -- 600px
title = "Warzone 2: Electric Boogaloo"} title = "Warzone 2: Electric Boogaloo"}
local title = am.group()
local world = am.group()
local layout = hex_layout(vec2(-368, win.bottom))
local map = hex_rectangular_map(45, 31)
local titlemap = hex_spiral_map(vec2(0), 10)
local titlelayout = hex_layout(vec2(win.right, win.bottom))
function show_axes() function show_axes()
xaxis = am.line(vec2(win.left, 0), vec2(win.right, 0)) xaxis = am.line(vec2(win.left, 0), vec2(win.right, 0))
yaxis = am.line(vec2(0, win.top), vec2(0, win.bottom)) yaxis = am.line(vec2(0, win.top), vec2(0, win.bottom))
@ -25,17 +26,6 @@ function show_axes()
end end
function world_init() function world_init()
world = am.group()
local layout = layout_init(vec2(-402, win.bottom))
local map = rectmap_init(45, 31)
local lgui = am.group(
am.rect(win.left, win.top, -402, win.bottom, guibgcolor))
local rgui = am.group(
am.rect(win.right, win.top, 402, win.bottom, guibgcolor))
world:append(lgui)
world:append(rgui)
world:action(coroutine.create(function() world:action(coroutine.create(function()
for hex,_ in pairs(map) do for hex,_ in pairs(map) do
world:append(am.circle(hex_to_pixel(hex, layout), 11, rrgb(1), 6)) world:append(am.circle(hex_to_pixel(hex, layout), 11, rrgb(1), 6))
@ -46,9 +36,25 @@ function world_init()
end end
function init() function init()
world_init()
show_axes()
win.scene = world
local rotatable = am.group(am.rotate(45):tag"rotatable")
local backdrop = am.group{rotatable}
for _,hex in pairs(titlemap) do
local center = hex_to_pixel(hex, titlelayout)
rotatable:append(am.circle(center, 11, rrgb(1), 6))
end
local line1 = am.text("WARZONE 2")
local line2 = am.text("Electric Boogaloo")
local line3 = am.text("by Nick Hayashi")
local title = am.group{backdrop,
am.translate(0, 150) ^ am.scale(4) ^ line1,
am.translate(0, 100) ^ am.scale(3) ^ line2,
am.translate(0, 60) ^ am.scale(1) ^ line3
}:action(function()
rotatable"rotatable".angle = (am.frame_time / 5)
end)
win.scene = title
end end
----- [[ MAIN ]] --------------------------------------------------------------- ----- [[ MAIN ]] ---------------------------------------------------------------

Loading…
Cancel
Save