Collisions and ipairs! Eep!
Posted: Sat May 30, 2015 2:30 am
Here's a lobster-y problem that I've been trying to figure out with no luck. Can you help me? (Also, bear with me - I'm no Lua wiz... yet)
So, in my game, there are two players, Crusader Red and Crusader Blue. They can each press a key to summon a lobster. Fun!
Now using the simple collision check below, I can have these summoned lobsters collide with the other player or objects in the environment. Such as:
Now my stuggle: to add some icing to the cake, I want to have something happen when opposing player lobsters collide. I can't seem to wrap my head around it. How can I check the lobsters in ipairs(crusaderRed) vs those in ipairs(crusaderBlue). Does this make any sense?
So, in my game, there are two players, Crusader Red and Crusader Blue. They can each press a key to summon a lobster. Fun!
Code: Select all
function crusaderControl(crusaderTable, dt, up, down, left, right, shoot)
-- movement code here. blahblah...
-- The fun stuff: lobster summoning!
if love.keyboard.isDown(shoot) and crusaderTable.canShoot then
newlobster = {
x = (crusaderTable.x)+21, -- spawn from center of player
y = (crusaderTable.y)+32,
img = crusaderTable.lobsterImg,
speed = 50
}
table.insert(crusaderTable.lobsters, newlobster)
crusaderTable.canShoot = false
crusaderTable.canShootTimer = crusaderTable.canShootTimerMax
crusaderTable.lobsterCount = crusaderTable.lobsterCount + 1
end
end
Code: Select all
--collision check function, from the love wiki:
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
--Now the fun fun stuff. A lobster collides with another player.
for i, lobster in ipairs(crusaderTable.lobsters) do
--If player generated lobsters collide with other player crusader,
--the lobster is destroyed and you get 1000 pts.
if CheckCollision (lobster.x, lobster.y, lobster.img:getWidth(), lobster.img:getHeight(), theirCrusader.x, theirCrusader.y, theirCrusader.width, theirCrusader.height) then
crusaderTable.score = crusaderTable.score +1000
end
end