Add enemies
This commit is contained in:
parent
9a9e57dbd2
commit
c0b0012701
|
@ -13,9 +13,14 @@ 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,8 @@ 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.2)
|
||||
spawnEnemy(0.5, 0.1)
|
||||
spawnEnemy(0.1, 0.3)
|
||||
end
|
||||
|
||||
function spawnCities()
|
||||
|
@ -103,6 +107,14 @@ function spawnExplosion(x, y)
|
|||
})
|
||||
end
|
||||
|
||||
function spawnEnemy(x, y)
|
||||
table.insert(enemies, {
|
||||
x = x,
|
||||
y = y,
|
||||
until_shoot = math.random() * (enemy_max_shoot - enemy_min_shoot)
|
||||
})
|
||||
end
|
||||
|
||||
function updateMissiles(dt)
|
||||
for _, missile in ipairs(missiles) do
|
||||
missile.x = missile.x + missile.dx * dt
|
||||
|
@ -215,11 +227,23 @@ function updateExplosions(dt)
|
|||
end
|
||||
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)
|
||||
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
|
||||
|
@ -312,10 +336,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
|
||||
|
|
Loading…
Reference in New Issue