From 38af0bcaa1b30585965a477941e17e80707a47f0 Mon Sep 17 00:00:00 2001 From: Donald Ball Date: Thu, 20 Nov 2025 21:37:04 -0500 Subject: [PATCH] add ship actor --- asteroids/actors.lua | 45 ++++++++++++++++++++++++++++++++++++++++++++ asteroids/main.lua | 27 ++------------------------ 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/asteroids/actors.lua b/asteroids/actors.lua index 92c199d..0720d4c 100644 --- a/asteroids/actors.lua +++ b/asteroids/actors.lua @@ -71,9 +71,54 @@ function actors.buildActors(game) love.graphics.pop() end + local ships = {} + local ships_mt = { __index = ships } + + function ships.new(ship) + local instance = { + x = game.width / 2, + y = game.height / 2, + angle = 0, + speed = 0, + spin = 0, + facing = phys.tau * 3 / 4, + radius = game.ship.radius, + } + for k, v in pairs(game.ship) do + instance[k] = v + end + for k, v in pairs(ship) do + instance[k] = v + end + setmetatable(instance, ships_mt) + return instance + end + + function ships:draw() + love.graphics.push() + love.graphics.translate(self.x, self.y) + love.graphics.setColor(1, 1, 1) + local width = self.sprite:getWidth() + local height = self.sprite:getHeight() + + local scale = 2 * game.ship.radius / math.max(width, height) + love.graphics.draw(game.ship.sprite, + 0, + 0, + (self.facing + phys.tau / 4) % phys.tau, + scale, + scale, + width / 2, + height / 2 + ) + -- TODO rocket exhaust + love.graphics.pop() + end + return { asteroids = asteroids, bullets = bullets, + ships = ships, } end diff --git a/asteroids/main.lua b/asteroids/main.lua index 9869fb5..5b77da5 100644 --- a/asteroids/main.lua +++ b/asteroids/main.lua @@ -65,15 +65,7 @@ local function initState() return { debug = true, actors = actors, - ship = { - x = game.width / 2, - y = game.height / 2, - angle = 0, - speed = 0, - spin = 0, - facing = phys.tau * 3 / 4, - radius = game.ship.radius, - }, + ship = actors.ships.new({}), bullets = {}, asteroids = asteroids, controls = { @@ -193,22 +185,7 @@ function love.draw() for x = -1, 1 do love.graphics.origin() love.graphics.translate(x * game.width, y * game.height) - - love.graphics.push() - love.graphics.translate(state.ship.x, state.ship.y) - love.graphics.setColor(1, 1, 1) - local scale = 2 * game.ship.radius / math.max(game.ship.sprite:getWidth(), game.ship.sprite:getHeight()) - love.graphics.draw(game.ship.sprite, - 0, - 0, - (state.ship.facing + phys.tau / 4) % phys.tau, - scale, - scale, - game.ship.sprite:getWidth() / 2, - game.ship.sprite:getHeight() / 2 - ) - -- TODO rocket exhaust - love.graphics.pop() + state.ship:draw() for _, bullet in ipairs(state.bullets) do bullet:draw()