Compare commits

...

5 Commits

Author SHA1 Message Date
Juhani Krekelä 891e37fa4f Allow exploding all reflected missiles 2023-06-02 18:08:33 +03:00
Juhani Krekelä cc73f371a5 Make enemies killable 2023-06-02 18:04:16 +03:00
Juhani Krekelä c12d3f7bf0 Fix location calculation for explosions destroying things 2023-06-02 18:02:22 +03:00
Juhani Krekelä c0b0012701 Add enemies 2023-06-02 17:46:01 +03:00
Juhani Krekelä 9a9e57dbd2 Increase trail length 2023-06-02 17:26:41 +03:00
1 changed files with 100 additions and 8 deletions

View File

@ -7,15 +7,20 @@ local wall_thickness = 0.01
local missiles = {}
local missile_radius = 0.005
local missile_trail_length = 7
local missile_trail_length = 15
local cities = {}
local city_radius = 0.05
local explosions = {}
local explosion_radius = 0.05
local explosion_radius = 0.08
local explosion_duration = 0.4
local enemies = {}
local enemy_radius = 0.025
local enemy_min_shoot = 5
local enemy_max_shoot = 10
local window_width = nil
local window_height = nil
local viewport_x_offset = nil
@ -55,9 +60,16 @@ function love.load()
setScreenDimensions(width, height)
movePaddle(0)
spawnCities()
spawnMissile(0.5, 0.1, cities[3].x, cities[3].y, 0.2)
spawnMissile(0.1, 0.3, cities[7].x, cities[7].y, 0.2)
spawnMissile(0.1, 0.3, cities[3].x, cities[3].y, 0.1)
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()
@ -103,6 +115,16 @@ function spawnExplosion(x, y)
})
end
function spawnEnemy(x, y, active)
table.insert(enemies, {
x = x,
y = y,
active = active,
until_shoot = math.random() * (enemy_max_shoot - enemy_min_shoot),
alive = true
})
end
function updateMissiles(dt)
for _, missile in ipairs(missiles) do
missile.x = missile.x + missile.dx * dt
@ -152,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,
@ -186,20 +222,30 @@ function updateExplosions(dt)
-- Destroy missiles within range
for _, missile in ipairs(missiles) do
local dx = missile.x - explosion.x
local dy = missile.x - explosion.x
local dy = missile.y - explosion.y
local distance = math.sqrt(dx * dx + dy * dy)
if distance < explosion.radius then
missile.alive = false
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
-- Destroy cities within range
for _, city in ipairs(cities) do
local dx = city.x - explosion.x
local dy = city.x - explosion.x
local dy = city.y - explosion.y
local distance = math.sqrt(dx * dx + dy * dy)
if distance < explosion.radius + city_radius then
city.alive = false
@ -215,15 +261,49 @@ function updateExplosions(dt)
end
end
function updateEnemies(dt)
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
function love.update(dt)
updateMissiles(dt)
updateExplosions(dt)
updateEnemies(dt)
if #explosions == 0 and #missiles == 0 then
if #explosions == 0 and #missiles == 0 and #enemies == 0 then
love.event.quit()
end
end
function explodeAllReflected()
for _, missile in ipairs(missiles) do
if missile.reflected then
spawnExplosion(missile.x, missile.y)
-- Freeze the missile
missile.dx = 0
missile.dy = 0
end
end
end
function movePaddle(screen_x)
paddle_x = fromScreenCoordinate(screen_x, 0)
paddle_x = math.max(paddle_x, paddle_width/2 + wall_thickness)
@ -233,6 +313,8 @@ end
love.mousemoved = movePaddle
love.mousepressed = explodeAllReflected
love.resize = setScreenDimensions
function drawWalls()
@ -312,10 +394,20 @@ function drawExplosions()
end
end
function drawEnemies()
for _, enemy in ipairs(enemies) do
love.graphics.setColor(0.7, 0.5, 1)
local x, y = toScreenCoordinates(enemy.x, enemy.y)
local radius = toScreenSize(enemy_radius)
love.graphics.circle('fill', x, y, radius)
end
end
function love.draw()
drawCities()
drawWalls()
drawMissiles()
drawPaddle()
drawEnemies()
drawExplosions()
end