Browse Source

hi lol

master
churchianity 6 years ago
parent
commit
ceb9a33620
  1. 4
      README.md
  2. 5
      src/hexyz.lua
  3. 101
      src/main.lua

4
README.md

@ -3,7 +3,7 @@
This is a small and simple library for using hexagonal grids in amulet + lua. I wrote it for a tower defense game I'm making. This is a small and simple library for using hexagonal grids in amulet + lua. I wrote it for a tower defense game I'm making.
It's not really well documented. If you want an actual good resource, go to [amit's guide to hexagonal grids](https://redblobgames.com/grid/hexagons).
It's not really well documented. If you want an actual good resource, go to [amit's guide to hexagonal grids](https://redblobgames.com/grids/hexagons).
So much of what is here I derived from amit's work. So much of what is here I derived from amit's work.
@ -45,7 +45,7 @@ The noise values on the hashmaps are seeded. You can optionally provide a seed a
* [Hex Map 1](https://catlikecoding.com/unity/tutorials/hex-map/) - unity tutorial for hexagon grids with some useful generalized math. * [Hex Map 1](https://catlikecoding.com/unity/tutorials/hex-map/) - unity tutorial for hexagon grids with some useful generalized math.
* [Hexagonal Grids](https://redblobgames.com/grid/hexagons) - THE resource on hexagonal grids on the internet.
* [Hexagonal Grids](https://redblobgames.com/grids/hexagons) - THE resource on hexagonal grids on the internet.
* [Amulet Docs](http://amulet.xyz/doc) - amulet documentation. * [Amulet Docs](http://amulet.xyz/doc) - amulet documentation.

5
src/hexyz.lua

@ -268,8 +268,9 @@ function rectangular_map(width, height, seed)
local noise = 0 local noise = 0
for oct = 1, math.max(width, height) do for oct = 1, math.max(width, height) do
noise = noise + 2/3^oct * math.simplex(vec2(idelta + seed * width, jdelta + seed * height) * 2^oct)
noise = noise + 2/3^oct * math.simplex(vec2(idelta + seed*width,
jdelta + seed*height
* 2^oct))
end end
-- store hex in the map paired with its associated noise value -- store hex in the map paired with its associated noise value

101
src/main.lua

@ -90,40 +90,20 @@ local map = rectangular_map(45, 31)
local layout = layout(vec2(-268, win.bottom)) local layout = layout(vec2(-268, win.bottom))
local home = hex_to_pixel(vec2(23, 4), layout) local home = hex_to_pixel(vec2(23, 4), layout)
--[[============================================================================
----- 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 ----- ----- FUNCTIONS -----
============================================================================]]-- ============================================================================]]--
-- --
function keep_time()
function keep_score()
local offset = am.current_time() local offset = am.current_time()
game:action(function()
game:remove("time")
world:action(function()
world:remove("time")
local time_str = string.format("%.2f", am.current_time() - offset) local time_str = string.format("%.2f", am.current_time() - offset)
game:append(
world:append(
am.translate(-374, win.top - 10) am.translate(-374, win.top - 10)
^ am.text(time_str):tag"time") ^ am.text(time_str):tag"time")
end) end)
@ -131,9 +111,9 @@ end
-- TODO refactor to something like - poll-mouse or mouse-hover event -- TODO refactor to something like - poll-mouse or mouse-hover event
function show_coords() function show_coords()
game:action(function()
game:remove("coords")
game:remove("selected")
world:action(function()
world:remove("coords")
world:remove("selected")
local hex = pixel_to_hex(win:mouse_position(), layout) local hex = pixel_to_hex(win:mouse_position(), layout)
local mouse = hex_to_offset(hex) local mouse = hex_to_offset(hex)
@ -156,53 +136,42 @@ function show_coords()
end) end)
end end
--
function title_init()
backdrop = am.group{}:tag"backdrop"
menu = am.group{}:tag"menu"
title = am.group{menu, backdrop}:tag"title"
end
--
function game_init()
-- setup nodes
-- generate map, no seed means random map
function render_map(seed)
-- setup
world = am.group{}:tag"world" world = am.group{}:tag"world"
panel = am.group{}:tag"panel"
game = am.group{world, panel}:tag"game"
map = rectangular_map(45, 31, seed)
-- render world -- render world
world:action(coroutine.create(function() world:action(coroutine.create(function()
-- background panel for gui elements
panel:append(am.rect(win.left, win.top, -268, win.bottom):tag"bg")
-- begin map generation -- begin map generation
for hex,noise in pairs(map) do
for hex,elevation in pairs(map) do
-- determine cell color based on noise
-- determine cell
local color local color
-- impassable
if noise < -0.5 then
color = vec4(0.10, 0.30, 0.20, (noise + 1) / 2)
-- lowest elevation : impassable
if elevation < -0.5 then
color = vec4(0.10, 0.30, 0.20, (elevation + 1) / 2)
-- passable
elseif noise < 0 then
color = vec4(0.10, 0.25, (noise + 1.9) / 18, (noise + 1.9) / 2)
-- med-low elevation : passable
elseif elevation < 0 then
color = vec4(0.10, 0.25, 0.10, (elevation + 1.9) / 2)
-- passable
elseif noise < 0.5 then
color = vec4(0.25, 0.20, 0.10, (noise + 1.9) / 2)
-- med-high elevation : passable
elseif elevation< 0.5 then
color = vec4(0.25, 0.20, 0.10, (elevation + 1.9) / 2)
-- impassable
-- highest elevation : impassable
else else
color = vec4(0.10, 0.30, 0.20, (noise + 1) / 2)
color = vec4(0.10, 0.30, 0.20, (elevation + 1) / 2)
end end
-- determine cell shading mask based on map position
-- subtly shade edges of map. offset coords are easier to work with
local off = hex_to_offset(hex) local off = hex_to_offset(hex)
local mask = vec4(0, 0, 0, math.max(((off.x-23)/45)^2,
((-off.y-16)/31)^2))
local mask = vec4(0, 0, 0, math.max(((off.x - 23) / 45)^2,
((-off.y - 16) / 31)^2))
color = color - mask color = color - mask
-- determine hexagon center for drawing -- determine hexagon center for drawing
@ -210,32 +179,26 @@ function game_init()
-- prepend hexagon to screen -- prepend hexagon to screen
world:prepend(am.circle(center, 11, color, 6):tag(tostring(hex))) world:prepend(am.circle(center, 11, color, 6):tag(tostring(hex)))
-- fade in bg panel
panel"bg".color = BASE03
-- sleep
--am.wait(am.delay(0.01))
end end
-- home base -- home base
world:append(am.translate(home) world:append(am.translate(home)
^ am.rotate(0):tag"home_rotate" ^ am.rotate(0):tag"home_rotate"
^ am.circle(vec2(0), 22, MAGENTA, 5)):tag"home" ^ am.circle(vec2(0), 22, MAGENTA, 5)):tag"home"
world:append(am.scale(5) ^ am.sprite(beetle))
world:action(function() world:action(function()
world"home_rotate".angle = am.frame_time / 5 world"home_rotate".angle = am.frame_time / 5
end) end)
show_coords() -- mouse-hover events show_coords() -- mouse-hover events
keep_time() -- scoring
keep_score() -- scoring
end)) end))
win.scene = game -- make it so
win.scene = world -- make it so
end end
--[[============================================================================ --[[============================================================================
----- MAIN ----- ----- MAIN -----
============================================================================]]-- ============================================================================]]--
game_init()
render_map()
Loading…
Cancel
Save