diff --git a/asteroids/actors.lua b/asteroids/actors.lua new file mode 100644 index 0000000..c0ca225 --- /dev/null +++ b/asteroids/actors.lua @@ -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 diff --git a/asteroids/main.lua b/asteroids/main.lua index b2d0d9e..43c7c43 100644 --- a/asteroids/main.lua +++ b/asteroids/main.lua @@ -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,