When you load/run a file in Lua, Lua looks through the whole file in a sequential manner once, so your line to check collisions only occurs upon main.lua
's loading, never to be looked at again.
As your code stands now, it only checks the collision of the enemy and bullet once
if CheckCollision(enemy.x,enemy.y,enemy.w,enemy.h,bullets.x,bullets.y,bullets.w,bullets.h) then enemy = nil
end
If you put this into the love.update(dt)
method, it will achieve your desired effect.
I'd like to note that once the enemy
is set to nil
(collision occurs), you will throw an error about attempting to index a nil value since your enemy
variable is no longer a table.
Also noteworthy, these lines
bullets.x = o.x
bullets.y = o.y
in the for loop
for i, o in ipairs(bullets) do
cause your bullets to behave improperly (at least, I assume you don't wish for the behavior they have) Each time a new bullet is fired, it is added to the bullets
table with the code
table.insert(bullets, {
x = player.x,
y = player.y,
dir = direction,
speed = 400
})
This puts each new table into the #bullets + 1
(the table's last index + 1) index of bullets
. Since your for loop iterates over each bullet object in the bullets
table, the last assignment that occurs is always on the last bullet in the table.
Let me try to explain this simpler.
Say a player fires two bullets. The first bullet firing will invoke the table.insert(...)
call I mentioned before. So, our bullets
table will look like this
bullets = {
x = 100,
y = 100, -- This is what you set player.x and player.y to in the start.
w = 15,
h = 15,
-- This is that new table we added - the 1st bullet fired.
{
-- This will all be inside it according to the table.insert(...) call.
x = 100, -- What player.x is equal to
y = 100, -- What player.y is equal to
dir = ... -- Who knows what it is, just some radians that you calculated.
speed = 400
}
}
Now, you used an ipairs(...)
call which means that our for loop will only look at the tables inside bullets
- smart thinking. But there is a problem with its implementation.
-- With our new table inside bullets, we will only have that table to look at for the entire loop. So, lets jump right into the loop implementation.
local i, o
for i, o in ipairs(bullets) do
-- This is fine. These lines look at the new table's x and y values and move them correctly.
o.x = o.x + math.cos(o.dir) * o.speed * dt
o.y = o.y + math.sin(o.dir) * o.speed * dt
-- This is the problem.
bullets.x = o.x -- Now, those x and y values in the bullets table are set to the new table's x and y values.
bullets.y = o.y
-- The rest of the loop works fine.
...
end
Now, for one new bullet, it works fine. Each update will update bullets.x
and bullets.y
correctly as the new bullet travels. But now lets considered the second bullet our player fired.
The new look of bullets
is like this
bullets = {
x = 150, -- These are equal to the first bullet's values - for now, at least.
y = 150,
w = 15,
h = 15,
-- This 1st bullet is still here.
{
x = 150, -- Lets say the bullet has moved 50 pixels in both directions.
y = 150,
dir = ...
speed = 400
},
-- This is the new second bullet.
{
x = 100, -- Remember player.x and player.y are both 100
y = 100,
dir = ...
speed = 400
}
}
See where this is going yet? Lets jump to the for loop on the first iteration.
-- First iteration occurs. We're looking at the first bullet.
for i, o in ipairs(bullets) do
o.x = o.x + math.cos(o.dir) * o.speed * dt -- Lets say o.x = 160 now
o.y = o.y + math.sin(o.dir) * o.speed * dt -- and o.y = 160
bullets.x = o.x -- bullets.x = o.x, so bullets.x = 160
bullets.y = o.y -- bullets.y = o.y, so bullets.y = 160
...
end
But then we get to the second bullet...
-- Second iteration, second bullet.
for i, o in ipairs(bullets) do
-- Since it's the new table, o.x and o.y start at 100
o.x = o.x + math.cos(o.dir) * o.speed * dt -- Lets say o.x = 110
o.y = o.y + math.sin(o.dir) * o.speed * dt -- Lets say o.y = 110 as well
bullets.x = o.x
bullets.y = o.y
-- But now our bullets.x and bullets.y have moved to the new bullet!
-- The position of the 1st bullet is completely forgotten about!
...
end
And this is the problem. The way the loop is written currently, the program only cares about the most recently fired bullet because it is going to be placed last in the bullets
table - it is checked and assigned to bullets.x
and bullets.y
last. This causes the collision check to only care if the most recently fired bullet is touching the enemy, none of the other ones.
There are two ways to fix this. Either evaluate the position of each bullet separately on collisions and add widths and heights to their tables, like this
-- When you add a bullet
table.insert(bullets, {
x = player.x,
y = player.y,
w = bullets.w,
h = bullets.h,
dir = direction,
speed = 400
})
-- When looking for collisions
local i, o
for i, o in ipairs(bullets) do
o.x = o.x + math.cos(o.dir) * o.speed * dt
o.y = o.y + math.sin(o.dir) * o.speed * dt
-- This will destroy an enemy correctly
if CheckCollision(enemy.x,enemy.y,enemy.w,enemy.h, o.x, o.y, o.w, o.h) then enemy = nil end
if (o.x < -10) or (o.x > love.graphics.getWidth() + 10)
or (o.y < -10) or (o.y > love.graphics.getHeight() + 10) then
table.remove(bullets, i)
end
end
This way you just move the collision checker's position to inside the loop and change its parameters.
The other way is to make class-like tables that can be "instantiated," whose objects' metatables point to the class-like table. Harder, but better practice and much easier to write methods and what not for. Makes generic inspections and evaluations of multiple players, enemies, bullets, etc. much easier as well.