Make enemies killable

This commit is contained in:
Juhani Krekelä 2023-06-02 18:04:16 +03:00
parent c12d3f7bf0
commit cc73f371a5
1 changed files with 55 additions and 10 deletions

View File

@ -60,8 +60,16 @@ function love.load()
setScreenDimensions(width, height) setScreenDimensions(width, height)
movePaddle(0) movePaddle(0)
spawnCities() spawnCities()
spawnEnemy(0.5, 0.1) spawnEnemy(0.1, 0.1, false)
spawnEnemy(0.1, 0.3) spawnEnemy(0.2, 0.1, true)
spawnEnemy(0.3, 0.1, false)
spawnEnemy(0.4, 0.1, true)
spawnEnemy(0.5, 0.1, false)
spawnEnemy(0.6, 0.1, true)
spawnEnemy(0.7, 0.1, false)
spawnEnemy(0.8, 0.1, true)
spawnEnemy(0.9, 0.1, false)
spawnEnemy(0.1, 0.3, true)
end end
function spawnCities() function spawnCities()
@ -107,11 +115,13 @@ function spawnExplosion(x, y)
}) })
end end
function spawnEnemy(x, y) function spawnEnemy(x, y, active)
table.insert(enemies, { table.insert(enemies, {
x = x, x = x,
y = y, y = y,
until_shoot = math.random() * (enemy_max_shoot - enemy_min_shoot) active = active,
until_shoot = math.random() * (enemy_max_shoot - enemy_min_shoot),
alive = true
}) })
end end
@ -164,6 +174,20 @@ function updateMissiles(dt)
end end
end end
if missile.reflected then
for _, enemy in ipairs(enemies) do
local dx = enemy.x - missile.x
local dy = enemy.y - missile.y
local distance = math.sqrt(dx * dx + dy * dy)
if distance < enemy_radius then
spawnExplosion(missile.x, missile.y)
-- Freeze the missile in-place
missile.dx = 0
missile.dy = 0
end
end
end
if collided then if collided then
table.insert(missile.history, 1, { table.insert(missile.history, 1, {
x = missile.x, x = missile.x,
@ -205,6 +229,16 @@ function updateExplosions(dt)
end end
end end
-- Destroy enemies within range
for _, enemy in ipairs(enemies) do
local dx = enemy.x - explosion.x
local dy = enemy.y - explosion.y
local distance = math.sqrt(dx * dx + dy * dy)
if distance < explosion.radius + enemy_radius then
enemy.alive = false
end
end
explosion.remaining = explosion.remaining - dt explosion.remaining = explosion.remaining - dt
if explosion.remaining < explosion_duration * 0.2 then if explosion.remaining < explosion_duration * 0.2 then
@ -228,7 +262,11 @@ function updateExplosions(dt)
end end
function updateEnemies(dt) function updateEnemies(dt)
for _, enemy in ipairs(enemies) do local i = 1
while i <= #enemies do
local enemy = enemies[i]
if enemy.active then
enemy.until_shoot = enemy.until_shoot - dt enemy.until_shoot = enemy.until_shoot - dt
if enemy.until_shoot < 0 then if enemy.until_shoot < 0 then
enemy.until_shoot = enemy_min_shoot + math.random() * (enemy_max_shoot - enemy_min_shoot) enemy.until_shoot = enemy_min_shoot + math.random() * (enemy_max_shoot - enemy_min_shoot)
@ -236,6 +274,13 @@ function updateEnemies(dt)
spawnMissile(enemy.x, enemy.y, target.x, target.y, 0.2) spawnMissile(enemy.x, enemy.y, target.x, target.y, 0.2)
end end
end end
if not enemy.alive then
table.remove(enemies, i)
else
i = i + 1
end
end
end end
function love.update(dt) function love.update(dt)