reduce asteroid init

This commit is contained in:
Donald Ball 2025-11-20 21:07:12 -05:00
parent b2b70ee910
commit c1c774338e
2 changed files with 7 additions and 19 deletions

View file

@ -8,7 +8,8 @@ function actors.buildActors(game)
local asteroids_mt = { __index = asteroids } local asteroids_mt = { __index = asteroids }
function asteroids.new(asteroid) function asteroids.new(asteroid)
local stage = game.asteroids.stages[asteroid.stage or #game.asteroids.stages] local stageIndex = asteroid.stage or #game.asteroids.stages
local stage = game.asteroids.stages[stageIndex]
local instance = { local instance = {
angle = math.random() * phys.tau, angle = math.random() * phys.tau,
speed = stage.speed, speed = stage.speed,
@ -20,6 +21,7 @@ function actors.buildActors(game)
for k, v in pairs(asteroid) do for k, v in pairs(asteroid) do
instance[k] = v instance[k] = v
end end
instance.stage = stageIndex
setmetatable(instance, asteroids_mt) setmetatable(instance, asteroids_mt)
return instance return instance
end end

View file

@ -127,32 +127,18 @@ function love.update(dt)
if asteroid.stage > 1 then if asteroid.stage > 1 then
local angle1 = love.math.random() * phys.tau local angle1 = love.math.random() * phys.tau
local angle2 = angle1 - phys.tau / 2 local angle2 = angle1 - phys.tau / 2
local stage = game.asteroids.stages[asteroid.stage - 1] table.insert(state.asteroids, state.actors.asteroids.new({
local radius = stage.radius
local speed = stage.speed
local sprite = stage.sprite
table.insert(state.asteroids, {
x = asteroid.x, x = asteroid.x,
y = asteroid.y, y = asteroid.y,
angle = angle1, angle = angle1,
stage = asteroid.stage - 1, stage = asteroid.stage - 1,
radius = radius, }))
speed = speed, table.insert(state.asteroids, state.actors.asteroids.new({
facing = math.random() * phys.tau,
spin = 0.1 + math.random() * 0.1,
sprite = sprite,
})
table.insert(state.asteroids, {
x = asteroid.x, x = asteroid.x,
y = asteroid.y, y = asteroid.y,
angle = angle2, angle = angle2,
stage = asteroid.stage - 1, stage = asteroid.stage - 1,
radius = radius, }))
speed = speed,
facing = math.random() * phys.tau,
spin = 0.1 + math.random() * 0.1,
sprite = sprite,
})
end end
table.remove(state.asteroids, asteroidIndex) table.remove(state.asteroids, asteroidIndex)
break break