I suggest you do the following:
Create a variable to keep track of time
Code: Select all
-- Time since the last enemy was spawned
local fTimeSinceLastSpawn = 0
In your update loop, you'll want to check if you want to spawn more enemies. If so, you'll want to add the delta time to the variable we just created.
To do this you'll need to know how many enemies you want to spawn and the amount of enemies you've spawned already
Code: Select all
-- Maximum amount of enemies
intMaxEnemies = 1000
-- Current amount of enemies
intActiveEnemies = 0
In your love.update, do the following:
Code: Select all
if intActiveEnemies < intMaxEnemies then
fTimeSinceLastSpawn = fTimeSinceLastSpawn + dt
end
This will add the delta time to fTimeSinceLastSpawn as long as you've spawned less enemies than the maximum amount.
After that, you'll want to check if fTimeSinceLastSpawn is bigger than 2 seconds in your case. You can do this with a simple if statement:
In this if statement you want to spawn an enemy for every active wave, to do this you'll need to know how many waves are active. For my example I'll use the variable intActiveWaves.
Code: Select all
-- Amount of active waves
intActiveWaves = 1
You'll want to increment / decrement this variable when a new wave begins or when a wave ends.
Now you'll want to make a for loop to loop from 1 to intActiveWaves and spawn an enemy inside of that for loop:
Code: Select all
for i=1, intActiveWaves do
SpawnEnemy()
end
Just before you close your if statement: be sure to reset fTimeSinceLastSpawn to 0!
In the end your code would look something along the lines of this:
Code: Select all
-- Amount of active waves
intActiveWaves = 1
-- Maximum amount of enemies
intMaxEnemies = 1000
-- Current amount of enemies
intActiveEnemies = 0
-- Time since the last enemy was spawned
local fTimeSinceLastSpawn = 0
function love.update(dt)
-- Check if the enemy limit has been reached,
-- if not: add deltatime to TimeSinceLastSpawn
if intActiveEnemies < intMaxEnemies then
fTimeSinceLastSpawn = fTimeSinceLastSpawn + dt
end
-- If TimeSinceLastSpawn is bigger than 2 (at least 2 seconds have passed)
if fTimeSinceLastSpawn >= 2 then
-- Spawn an enemy for every active wave
for i=1, intActiveWaves do
SpawnEnemy()
end
-- Reset the time since last spawn to 0
fTimeSinceLastSpawn = 0
end
end
I hope this help you with your game, if you need any more help just let me know!