Here's a first few steps in the right direction.
In your main.lua add this function under your check collision function.
Code: Select all
function isPointInAxisParallelRectangle(x, y, x1, y1, x2, y2)
return x > x1 and x < x2 and y > y1 and y < y2
end
Then add this code inside your love.update(dt) function. I added it just before your
Check Player Bullet Enemy Collision routine.
Code: Select all
for j,b in ipairs(player.bullets) do
if isPointInAxisParallelRectangle(b.x, b.y, barrier.x1, barrier.y, barrier.x1 + 44, barrier.y + 32) then
barrier.imageData = barrier.image:getData()
local x = b.x - barrier.x1
local y = b.y - barrier.y
local r, g, b, a = barrier.imageData:getPixel(x,y)
if a > 0 then
barrier.imageData:setPixel(x, y, r, g, b, 0)
barrier.image:refresh()
table.remove(player.bullets, j)
end
end
end
This will only work on your first barrier. You'll have to modify your barriers.lua file in order to store all your barriers in a table, and have each barrier have a different copy of the image file. Then you can iterate over your barriers one by one. I'm guessing you know how to do that.
What I did here is check if the player's bullet is inside the barrier bounds. The first collision check. If it is, then I get the position of the bullet (x, and y) and check to see if the alpha is not null on the barrier at that location. If it isn't null, then I count it as a hit. I then remove the bullet, and change the pixel alpha to 0 to hide the pixel that got hit.
This is really just a quick first attempt. As you'll see, it's buggy. Hopefully, it can help you find the correct way.
I'm guessing the game doesn't update fast enough, so sometimes the bullet passes through some pixels. Or the x, and y of the bullet I am getting aren't good. Not sure.
Also, you might want to remove more than one pixel when it gets hit. Perhaps you could remove more, like some around the one that got hit, etc.. Lots of refinement here. Like bigger injury to surface from bigger bullets, etc...
Anyhow, I hope this helps a bit.