From 63b6193a7d6213e4c689d60742b1ec1fef7b8127 Mon Sep 17 00:00:00 2001 From: Nicholas Hayashi Date: Wed, 14 Jul 2021 22:38:44 -0400 Subject: [PATCH] remove initial tower --- main.lua | 20 ++++- src/grid.lua | 10 ++- src/tower.lua | 231 +++++++++++++++++++++++++------------------------- texture.lua | 2 +- 4 files changed, 143 insertions(+), 120 deletions(-) diff --git a/main.lua b/main.lua index 41b7381..a3cf5da 100644 --- a/main.lua +++ b/main.lua @@ -52,6 +52,7 @@ settings = am.load_state("settings", "json") or { window_height = 1050, music_volume = 0.2, sfx_volume = 0.1, + sound_on = true } math.randomseed(os.time()) @@ -235,6 +236,22 @@ function make_main_scene_toolbelt() return am.translate(pixel_offset) ^ group end +function make_sound_toggle_node(on) + local sprite + if on then + sprite = pack_texture_into_sprite(TEXTURES.SOUND_ON1, 40, 30) + else + sprite = pack_texture_into_sprite(TEXTURES.SOUND_OFF, 40, 30) + end + + return (am.translate(win.right - 30, win.top - 60) ^ sprite) +end + +function toggle_mute() + settings.sound_on = not settings.sound_on + win.scene:replace("sound-on-off-icon", make_sound_toggle_node(settings.sound_on)) +end + function main_scene(do_backdrop, do_logo) local group = am.group() @@ -268,8 +285,7 @@ function main_scene(do_backdrop, do_logo) ) group:append( - am.translate(win.right - 30, win.top - 60) - ^ pack_texture_into_sprite(TEXTURES.SOUND_ON1, 40, 30) + make_sound_toggle_node(settings.sound_on) ) if do_logo then diff --git a/src/grid.lua b/src/grid.lua index 8e1c247..2633633 100644 --- a/src/grid.lua +++ b/src/grid.lua @@ -1,7 +1,8 @@ do -- add padding, because we terraform the very outer edge and it looks ugly, so hide it - local padding = 3 + -- should be an even number to preserve a 'true' center + local padding = 4 -- the size of the grid should basically always be constant (i think), -- but different aspect ratios complicate this in an annoying way @@ -26,10 +27,10 @@ do local hhs = hex_horizontal_spacing(HEX_SIZE) local hvs = hex_vertical_spacing(HEX_SIZE) + -- number of 'spacings' on the grid == number of cells - 1 HEX_GRID_PIXEL_WIDTH = (HEX_GRID_WIDTH - 1) * hhs HEX_GRID_PIXEL_HEIGHT = (HEX_GRID_HEIGHT - 1) * hvs - -- number of 'spacings' on the grid == number of cells - 1 HEX_GRID_PIXEL_DIMENSIONS = vec2(HEX_GRID_PIXEL_WIDTH , HEX_GRID_PIXEL_HEIGHT) end @@ -213,6 +214,11 @@ function make_hex_grid_scene(map) end end + -- add the magenta diamond that represents 'home' + world:append( + am.circle(hex_to_pixel(HEX_GRID_CENTER, vec2(HEX_SIZE)), HEX_SIZE/2, COLORS.MAGENTA, 4) + ) + apply_flow_field(map, generate_flow_field(map, HEX_GRID_CENTER), world) return am.translate(WORLDSPACE_COORDINATE_OFFSET) ^ world diff --git a/src/tower.lua b/src/tower.lua index c3d1b8b..ead3dfb 100644 --- a/src/tower.lua +++ b/src/tower.lua @@ -221,121 +221,6 @@ do end end -function tower_serialize(tower) - local serialized = entity_basic_devectored_copy(tower) - - for i,h in pairs(tower.hexes) do - serialized.hexes[i] = { h.x, h.y } - end - - return am.to_json(serialized) -end - -function tower_deserialize(json_string) - local tower = entity_basic_json_parse(json_string) - - for i,h in pairs(tower.hexes) do - tower.hexes[i] = vec2(tower.hexes[i][1], tower.hexes[i][2]) - end - - tower.update = get_tower_update_function(tower.type) - tower.node = am.translate(tower.position) ^ make_tower_node(tower.type) - - return tower -end - -function towers_on_hex(hex) - local t = {} - for tower_index,tower in pairs(state.towers) do - if tower then - for _,h in pairs(tower.hexes) do - if h == hex then - table.insert(t, tower_index, tower) - break - end - end - end - end - return t -end - -function tower_on_hex(hex) - return table.find(state.towers, function(tower) - for _,h in pairs(tower.hexes) do - if h == hex then return true end - end - end) -end - -function tower_type_is_buildable_on(hex, tile, tower_type) - -- this function gets polled a lot, and sometimes with nil/false tower types - if not tower_type then return false end - - local blocking_towers = {} - local blocking_mobs = {} - local has_water = false - local has_mountain = false - local has_ground = false - - for _,h in pairs(hex_spiral_map(hex, get_tower_size(tower_type))) do - table.merge(blocking_towers, towers_on_hex(h)) - table.merge(blocking_mobs, mobs_on_hex(h)) - - local tile = hex_map_get(state.map, h) - -- this should always be true, unless it is possible to place a tower - -- where part of the tower overflows the edge of the map - if tile then - if is_water_elevation(tile.elevation) then - has_water = true - - elseif is_mountain_elevation(tile.elevation) then - has_mountain = true - - else - has_ground = true - end - end - end - - local towers_blocking = table.count(blocking_towers) ~= 0 - local mobs_blocking = table.count(blocking_mobs) ~= 0 - local blocked = mobs_blocking or towers_blocking - - if tower_type == TOWER_TYPE.GATTLER then - return not (blocked or has_water or has_mountain) - - elseif tower_type == TOWER_TYPE.HOWITZER then - return not (blocked or has_water or has_mountain) - - elseif tower_type == TOWER_TYPE.REDEYE then - return not blocked - and not has_water - and not has_ground - and has_mountain - - elseif tower_type == TOWER_TYPE.LIGHTHOUSE then - local has_water_neighbour = false - for _,h in pairs(hex_neighbours(hex)) do - local tile = hex_map_get(state.map, h) - - if tile and tile.elevation < -0.5 then - has_water_neighbour = true - break - end - end - return not blocked - and not has_mountain - and not has_water - and has_water_neighbour - - elseif tower_type == TOWER_TYPE.WALL then - return not blocked and tile_is_medium_elevation(tile) - - elseif tower_type == TOWER_TYPE.MOAT then - return not blocked and tile_is_medium_elevation(tile) - end -end - local function update_tower_redeye(tower, tower_index) if not tower.target_index then for index,mob in pairs(state.mobs) do @@ -516,6 +401,122 @@ local function get_tower_update_function(tower_type) end end +function tower_serialize(tower) + local serialized = entity_basic_devectored_copy(tower) + + for i,h in pairs(tower.hexes) do + serialized.hexes[i] = { h.x, h.y } + end + + return am.to_json(serialized) +end + +function tower_deserialize(json_string) + local tower = entity_basic_json_parse(json_string) + + for i,h in pairs(tower.hexes) do + tower.hexes[i] = vec2(tower.hexes[i][1], tower.hexes[i][2]) + end + + tower.update = get_tower_update_function(tower.type) + tower.node = am.translate(tower.position) ^ make_tower_node(tower.type) + + return tower +end + +function towers_on_hex(hex) + local t = {} + for tower_index,tower in pairs(state.towers) do + if tower then + for _,h in pairs(tower.hexes) do + if h == hex then + table.insert(t, tower_index, tower) + break + end + end + end + end + return t +end + +function tower_on_hex(hex) + return table.find(state.towers, function(tower) + for _,h in pairs(tower.hexes) do + if h == hex then return true end + end + end) +end + +function tower_type_is_buildable_on(hex, tile, tower_type) + -- this function gets polled a lot, and sometimes with nil/false tower types + if not tower_type then return false end + + local blocking_towers = {} + local blocking_mobs = {} + local has_water = false + local has_mountain = false + local has_ground = false + + for _,h in pairs(hex_spiral_map(hex, get_tower_size(tower_type))) do + table.merge(blocking_towers, towers_on_hex(h)) + table.merge(blocking_mobs, mobs_on_hex(h)) + + local tile = hex_map_get(state.map, h) + -- this should always be true, unless it is possible to place a tower + -- where part of the tower overflows the edge of the map + if tile then + if is_water_elevation(tile.elevation) then + has_water = true + + elseif is_mountain_elevation(tile.elevation) then + has_mountain = true + + else + has_ground = true + end + end + end + + local towers_blocking = table.count(blocking_towers) ~= 0 + local mobs_blocking = table.count(blocking_mobs) ~= 0 + local blocked = mobs_blocking or towers_blocking + + if tower_type == TOWER_TYPE.GATTLER then + return not (blocked or has_water or has_mountain) + + elseif tower_type == TOWER_TYPE.HOWITZER then + return not (blocked or has_water or has_mountain) + + elseif tower_type == TOWER_TYPE.REDEYE then + return not blocked + and not has_water + and not has_ground + and has_mountain + + elseif tower_type == TOWER_TYPE.LIGHTHOUSE then + local has_water_neighbour = false + for _,h in pairs(hex_neighbours(hex)) do + local tile = hex_map_get(state.map, h) + + if tile and tile.elevation < -0.5 then + has_water_neighbour = true + break + end + end + return not blocked + and not has_mountain + and not has_water + and has_water_neighbour + + elseif tower_type == TOWER_TYPE.WALL then + return not blocked and tile_is_medium_elevation(tile) + + elseif tower_type == TOWER_TYPE.MOAT then + return not blocked and tile_is_medium_elevation(tile) + end +end + + function make_and_register_tower(hex, tower_type) local tower = make_basic_entity( hex, diff --git a/texture.lua b/texture.lua index 50568cd..f1dc6a2 100644 --- a/texture.lua +++ b/texture.lua @@ -76,6 +76,6 @@ function pack_texture_into_sprite(texture, width, height, color) end if fail_count > 0 then - log("failed to load %d textures", fail_count) + log("failed to load %d texture(s)", fail_count) end