add bullet actor

This commit is contained in:
Donald Ball 2025-11-20 21:26:44 -05:00
parent c1c774338e
commit 583efe528c
2 changed files with 51 additions and 20 deletions

View file

@ -166,20 +166,16 @@ function love.gamepadpressed(joystick, button)
end
if button == "b" then
-- debounce, perhaps
local bullet = {
local bullet = state.actors.bullets.new({
x = state.ship.x + math.cos(state.ship.facing) * game.ship.radius,
y = state.ship.y + math.sin(state.ship.facing) * game.ship.radius,
angle = state.ship.facing,
speed = game.ship.bullet.speed,
radius = game.ship.bullet.radius,
timeLeft = game.ship.bullet.time,
sound = love.audio.newSource(game.ship.bullet.sound),
facing = state.ship.facing,
spin = 0,
}
})
phys.apply(bullet, state.ship)
table.insert(state.bullets, bullet)
bullet.sound:play()
phys.apply(state.ship, { speed = game.ship.bullet.recoil, angle = state.ship.facing + phys.tau / 2 })
end
if button == "x" then
@ -215,22 +211,11 @@ function love.draw()
love.graphics.pop()
for _, bullet in ipairs(state.bullets) do
love.graphics.setColor(0, 1, 0)
love.graphics.circle('fill', bullet.x, bullet.y, game.ship.bullet.radius)
bullet:draw()
end
for _, asteroid in ipairs(state.asteroids) do
love.graphics.setColor(1, 1, 1)
scale = 2 * asteroid.radius / math.max(asteroid.sprite:getWidth(), asteroid.sprite:getHeight())
love.graphics.draw(asteroid.sprite,
asteroid.x,
asteroid.y,
asteroid.facing,
scale,
scale,
asteroid.sprite:getWidth() / 2,
asteroid.sprite:getHeight() / 2
)
asteroid:draw()
end
end
end