You'll want to loop over some rows and columns, which place an enemy into a table. That way you can loop over the table easily;
One way to setup a grid of enemies is something like this;
Code: Select all
function love.load()
enemies = {}
enemies.w = 50 -- width of enemy
enemies.h = 30 -- height of enemy
enemies.rows = 4 -- total enemies per row
enemies.columns = 3 -- total enemies per column
enemies.gap = 10 -- space between each enemy
local x = 1
local y = 1
local wave = {}
-- calculate the total size (width/height) of the grid of enemies when populated
wave.w = enemies.rows*(enemies.h+enemies.gap)
wave.h = enemies.columns*(enemies.w+enemies.gap)
-- loop over the columns one at a time (top to bottom)
for y = 1, wave.w, enemies.h+enemies.gap do
-- loop over the rows one at a time (left to right)
for x = 1, wave.h, enemies.w+enemies.gap do
-- add an enemy
table.insert(enemies, {
x = x + love.graphics.getWidth()/2-wave.w/2, -- center the enemies relative position on screen
y = y + enemies.gap, -- add a gap from top of the screen
})
-- move to the next column
x = x + enemies.w + enemies.gap
end
-- move to the next row
y = y + enemies.h
end
end
That way, all your enemies exist in
enemies table, and can be looped over easily like so, which is helpful for everything from collision detection or even drawing:
Code: Select all
function love.draw()
-- draw the enemies by looping over the table
love.graphics.setColor(1,0,0,1)
for i, e in ipairs(enemies) do
love.graphics.rectangle("fill", e.x,e.y,enemies.w,enemies.h)
end
end
Or checking collision, etc:
Code: Select all
function love.update(dt)
for i,e in ipairs(enemies) do
checkcollision(e, missile)
end
end