Browse Source

remove initial tower

master
Nicholas Hayashi 3 years ago
parent
commit
63b6193a7d
  1. 20
      main.lua
  2. 10
      src/grid.lua
  3. 231
      src/tower.lua
  4. 2
      texture.lua

20
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

10
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

231
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,

2
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
Loading…
Cancel
Save