destructible asteroids
This commit is contained in:
parent
3502dfbdd7
commit
c98ab26ac0
1 changed files with 13 additions and 13 deletions
26
main.lua
26
main.lua
|
|
@ -13,6 +13,7 @@ function love.load()
|
||||||
bullets = {}
|
bullets = {}
|
||||||
bulletTimerLimit = 0.5
|
bulletTimerLimit = 0.5
|
||||||
bulletTimer = bulletTimerLimit
|
bulletTimer = bulletTimerLimit
|
||||||
|
bulletRadius = 5
|
||||||
|
|
||||||
asteroids = {
|
asteroids = {
|
||||||
{
|
{
|
||||||
|
|
@ -53,6 +54,10 @@ function love.update(dt)
|
||||||
shipX = (shipX + shipSpeedX * dt) % arenaWidth
|
shipX = (shipX + shipSpeedX * dt) % arenaWidth
|
||||||
shipY = (shipY + shipSpeedY * dt) % arenaHeight
|
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
|
for bulletIndex = #bullets, 1, -1 do
|
||||||
local bullet = bullets[bulletIndex]
|
local bullet = bullets[bulletIndex]
|
||||||
bullet.timeLeft = bullet.timeLeft - dt
|
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.x = (bullet.x + math.cos(bullet.angle) * bulletSpeed * dt) % arenaWidth
|
||||||
bullet.y = (bullet.y + math.sin(bullet.angle) * bulletSpeed * dt) % arenaHeight
|
bullet.y = (bullet.y + math.sin(bullet.angle) * bulletSpeed * dt) % arenaHeight
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
bulletTimer = bulletTimer + dt
|
bulletTimer = bulletTimer + dt
|
||||||
|
|
@ -78,10 +91,6 @@ function love.update(dt)
|
||||||
end
|
end
|
||||||
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
|
for asteroidIndex, asteroid in ipairs(asteroids) do
|
||||||
local asteroidSpeed = 20
|
local asteroidSpeed = 20
|
||||||
asteroid.x = (asteroid.x + math.cos(asteroid.angle) * asteroidSpeed * dt) % arenaWidth
|
asteroid.x = (asteroid.x + math.cos(asteroid.angle) * asteroidSpeed * dt) % arenaWidth
|
||||||
|
|
@ -121,13 +130,4 @@ function love.draw()
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue