diff --git a/hex.lua b/hex.lua index 443de0a..1738837 100644 --- a/hex.lua +++ b/hex.lua @@ -45,6 +45,12 @@ local HEX_DIRECTIONS = {vec2( 1 , 0), -- HEX UTILITY FUNCTIONS ------------------------------------------------------- +function hex_equals(a, b) + return a.s == b.s and a.t == b.t +end + + + local function hex_round(s, t) rs = round(s) rt = round(t) @@ -113,7 +119,7 @@ end ]] -- returns parallelogram-shaped map. -function map_parallelogram_init(layout, width, height) +function grammap_init(layout, width, height) map = {} setmetatable(map, {__index={layout=layout, width=width, @@ -128,7 +134,7 @@ function map_parallelogram_init(layout, width, height) end -- returns triangular map. -function map_triangular_init(layout, size) +function trimap_init(layout, size) map = {} setmetatable(map, {__index={layout=layout, size=size, @@ -142,7 +148,7 @@ function map_triangular_init(layout, size) end -- returns hexagonal map. length of map is radius * 2 + 1 -function map_hexagonal_init(layout, radius) +function hexmap_init(layout, radius) map = {} setmetatable(map, {__index={layout=layout, radius=radius, @@ -159,30 +165,28 @@ function map_hexagonal_init(layout, radius) end -- returns rectangular map. -function map_rectangular_init(layout, width, height) +function rectmap_init(layout, width, height) map = {} - mt = {__index = {layout = layout, width = width, height = height, - - retrieve = function(pix) - hex = pixel_to_hex(pix, layout) - print(tostring(hex)) - print(map[tostring(hex)]) - return vec2(hex.s, hex.t + math.floor(hex.s / 2)) - end, - - store = function(hex) - pix = hex_to_pixel(hex, layout) - return vec2(pix.x - math.floor(pix.y / 2), pix.y) - end}} - + mt = {__index={layout=layout, width=width, height=height, + + -- get hex in map from pixel coordinate + retrieve=function(pix) + return pixel_to_hex(pix, layout) + end, + + -- store pixel in map from hex coordinate + store=function(hex) + map[hex]=hex_to_pixel(hex - vec2(0, math.floor(hex.s/2)), layout) + end + }} + + setmetatable(map, mt) + for s = 0, width do - soffset = math.floor(s/2) - - for t = -soffset, height - soffset do - map[tostring(vec2(s, t))] = hex_to_pixel(vec2(s, t), layout) + for t = 0, height do + map.store(vec2(s, t)) end end - setmetatable(map, mt) return map end diff --git a/main.lua b/main.lua index 58333d6..612a70f 100644 --- a/main.lua +++ b/main.lua @@ -7,18 +7,21 @@ require "hex" ----- [[ DUMMY FUNCTIONS ]] ---------------------------------------------------- -function show_hex_coords(map) - test_scene:action(function() +function show_hex_coords() + gui_scene:action(function() mouse_position = vec2(win:mouse_position().x, win:mouse_position().y) hex = map.retrieve(mouse_position) - test_scene:remove("text") - test_scene:append(am.translate(win.right - 30, win.top - 10) + gui_scene:remove("text") + gui_scene:append(am.translate(win.left + 30, win.top - 10) ^ am.text(string.format("%d,%d", hex.s, hex.t))) end) end function rcolor() - return vec4(math.random(20, 80) / 100) + return vec4(math.random(20, 80) / 100, + math.random(20, 80) / 100, + math.random(20, 80) / 100, + 1) end ----- [[ BLAH BLAH LBAH ]] ----------------------------------------------- @@ -32,25 +35,48 @@ win = am.window { ----- [[ MAP RENDERING ]] ------------------------------------------------ -function game_scene(layout) - map = map_rectangular_init(layout, 45, 31) - hexagons = am.group() - - for _,hex in pairs(map) do - hexagons:append( - am.circle(hex, layout.size.x, rcolor(), 6):tag(tostring(hex))) - end - return hexagons +function map_init(layout) + map = rectmap_init(layout, 45, 31) + + map_scene:action(function() + for _,hex in pairs(map) do + if hex_equals(_, vec2(23, 16)) then + print("yay") + else + map_scene:append(am.circle(hex, layout.size.x, rcolor(), 6)) + end + end + + map_scene:append(am.rect(268, win.top, win.right, win.bottom, vec4(0.4, 0.6, 0.8, 1))) + + local coalburner = [[ + ......... + ..kkkkk.. + .k.....k. + k..wo...k + k..ooo..k + k...o...k + .k.....k. + ..kkkkk.. + ......... + ]] + + map_scene:append(am.translate(280, 200) ^ am.scale(10) ^ am.sprite(coalburner)) + + print(win.right - 268) + + return true + end) end ----- [[ MAIN ]] ----------------------------------------------------------- map = {} -game_scene = game_scene(layout_init(vec2(win.left, win.bottom))) -test_scene = am.group() +gui_scene = am.group() +map_scene = am.group(); map_init(layout_init(vec2(win.left, win.bottom))) -win.scene = am.group{test_scene, game_scene} +win.scene = am.group{map_scene, gui_scene} show_hex_coords(map)