From 8d3e05270a11a454e1f35fbdd593502d2ca21397 Mon Sep 17 00:00:00 2001 From: Nicholas Hayashi Date: Mon, 11 Jan 2021 09:24:55 -0500 Subject: [PATCH] day night cycle --- main.lua | 15 +++++++++++---- sound.lua | 1 + src/grid.lua | 8 ++++++++ src/mob.lua | 27 ++++++++++++++------------- src/tower.lua | 32 +++++++++++++++++--------------- 5 files changed, 51 insertions(+), 32 deletions(-) diff --git a/main.lua b/main.lua index a155802..0942906 100644 --- a/main.lua +++ b/main.lua @@ -67,9 +67,9 @@ local TRDT = TRDTS.SEED local function game_action(scene) if SCORE < 0 then game_end() end - TIME = TIME + am.delta_time - SCORE = SCORE + am.delta_time - MOUSE = WIN:mouse_position() + TIME = TIME + am.delta_time + SCORE = SCORE + am.delta_time + MOUSE = WIN:mouse_position() PERF_STATS = am.perf_stats() local hex = pixel_to_hex(MOUSE - WORLDSPACE_COORDINATE_OFFSET) @@ -85,7 +85,7 @@ local function game_action(scene) if WIN:mouse_pressed"left" then if hot and is_buildable(hex, tile, nil) then - make_and_register_tower(hex, SELECTED_TOWER_TYPE) + build_tower(hex, SELECTED_TOWER_TYPE) end end @@ -141,6 +141,13 @@ local function game_action(scene) end WIN.scene"coords".text = str end + + do_day_night_cycle() +end + +function do_day_night_cycle() + local tstep = (math.sin(TIME) / PERF_STATS.avg_fps + 1)/8 + WORLD"negative_mask".color = vec4(tstep) end function game_end() diff --git a/sound.lua b/sound.lua index 3bacc2c..556262c 100644 --- a/sound.lua +++ b/sound.lua @@ -4,6 +4,7 @@ SOUNDS = { EXPLOSION1 = 49179102, -- this slowed sounds metal as fuck EXPLOSION2 = 19725402, EXPLOSION3 = 69338002, + EXPLOSION4 = 92224102, HIT1 = 25811004, LASER1 = 79859301, LASER2 = 86914201, diff --git a/src/grid.lua b/src/grid.lua index 81a4828..23370cf 100644 --- a/src/grid.lua +++ b/src/grid.lua @@ -70,6 +70,7 @@ end function grid_cost(map, from, to) local t1, t2 = map.get(from.x, from.y), map.get(to.x, to.y) + --local base_cost = math.abs(t1.elevation) * 10 return math.abs(10 * math.abs(t1.elevation)^0.5 - 10 * math.abs(t2.elevation)^0.5) end @@ -112,6 +113,13 @@ function random_map(seed) node = node }) + getmetatable(map).__index.neighbours = function(hex) + return table.filter(hex_neighbours(hex), function(_hex) + local tile = map.get(_hex.x, _hex.y) + return tile and tile.elevation > -0.5 and tile.elevation <= 0.5 + end) + end + world:append(node) end end diff --git a/src/mob.lua b/src/mob.lua index e63d4c3..c196af6 100644 --- a/src/mob.lua +++ b/src/mob.lua @@ -33,7 +33,7 @@ end -- check if a the tile at |hex| is passable by |mob| function mob_can_pass_through(mob, hex) local tile = HEX_MAP.get(hex.x, hex.y) - return tile and tile.elevation < 0.5 and tile.elevation > -0.5 + return tile and tile.elevation <= 0.5 and tile.elevation > -0.5 end function mob_die(mob, mob_index) @@ -49,15 +49,6 @@ function do_hit_mob(mob, damage, mob_index) end end -function check_for_broken_mob_pathing(hex) - for _,mob in pairs(MOBS) do - --if mob and mob.path[hex.x] and mob.path[hex.x][hex.y] then - --mob.path = get_mob_path(mob, HEX_MAP, mob.hex, HEX_GRID_CENTER) - --end - end -end - - -- @TODO performance. -- try reducing map size by identifying key nodes (inflection points) -- there are performance hits everytime we spawn a mob and it's Astar's fault @@ -99,11 +90,21 @@ local function mob_update(mob, mob_index) end local frame_target = mob.path[mob.hex.x] and mob.path[mob.hex.x][mob.hex.y] + -- frame_target will be false when we are one hex away from the center, + -- or nil if something went wrong + if frame_target == false then + frame_target = { hex = HEX_GRID_CENTER, priority = 0 } - if frame_target --[[and mob_can_pass_through(mob, frame_target.hex)]] then - local factor = 1 + mob.speed * (1/frame_target.priority) / PERF_STATS.avg_fps - mob.position = mob.position + math.normalize(hex_to_pixel(frame_target.hex) - mob.position) * factor + elseif frame_target == nil then + log("bad") + + elseif mob_can_pass_through(mob, frame_target.hex) then + -- this is supposed to achieve frame rate independence, but i have no idea if it actually does + local rate = 1 + mob.speed * (1/frame_target.priority) / PERF_STATS.avg_fps + + mob.position = mob.position + math.normalize(hex_to_pixel(frame_target.hex) - mob.position) * rate mob.node.position2d = mob.position + else mob.path = get_mob_path(mob, HEX_MAP, mob.hex, HEX_GRID_CENTER) end diff --git a/src/tower.lua b/src/tower.lua index 7eea041..4ca658e 100644 --- a/src/tower.lua +++ b/src/tower.lua @@ -1,9 +1,18 @@ +--[[ +tower(entity) structure: +{ + -- @NOTE these should probably be wrapped in a 'weapon' struct or something, so towers can have multiple weapons + range - number - distance it can shoot + last_shot_time - number - timestamp (seconds) of last time it shot + target_index - number - index of entity it is currently shooting +} +--]] TOWER_TYPE = { - REDEYE = 1, - WALL = 2, - MOAT = 3, + REDEYE = 1, + WALL = 2, + MOAT = 3, } function get_tower_texture(tower_type) @@ -41,17 +50,6 @@ local function make_tower_sprite(tower_type) end end - - ---[[ -tower(entity) structure: -{ - -- @NOTE these should probably be wrapped in a 'weapon' struct or something, so towers can have multiple weapons - range - number - distance it can shoot - last_shot_time - number - timestamp (seconds) of last time it shot - target_index - number - index of entity it is currently shooting -} ---]] function is_buildable(hex, tile, tower) local blocked = #mobs_on_hex(hex) ~= 0 return not blocked and tile.elevation <= 0.5 and tile.elevation > -0.5 @@ -102,8 +100,12 @@ function make_and_register_tower(hex, tower_type) -- make this cell impassable HEX_MAP[hex.x][hex.y].elevation = 2 - check_for_broken_mob_pathing(hex) register_entity(TOWERS, tower) end +function build_tower(hex, tower_type) + make_and_register_tower(hex, tower_type) + WIN.scene:action(am.play(am.sfxr_synth(SOUNDS.EXPLOSION4))) +end +