diff --git a/main.lua b/main.lua index a947144..0a0e1f6 100644 --- a/main.lua +++ b/main.lua @@ -86,6 +86,7 @@ require "src/grid" require "src/mob" require "src/projectile" require "src/tower" +require "src/map-editor" local sound_toggle_node_tag = "sound-on-off-icon" @@ -194,9 +195,12 @@ function make_main_scene_toolbelt() end end }, - false and { + { texture = TEXTURES.MAP_EDITOR_HEX, - action = function() alert("not yet :)") end + action = function() + win.scene:remove("menu") + map_editor_init() + end }, include_unpause_option and { texture = TEXTURES.UNPAUSE_HEX, @@ -341,6 +345,10 @@ function main_scene(do_backdrop, do_logo) return group:tag"menu" end +function switch_scene(scene) + win.scene = am.group(scene) +end + win.scene = am.group( main_scene(true, true) ) diff --git a/src/game.lua b/src/game.lua index da5b871..bab8f67 100644 --- a/src/game.lua +++ b/src/game.lua @@ -26,7 +26,6 @@ local function get_initial_game_state(seed) world = world, -- the root scene graph node for the game 'world' ui = nil, -- unused, root scene graph node for the 'ui' stuff - perf = {}, -- result of call to am.perf_stats, called every frame time = 0, -- real time since the *current* game started in seconds score = 0, -- current game score money = STARTING_MONEY, -- current money @@ -71,7 +70,7 @@ local function get_top_right_display_text(hex, evenq, centered_evenq, display_ty str = string.format("%s %s lang %s", am.platform, am.version, am.language()) elseif display_type == TRDTS.PERF then - str = table.tostring(state.perf) + str = table.tostring(am.perf_stats()) elseif display_type == TRDTS.SEED then str = "SEED: " .. state.map.seed @@ -205,7 +204,7 @@ end local function game_action(scene) if state.score < 0 then game_end() return true end - state.perf = am.perf_stats() + local perf = am.perf_stats() state.time = state.time + am.delta_time state.score = state.score + am.delta_time diff --git a/src/grid.lua b/src/grid.lua index b9bb1a5..8c298e2 100644 --- a/src/grid.lua +++ b/src/grid.lua @@ -227,7 +227,8 @@ function random_map(seed) HEX_GRID_DIMENSIONS.x, HEX_GRID_DIMENSIONS.y, HEX_ORIENTATION.FLAT, - seed + seed, + true ) math.randomseed(map.seed) @@ -264,3 +265,28 @@ function random_map(seed) return map end +function default_map_editor_map(seed) + if seed then + return random_map(seed) + end + + -- if there's no seed then we want a map w/ all noise values = 0 + local map = hex_rectangular_map( + HEX_GRID_DIMENSIONS.x, + HEX_GRID_DIMENSIONS.y, + HEX_ORIENTATION.FLAT, + seed, + false + ) + + for i,_ in pairs(map) do + for j,noise in pairs(map[i]) do + hex_map_set(map, i, j, { + elevation = noise + }) + end + end + + return map +end + diff --git a/src/hexyz.lua b/src/hexyz.lua index 1430f6d..02a1790 100644 --- a/src/hexyz.lua +++ b/src/hexyz.lua @@ -387,7 +387,7 @@ function hex_hexagonal_map(radius, seed) end -- Returns Unordered Rectangular Map of |width| and |height| with Simplex Noise -function hex_rectangular_map(width, height, orientation, seed) +function hex_rectangular_map(width, height, orientation, seed, do_generate_noise) local orientation = orientation or HEX_DEFAULT_ORIENTATION local seed = seed or math.random(width * height) @@ -397,26 +397,32 @@ function hex_rectangular_map(width, height, orientation, seed) map[i] = {} for j = 0, height - 1 do - -- begin to calculate noise - local idelta = i / width - local jdelta = j / height - local noise = 0 - - for oct = 1, 6 do - local f = 2/3^oct - local l = 2^oct - local pos = vec2(idelta + seed * width, jdelta + seed * height) - noise = noise + f * math.simplex(pos * l) + if do_generate_noise then + -- begin to calculate noise + local idelta = i / width + local jdelta = j / height + local noise = 0 + + for oct = 1, 6 do + local f = 2/3^oct + local l = 2^oct + local pos = vec2(idelta + seed * width, jdelta + seed * height) + noise = noise + f * math.simplex(pos * l) + end + j = j - math.floor(i/2) -- this is what makes it rectangular + + hex_map_set(map, i, j, noise) + else + j = j - math.floor(i/2) -- this is what makes it rectangular + hex_map_set(map, i, j, 0) end - j = j - math.floor(i/2) -- this is what makes it rectangular - - map[i][j] = noise end end elseif orientation == HEX_ORIENTATION.POINTY then for i = 0, height - 1 do local i_offset = math.floor(i/2) for j = -i_offset, width - i_offset - 1 do + -- @TODO noise calculations hex_map_set(map, j, i, 0) end end diff --git a/src/map-editor.lua b/src/map-editor.lua new file mode 100644 index 0000000..d6e8214 --- /dev/null +++ b/src/map-editor.lua @@ -0,0 +1,58 @@ + +local map_editor_state = { + map = {}, + world = {} +} + +local function deselect_tile() + win.scene:remove("tile_select_box") +end + +function map_editor_action() + local mouse = win:mouse_position() + local hex = pixel_to_hex(mouse - WORLDSPACE_COORDINATE_OFFSET, vec2(HEX_SIZE)) + local rounded_mouse = hex_to_pixel(hex, vec2(HEX_SIZE)) + WORLDSPACE_COORDINATE_OFFSET + local evenq = hex_to_evenq(hex) + local tile = hex_map_get(map_editor_state.map, hex) + local interactable = evenq_is_in_interactable_region(evenq{ y = -evenq.y }) + + if win:mouse_pressed"left" then + deselect_tile() + + win.scene:remove("tile_select_box") + win.scene:append(( + am.translate(rounded_mouse) + ^ pack_texture_into_sprite(TEXTURES.SELECT_BOX, HEX_SIZE*2, HEX_SIZE*2, COLORS.SUNRAY) + ) + :tag"tile_select_box" + ) + end + + -- update the cursor + if not interactable then + win.scene("cursor").hidden = true + + else + win.scene("cursor").hidden = false + win.scene("cursor_translate").position2d = rounded_mouse + end +end + +function map_editor_init() + local map_editor_scene = am.group():tag"map_editor" + + map_editor_state.map = default_map_editor_map() + map_editor_state.world = make_hex_grid_scene(map_editor_state.map) + + map_editor_scene:append(map_editor_state.world) + + win.scene:remove("map_editor") + win.scene:append(map_editor_scene) + win.scene:append( + am.translate(HEX_GRID_CENTER):tag"cursor_translate" + ^ make_hex_cursor(0, COLORS.TRANSPARENT):tag"cursor" + ) + + win.scene:late_action(map_editor_action) +end +