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)
movePaddle(0)
spawnCities()
spawnEnemy(0.5, 0.1)
spawnEnemy(0.1, 0.3)
spawnEnemy(0.1, 0.1, false)
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
function spawnCities()
@ -107,11 +115,13 @@ function spawnExplosion(x, y)
})
end
function spawnEnemy(x, y)
function spawnEnemy(x, y, active)
table.insert(enemies, {
x = x,
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
@ -164,6 +174,20 @@ function updateMissiles(dt)
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
table.insert(missile.history, 1, {
x = missile.x,
@ -205,6 +229,16 @@ function updateExplosions(dt)
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
if explosion.remaining < explosion_duration * 0.2 then
@ -228,12 +262,23 @@ function updateExplosions(dt)
end
function updateEnemies(dt)
for _, enemy in ipairs(enemies) do
enemy.until_shoot = enemy.until_shoot - dt
if enemy.until_shoot < 0 then
enemy.until_shoot = enemy_min_shoot + math.random() * (enemy_max_shoot - enemy_min_shoot)
local target = cities[math.random(1, #cities)]
spawnMissile(enemy.x, enemy.y, target.x, target.y, 0.2)
local i = 1
while i <= #enemies do
local enemy = enemies[i]
if enemy.active then
enemy.until_shoot = enemy.until_shoot - dt
if enemy.until_shoot < 0 then
enemy.until_shoot = enemy_min_shoot + math.random() * (enemy_max_shoot - enemy_min_shoot)
local target = cities[math.random(1, #cities)]
spawnMissile(enemy.x, enemy.y, target.x, target.y, 0.2)
end
end
if not enemy.alive then
table.remove(enemies, i)
else
i = i + 1
end
end
end