destructible asteroids

This commit is contained in:
Donald Ball 2025-11-14 16:50:50 -05:00
parent 3502dfbdd7
commit c98ab26ac0

View file

@ -13,6 +13,7 @@ function love.load()
bullets = {}
bulletTimerLimit = 0.5
bulletTimer = bulletTimerLimit
bulletRadius = 5
asteroids = {
{
@ -53,6 +54,10 @@ function love.update(dt)
shipX = (shipX + shipSpeedX * dt) % arenaWidth
shipY = (shipY + shipSpeedY * dt) % arenaHeight
local function areCirclesIntersecting(aX, aY, aRadius, bX, bY, bRadius)
return (aX - bX) ^ 2 + (aY - bY) ^ 2 <= (aRadius + bRadius) ^ 2
end
for bulletIndex = #bullets, 1, -1 do
local bullet = bullets[bulletIndex]
bullet.timeLeft = bullet.timeLeft - dt
@ -63,6 +68,14 @@ function love.update(dt)
bullet.x = (bullet.x + math.cos(bullet.angle) * bulletSpeed * dt) % arenaWidth
bullet.y = (bullet.y + math.sin(bullet.angle) * bulletSpeed * dt) % arenaHeight
end
for asteroidIndex = #asteroids, 1, -1 do
local asteroid = asteroids[asteroidIndex]
if areCirclesIntersecting(bullet.x, bullet.y, bulletRadius, asteroid.x, asteroid.y, asteroidRadius) then
table.remove(bullets, bulletIndex)
table.remove(asteroids, asteroidIndex)
break
end
end
end
bulletTimer = bulletTimer + dt
@ -78,10 +91,6 @@ function love.update(dt)
end
end
local function areCirclesIntersecting(aX, aY, aRadius, bX, bY, bRadius)
return (aX - bX) ^ 2 + (aY - bY) ^ 2 <= (aRadius + bRadius) ^ 2
end
for asteroidIndex, asteroid in ipairs(asteroids) do
local asteroidSpeed = 20
asteroid.x = (asteroid.x + math.cos(asteroid.angle) * asteroidSpeed * dt) % arenaWidth
@ -121,13 +130,4 @@ function love.draw()
end
end
end
love.graphics.origin()
love.graphics.setColor(1, 1, 1)
love.graphics.print(table.concat({
'shipAngle:' .. shipAngle,
'shipX: ' .. shipX,
'shipSpeedX: ' .. shipSpeedX,
'a1: ' .. asteroids[1].angle
}, '\n'))
end