Skip to content

Instantly share code, notes, and snippets.

@amacdougall
Created October 16, 2021 00:42
Show Gist options
  • Save amacdougall/cb3f16aae49210027dd2240173d7bb44 to your computer and use it in GitHub Desktop.
Save amacdougall/cb3f16aae49210027dd2240173d7bb44 to your computer and use it in GitHub Desktop.
Function which returns a shot-pattern thunk
-- fires bullets in a spread, with an optional delay between shots;
-- this can produce anything from shotgun blasts to elegant spirals
--
-- shot_options:
-- n: number of shots
-- arc: {from, to}, as ratios where 0.0 is up and 0.5 is down; can be > 1.0
-- interval: frames between shots
spread_shot = function(npc, create_bullet, interval, options)
-- TODO: this cooldown stuff begs to be refactored
local cooldown = interval
local elapsed = 0
local n = 0
local angle = 0.0
local arc_step = (options.arc.to - options.arc.from) / options.n
return function()
if cooldown == 0 then
n = options.n -- load new batch
angle = options.arc.from
elapsed = 0
cooldown = interval
else
cooldown -= 1
end
if n > 0 and elapsed % options.interval == 0 then
add(bullets, create_bullet(npc, angle))
angle += arc_step
n -= 1
end
elapsed += 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment