add ship actor

This commit is contained in:
Donald Ball 2025-11-20 21:37:04 -05:00
parent 583efe528c
commit 38af0bcaa1
2 changed files with 47 additions and 25 deletions

View file

@ -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

View file

@ -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()