Bullet actor collisions

This commit is contained in:
MaddoScientisto 2024-04-28 17:06:07 +02:00
commit 0e0a458949
5 changed files with 92 additions and 18 deletions

View file

@ -91,6 +91,8 @@ function _update()
p:update()
end
check_collisions()
--slidervalue = pgui:component("hslider",{pos=vec(190,20),value=slidervalue})
pgui:component("vstack",{stroke=true,pos=vec(0,0),color={0,18,12,0,7,6},height=0,margin=3,gap=3,contents={
@ -227,4 +229,34 @@ function render_layer(layer)
end
end
end
end
function distance(x1, y1, x2, y2)
return math.sqrt((x2 - x1)^2 + (y2 - y1)^2)
end
function collided(act, bul)
-- Calculate distance between actor and bullet
local dist = distance(act.x, act.y, bul.x, bul.y)
-- Check if distance is less than actor's half size and bullet's radius
return dist < (bul.radius + math.min(act.w, act.h) / 2)
end
function check_collisions()
for _, actor in ipairs(actors) do
if (actor.shootable) then
for _, bullet in ipairs(bullets) do
if collided(actor, bullet) --[[and not bullet.destroyed]] then
-- Destroy bullet
bullet:destroy()
-- Damage actor
actor:damage(bullet.damage)
end
end
end
end
end