extract asteroids ctor

This commit is contained in:
Donald Ball 2025-11-20 20:58:15 -05:00
parent 21c432f583
commit b2b70ee910
2 changed files with 38 additions and 9 deletions

32
asteroids/actors.lua Normal file
View file

@ -0,0 +1,32 @@
local phys = require("phys")
local inspect = require("inspect")
local actors = {}
function actors.buildActors(game)
local asteroids = {}
local asteroids_mt = { __index = asteroids }
function asteroids.new(asteroid)
local stage = game.asteroids.stages[asteroid.stage or #game.asteroids.stages]
local instance = {
angle = math.random() * phys.tau,
speed = stage.speed,
facing = math.random() * phys.tau,
spin = (math.random() - 0.5),
radius = stage.radius,
sprite = stage.sprite,
}
for k, v in pairs(asteroid) do
instance[k] = v
end
setmetatable(instance, asteroids_mt)
return instance
end
return {
asteroids = asteroids,
}
end
return actors

View file

@ -1,6 +1,7 @@
local love = require("love")
local phys = require("phys")
local inspect = require("inspect")
local actors = require("actors")
local function initGame()
return {
@ -38,6 +39,8 @@ end
local game = initGame()
local function initState()
---@diagnostic disable-next-line: redefined-local
local actors = actors.buildActors(game)
local asteroids = {
{
x = 100,
@ -52,15 +55,8 @@ local function initState()
y = game.height - 100,
}
}
for _, asteroid in ipairs(asteroids) do
asteroid.angle = love.math.random() * phys.tau
asteroid.stage = #game.asteroids.stages
local stage = game.asteroids.stages[asteroid.stage]
asteroid.speed = stage.speed
asteroid.facing = math.random() * phys.tau
asteroid.spin = 0.1 + math.random() * 0.1
asteroid.radius = stage.radius
asteroid.sprite = stage.sprite
for i, template in ipairs(asteroids) do
asteroids[i] = actors.asteroids.new(template)
end
local joystick = nil
if love.joystick.getJoystickCount() == 1 then
@ -68,6 +64,7 @@ local function initState()
end
return {
debug = true,
actors = actors,
ship = {
x = game.width / 2,
y = game.height / 2,