asteroid stages
This commit is contained in:
parent
78cd6176a8
commit
73cae82662
1 changed files with 68 additions and 38 deletions
106
main.lua
106
main.lua
|
|
@ -3,36 +3,59 @@ local love = require("love")
|
|||
function love.load()
|
||||
arenaWidth = 800
|
||||
arenaHeight = 600
|
||||
shipX = arenaWidth / 2
|
||||
shipY = arenaHeight / 2
|
||||
shipAngle = 0
|
||||
shipSpeedX = 0
|
||||
shipSpeedY = 0
|
||||
shipRadius = 30
|
||||
|
||||
bullets = {}
|
||||
bulletTimerLimit = 0.5
|
||||
bulletTimer = bulletTimerLimit
|
||||
bulletRadius = 5
|
||||
|
||||
asteroids = {
|
||||
asteroidStages = {
|
||||
{
|
||||
x = 100,
|
||||
y = 100,
|
||||
speed = 120,
|
||||
radius = 15,
|
||||
},
|
||||
{
|
||||
x = arenaWidth - 100,
|
||||
y = 100,
|
||||
speed = 70,
|
||||
radius = 30,
|
||||
},
|
||||
{
|
||||
x = arenaWidth / 2,
|
||||
y = arenaHeight - 100,
|
||||
}
|
||||
speed = 50,
|
||||
radius = 50,
|
||||
},
|
||||
{
|
||||
speed = 20,
|
||||
radius = 80,
|
||||
},
|
||||
}
|
||||
for asteroidIndex, asteroid in ipairs(asteroids) do
|
||||
asteroid.angle = love.math.random() * (2 * math.pi)
|
||||
|
||||
function reset()
|
||||
shipX = arenaWidth / 2
|
||||
shipY = arenaHeight / 2
|
||||
shipAngle = 0
|
||||
shipSpeedX = 0
|
||||
shipSpeedY = 0
|
||||
bullets = {}
|
||||
bulletTimer = bulletTimerLimit
|
||||
asteroids = {
|
||||
{
|
||||
x = 100,
|
||||
y = 100,
|
||||
},
|
||||
{
|
||||
x = arenaWidth - 100,
|
||||
y = 100,
|
||||
},
|
||||
{
|
||||
x = arenaWidth / 2,
|
||||
y = arenaHeight - 100,
|
||||
}
|
||||
}
|
||||
for asteroidIndex, asteroid in ipairs(asteroids) do
|
||||
asteroid.angle = love.math.random() * (2 * math.pi)
|
||||
asteroid.stage = #asteroidStages
|
||||
end
|
||||
end
|
||||
asteroidRadius = 80
|
||||
|
||||
reset()
|
||||
end
|
||||
|
||||
function love.update(dt)
|
||||
|
|
@ -70,20 +93,24 @@ function love.update(dt)
|
|||
end
|
||||
for asteroidIndex = #asteroids, 1, -1 do
|
||||
local asteroid = asteroids[asteroidIndex]
|
||||
if areCirclesIntersecting(bullet.x, bullet.y, bulletRadius, asteroid.x, asteroid.y, asteroidRadius) then
|
||||
if areCirclesIntersecting(bullet.x, bullet.y, bulletRadius, asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius) then
|
||||
table.remove(bullets, bulletIndex)
|
||||
local angle1 = love.math.random() * (2 * math.pi)
|
||||
local angle2 = (angle1 - math.pi) % (2 * math.pi)
|
||||
table.insert(asteroids, {
|
||||
x = asteroid.x,
|
||||
y = asteroid.y,
|
||||
angle = angle1,
|
||||
})
|
||||
table.insert(asteroids, {
|
||||
x = asteroid.x,
|
||||
y = asteroid.y,
|
||||
angle = angle2,
|
||||
})
|
||||
if asteroid.stage > 1 then
|
||||
local angle1 = love.math.random() * (2 * math.pi)
|
||||
local angle2 = (angle1 - math.pi) % (2 * math.pi)
|
||||
table.insert(asteroids, {
|
||||
x = asteroid.x,
|
||||
y = asteroid.y,
|
||||
angle = angle1,
|
||||
stage = asteroid.stage - 1,
|
||||
})
|
||||
table.insert(asteroids, {
|
||||
x = asteroid.x,
|
||||
y = asteroid.y,
|
||||
angle = angle2,
|
||||
stage = asteroid.stage - 1,
|
||||
})
|
||||
end
|
||||
table.remove(asteroids, asteroidIndex)
|
||||
break
|
||||
end
|
||||
|
|
@ -104,15 +131,18 @@ function love.update(dt)
|
|||
end
|
||||
|
||||
for asteroidIndex, asteroid in ipairs(asteroids) do
|
||||
local asteroidSpeed = 20
|
||||
asteroid.x = (asteroid.x + math.cos(asteroid.angle) * asteroidSpeed * dt) % arenaWidth
|
||||
asteroid.y = (asteroid.y + math.sin(asteroid.angle) * asteroidSpeed * dt) % arenaHeight
|
||||
asteroid.x = (asteroid.x + math.cos(asteroid.angle) * asteroidStages[asteroid.stage].speed * dt) % arenaWidth
|
||||
asteroid.y = (asteroid.y + math.sin(asteroid.angle) * asteroidStages[asteroid.stage].speed * dt) % arenaHeight
|
||||
|
||||
if areCirclesIntersecting(shipX, shipY, shipRadius, asteroid.x, asteroid.y, asteroidRadius) then
|
||||
love.load()
|
||||
if areCirclesIntersecting(shipX, shipY, shipRadius, asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius) then
|
||||
reset()
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if #asteroids == 0 then
|
||||
reset()
|
||||
end
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
|
|
@ -133,12 +163,12 @@ function love.draw()
|
|||
|
||||
for bulletIndex, bullet in ipairs(bullets) do
|
||||
love.graphics.setColor(0, 1, 0)
|
||||
love.graphics.circle('fill', bullet.x, bullet.y, 5)
|
||||
love.graphics.circle('fill', bullet.x, bullet.y, bulletRadius)
|
||||
end
|
||||
|
||||
for asteroidIndex, asteroid in ipairs(asteroids) do
|
||||
love.graphics.setColor(1, 1, 0)
|
||||
love.graphics.circle('fill', asteroid.x, asteroid.y, asteroidRadius)
|
||||
love.graphics.circle('fill', asteroid.x, asteroid.y, asteroidStages[asteroid.stage].radius)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue