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

@ -1,3 +1,4 @@
local love = require("love")
local phys = require("phys")
local inspect = require("inspect")
@ -26,8 +27,53 @@ function actors.buildActors(game)
return instance
end
function asteroids:draw()
local width = self.sprite:getWidth()
local height = self.sprite:getHeight()
love.graphics.push()
love.graphics.setColor(1, 1, 1)
local scale = 2 * self.radius / math.max(width, height)
love.graphics.draw(self.sprite,
self.x,
self.y,
self.facing,
scale,
scale,
width / 2,
height / 2
)
love.graphics.pop()
end
local bullets = {}
local bullets_mt = { __index = bullets }
function bullets.new(bullet)
local instance = {
spin = 0,
}
for k, v in pairs(game.ship.bullet) do
instance[k] = v
end
for k, v in pairs(bullet) do
instance[k] = v
end
instance.sound = love.audio.newSource(game.ship.bullet.sound)
instance.timeLeft = instance.time
setmetatable(instance, bullets_mt)
return instance
end
function bullets:draw()
love.graphics.push()
love.graphics.setColor(0, 1, 0)
love.graphics.circle('fill', self.x, self.y, self.radius)
love.graphics.pop()
end
return {
asteroids = asteroids,
bullets = bullets,
}
end

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