gamepad fire

This commit is contained in:
Donald Ball 2025-11-15 16:42:02 -05:00
parent 18062e9ca1
commit 1b1ed94b2f

View file

@ -7,7 +7,6 @@ local function initGame()
ship = { ship = {
radius = 30, radius = 30,
bullet = { bullet = {
timerLimit = 0.5,
radius = 5, radius = 5,
speed = 500, speed = 500,
time = 4, time = 4,
@ -65,8 +64,6 @@ local function initState()
y = 0, y = 0,
} }
}, },
-- the time after which the ship can fire another bullet
bulletTimer = game.ship.bullet.timerLimit,
bullets = {}, bullets = {},
asteroids = asteroids, asteroids = asteroids,
controls = { controls = {
@ -136,19 +133,6 @@ function love.update(dt)
end end
end end
state.bulletTimer = state.bulletTimer + dt
if love.keyboard.isDown('s') then
if state.bulletTimer >= game.ship.bullet.timerLimit then
state.bulletTimer = 0
table.insert(state.bullets, {
x = state.ship.x + math.cos(state.ship.angle) * game.ship.radius,
y = state.ship.y + math.sin(state.ship.angle) * game.ship.radius,
angle = state.ship.angle,
timeLeft = game.ship.bullet.time,
})
end
end
for _, asteroid in ipairs(state.asteroids) do for _, asteroid in ipairs(state.asteroids) do
asteroid.x = (asteroid.x + math.cos(asteroid.angle) * game.asteroids.stages[asteroid.stage].speed * dt) % asteroid.x = (asteroid.x + math.cos(asteroid.angle) * game.asteroids.stages[asteroid.stage].speed * dt) %
game.width game.width
@ -166,6 +150,21 @@ function love.update(dt)
end end
end end
function love.gamepadpressed(joystick, button)
if joystick ~= state.controls.joystick then
return
end
if button == "b" then
-- debounce, perhaps
table.insert(state.bullets, {
x = state.ship.x + math.cos(state.ship.angle) * game.ship.radius,
y = state.ship.y + math.sin(state.ship.angle) * game.ship.radius,
angle = state.ship.angle,
timeLeft = game.ship.bullet.time,
})
end
end
function love.draw() function love.draw()
for y = -1, 1 do for y = -1, 1 do
for x = -1, 1 do for x = -1, 1 do