diff --git a/asteroids/actors.lua b/asteroids/actors.lua index c0ca225..ceb84c1 100644 --- a/asteroids/actors.lua +++ b/asteroids/actors.lua @@ -8,7 +8,8 @@ function actors.buildActors(game) local asteroids_mt = { __index = asteroids } 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 = { angle = math.random() * phys.tau, speed = stage.speed, @@ -20,6 +21,7 @@ function actors.buildActors(game) for k, v in pairs(asteroid) do instance[k] = v end + instance.stage = stageIndex setmetatable(instance, asteroids_mt) return instance end diff --git a/asteroids/main.lua b/asteroids/main.lua index 43c7c43..4d242be 100644 --- a/asteroids/main.lua +++ b/asteroids/main.lua @@ -127,32 +127,18 @@ function love.update(dt) if asteroid.stage > 1 then local angle1 = love.math.random() * phys.tau local angle2 = angle1 - phys.tau / 2 - local stage = game.asteroids.stages[asteroid.stage - 1] - local radius = stage.radius - local speed = stage.speed - local sprite = stage.sprite - table.insert(state.asteroids, { + table.insert(state.asteroids, state.actors.asteroids.new({ x = asteroid.x, y = asteroid.y, angle = angle1, stage = asteroid.stage - 1, - radius = radius, - speed = speed, - facing = math.random() * phys.tau, - spin = 0.1 + math.random() * 0.1, - sprite = sprite, - }) - table.insert(state.asteroids, { + })) + table.insert(state.asteroids, state.actors.asteroids.new({ x = asteroid.x, y = asteroid.y, angle = angle2, stage = asteroid.stage - 1, - radius = radius, - speed = speed, - facing = math.random() * phys.tau, - spin = 0.1 + math.random() * 0.1, - sprite = sprite, - }) + })) end table.remove(state.asteroids, asteroidIndex) break