2016-11-14 00:35:14 +01:00
|
|
|
local tools = require 'assets/scripts/tools'
|
|
|
|
|
|
|
|
local asteroids = {}
|
2016-11-26 18:26:48 +01:00
|
|
|
local imgs = nil
|
2016-11-14 00:35:14 +01:00
|
|
|
|
|
|
|
function asteroids.load(game)
|
2016-11-26 18:26:48 +01:00
|
|
|
imgs = {
|
|
|
|
love.graphics.newImage('assets/sprites/asteroids/asteroid01.png'),
|
|
|
|
love.graphics.newImage('assets/sprites/asteroids/asteroid02.png'),
|
|
|
|
love.graphics.newImage('assets/sprites/asteroids/asteroid03.png'),
|
|
|
|
love.graphics.newImage('assets/sprites/asteroids/asteroid04.png')
|
2016-11-14 00:35:14 +01:00
|
|
|
}
|
2016-11-26 18:26:48 +01:00
|
|
|
asteroids.num = game.level * 5
|
|
|
|
asteroids.max_speed = 5
|
2016-11-14 00:35:14 +01:00
|
|
|
-- Generate asteroids
|
|
|
|
asteroids.bodys = {}
|
2016-11-26 18:26:48 +01:00
|
|
|
for i = 1, asteroids.num do
|
|
|
|
make_asteroid(i, game, true)
|
2016-11-14 00:35:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function asteroids.update(dt, game)
|
|
|
|
-- Rotate asteroids
|
|
|
|
for key, asteroid in pairs(asteroids.bodys) do
|
2016-11-26 18:26:48 +01:00
|
|
|
asteroid.angle = asteroid.angle + (dt * math.pi / 10)
|
|
|
|
asteroid.x = asteroid.x - asteroid.speed
|
2016-11-14 00:35:14 +01:00
|
|
|
end
|
|
|
|
-- Destroy asteroids
|
|
|
|
for key, asteroid in pairs(asteroids.bodys) do
|
2016-11-26 18:26:48 +01:00
|
|
|
if asteroid.x + asteroid.img:getWidth() < 0 then
|
|
|
|
table.remove(asteroids.bodys, key)
|
2016-11-14 00:35:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- Create asteroids
|
|
|
|
if tools.table_length(asteroids.bodys) < asteroids.num then
|
2016-11-26 18:26:48 +01:00
|
|
|
make_asteroid(tools.table_length(asteroids.bodys) + 1, game, false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function asteroids.draw()
|
|
|
|
for key, asteroid in pairs(asteroids.bodys) do
|
|
|
|
love.graphics.draw(asteroid.img, asteroid.x, asteroid.y, asteroid.angle, 1, 1, asteroid.img:getWidth() / 2, asteroid.img:getHeight() / 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function make_asteroid(pos, game, x_random)
|
|
|
|
local temp_img = imgs[math.random(1, tools.table_length(imgs))]
|
|
|
|
asteroids.bodys[pos] = {
|
2016-11-14 00:35:14 +01:00
|
|
|
x = game.canvas.width + temp_img:getWidth(),
|
2016-11-26 18:26:48 +01:00
|
|
|
y = math.random(game.window.height / 2, game.canvas.height - temp_img:getHeight()),
|
|
|
|
speed = math.random(1, asteroids.max_speed),
|
2016-11-14 00:35:14 +01:00
|
|
|
img = temp_img,
|
2016-11-26 18:26:48 +01:00
|
|
|
angle = math.random(0, 90)
|
|
|
|
}
|
|
|
|
if x_random then
|
|
|
|
asteroids.bodys[pos].x = math.random(0, game.canvas.width - temp_img:getWidth())
|
2016-11-14 00:35:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return asteroids
|