Here is my enemy.lua they spawn perfectly its just they overlap & collide when chasing the player.
Code: Select all
enemy = {}
enemy.timer = 0
enemy.timerLim = math.random(3,5)
enemy.amount = math.random(1,3)
enemy.side = math.random(1,4)
function enemy.generate(dt)
enemy.timer = enemy.timer + dt
if enemy.timer > enemy.timerLim then
for i=1,enemy.amount do
if enemy.side == 1 then --left
enemy.spawn(-20,screenHeight / 2 - 25)
end
if enemy.side == 2 then --top
enemy.spawn(screenWidth / 2 - 25, -20)
end
if enemy.side == 3 then --right
enemy.spawn(screenWidth,screenHeight / 2 - 25)
end
if enemy.side == 4 then --bottom
enemy.spawn(screenWidth / 2 - 25, screenHeight)
end
enemy.side = math.random(1,4)
end
enemy.amount = math.random(1,3)
enemy.timerLim = math.random(3,5)
enemy.timer = 0
end
end
enemy.width = 20
enemy.height = 20
enemy.speed = 1500
enemy.friction = 7.5
enemy.gravity = 700
function enemy.spawn(x,y)
table.insert(enemy, { x = x, y = y, xvel = 0, yvel = 0, health = 2, width = enemy.width, height = enemy.height })
end
function enemy.draw()
for i,v in ipairs(enemy) do
love.graphics.rectangle('fill',v.x,v.y,enemy.width,enemy.height)
end
end
function enemy.physics(dt)
for i,v in ipairs(enemy) do
v.x = v.x + v.xvel * dt
v.y = v.y + v.yvel * dt
v.xvel = v.xvel * (1 - math.min(dt*enemy.friction, 1))
v.yvel = v.yvel * (1 - math.min(dt*enemy.friction, 1))
end
end
function enemy.AI(dt)
for i,v in ipairs(enemy) do
if player.x + player.width / 2 < v.x + v.width / 2 then
if v.xvel > -enemy.speed then
v.xvel = v.xvel - enemy.speed * dt
end
end
if player.x + player.width / 2 > v.x + v.width / 2 then
if v.xvel < enemy.speed then
v.xvel = v.xvel + enemy.speed * dt
end
end
if player.y + player.height / 2 < v.y + v.height / 2 then
if v.yvel > -enemy.speed then
v.yvel = v.yvel - enemy.speed * dt
end
end
if player.y + player.height / 2 > v.y + v.height / 2 then
if v.yvel < enemy.speed then
v.yvel = v.yvel + enemy.speed * dt
end
end
end
end
function enemy.bulletcollide()
for i,v in ipairs(enemy) do
for ia,va in ipairs(bullet) do
if va.x + va.width > v.x and
va.x < v.x + v.width and
va.y + va.height > v.y and
va.y < v.y + v.height then
table.remove(enemy, i)
table.remove(bullet, ia)
end
end
end
end
function enemy.collide()
for i,v in ipairs(enemy) do
if v.x < 0 then
v.x = 0
end
if v.y < 0 then
v.y = 0
end
if v.x > 777 then
v.x = 777
end
if v.y > 577 then
v.y = 577
end
end
end
function DRAW_ENEMY()
enemy.draw()
end
function UPDATE_ENEMY(dt)
enemy.physics(dt)
enemy.AI(dt)
enemy.collide()
enemy.generate(dt)
enemy.bulletcollide()
enemy.overlapping()
end