diff --git a/hex.lua b/hex.lua index 66cf09a..f7676a2 100644 --- a/hex.lua +++ b/hex.lua @@ -58,7 +58,7 @@ function hex_round(s, t) sdelta = math.abs(rs - s) tdelta = math.abs(rt - t) - zdelta = math.abs(rz + s + t) + zdelta = math.abs(rz - (-s - t)) if sdelta > tdelta and sdelta > zdelta then rs = -rt - rz @@ -82,13 +82,10 @@ POINTY_ORIENTATION = {3.0^0.5, 3.0^0.5/2.0, 0.0, 3.0/2.0, 3.0^0.5/3.0, -1.0/3.0, 0.0, 2.0/3.0} -- layout. -function layout(size, orientation, origin, width, height, radius) - return {size = size or {11, 11}, - orientation = orientation or FLAT_ORIENTATION, - origin = origin or {0, 0}, - width = width or 45, - height = height or 31, - radius = radius or width or 6} +function layout_init(origin, size, orientation) + return {origin = origin or {0, 0}, + size = size or {11, 11}, + orientation = orientation or FLAT_ORIENTATION} end -- hex to screen @@ -115,29 +112,33 @@ function pixel_to_hex(x, y, layout) end ----- [[ MAP STORAGE & RETRIEVAL ]] -------------------------------------------- ---[[ all functions return a table of tables; a map of points - storage functions take a range of hex coordinates, and return pixel ones. - retrieval functions do the opposite. - everything except map shape is determined by layout. - pick a pair of functions based on the shape of map you want to use. - it is not advised to use a single layout instance with multiple shapes. ]] - --- returns parallelogram-shaped map. width and height are used. -function ogram_map_store(layout) + +--[[ _init functions return a table of tables; + a map of points in a chosen shape and specified layout. + the shape, as well as the layout used is stored in a metatable + for reuse. + ]] + +-- returns parallelogram-shaped map. +function map_parallelogram_init(layout, width, height) map = {} - for s = 0, layout.width do - for t = 0, layout.height do + setmetatable(map, {__index={layout=layout, shape=parallelogram}}) + + for s = 0, width do + for t = 0, height do table.insert(map, hex_to_pixel(s, t, layout)) end end return map end --- returns triangular map. radius is used as the triangle side length. -function tri_map_store(layout) +-- returns triangular map. +function map_triangular_init(layout, size) map = {} - for s = 0, layout.radius do - for t = layout.radius - s, layout.radius do + setmetatable(map, {__index={layout=layout, shape=triangular}}) + + for s = 0, size do + for t = size - s, size do table.insert(map, hex_to_pixel(s, t, layout)) end end @@ -145,11 +146,13 @@ function tri_map_store(layout) end -- returns hexagonal map. length of map is radius * 2 + 1 -function hex_map_store(layout) +function map_hexagonal_init(layout, radius) map = {} - for s = -layout.radius, layout.radius do - t1 = math.max(-layout.radius, -s - layout.radius) - t2 = math.min(layout.radius, -s + layout.radius) + setmetatable(map, {__index={layout=layout, shape=hexagonal}}) + + for s = -radius, radius do + t1 = math.max(-radius, -s - radius) + t2 = math.min(radius, -s + radius) for t = t1, t2 do table.insert(map, hex_to_pixel(s, t, layout)) @@ -158,16 +161,28 @@ function hex_map_store(layout) return map end --- returns rectangular map. width and height are used. -function rect_map_store(layout) +-- returns rectangular map. +function map_rectangular_init(layout, width, height) map = {} - for s = 0, layout.width do - soffset = math.floor(s / 2) + setmetatable(map, {__index={layout=layout, shape=rectangular}}) + + for s = 0, width do + soffset = math.floor(s/2) - for t = -soffset, layout.height - soffset do + for t = -soffset, height - soffset do table.insert(map, hex_to_pixel(s, t, layout)) end end return map end +-- places single hex into map table, if it is not already present. +function map_store(map) + +end + +-- retrieves single hex from map table, if it is present. +function map_retrieve(map) + +end + diff --git a/main.lua b/main.lua index 2a1f27a..dd28b9b 100644 --- a/main.lua +++ b/main.lua @@ -19,14 +19,7 @@ function rcolor() return vec4(math.random(20, 80) / 100) end -function show_hex_coords() - win.scene:action(function() - x, y = unpack(pixel_to_hex(win:mouse_position().x, win:mouse_position().y, layout)) - test_scene = ( - am.translate(vec2(unpack(hex_to_pixel(x, y, layout)))) - ^ am.text(string.format("%d, %d", x, y))) - end) -end +----- [[ BLAH BLAH LBAH ]] ----------------------------------------------- local win = am.window { title = "Warzone 2: Electric Boogaloo", @@ -35,34 +28,29 @@ local win = am.window { width = 1280 * 3 / 4, -- 960 height = 800 * 3 / 4} -- 600 -local layout = layout({11, 11}, - FLAT_ORIENTATION, - vec2(win.left, win.bottom), - 45, 31) +local layout = layout_init({win.left, win.bottom}) ----- [[ MAP RENDERING ]] ------------------------------------------------ function render_map(layout) - coords = rect_map_store(layout) - map = am.group() + map = map_rectangular_init(layout, 45, 31) + hexagons = am.group() - for _,v in pairs(coords) do - map:append(am.circle(vec2(unpack(v)), layout.size[1], rcolor(), 6)) + for _,v in pairs(map) do + hexagons:append(am.circle(vec2(unpack(v)), layout.size[1], rcolor(), 6)) end - return map + return hexagons end ----- [[ MAIN ]] ----------------------------------------------------------- -local game_scene = render_map(layout) -local test_scene = am.group() +local game_scene = render_map(layout) +local test_scene = am.group() win.scene = am.group{test_scene, game_scene} test_scene:action(function() x, y = unpack(pixel_to_hex(win:mouse_position().x, win:mouse_position().y, layout)) - test_scene:replace("text", - am.translate(vec2(unpack(hex_to_pixel(x, y, layout)))) - ^ am.text(string.format("%d, %d", x, y))) - am.delay(1) + test_scene:remove_all("text") + test_scene:append(am.translate(vec2(unpack(hex_to_pixel(x, y, layout)))) ^ am.text(string.format("%d, %d", x, y))) end)