diff --git a/asteroids/main.lua b/asteroids/main.lua index 3003812..660b3be 100644 --- a/asteroids/main.lua +++ b/asteroids/main.lua @@ -24,7 +24,7 @@ local function initGame() controls = { ship = { turnRate = 10, - thrustRate = 100, + maxThrust = 200, }, }, } @@ -51,7 +51,10 @@ local function initState() asteroid.angle = love.math.random() * (2 * math.pi) asteroid.stage = #game.asteroids.stages end - + local joystick = nil + if love.joystick.getJoystickCount() == 1 then + joystick = love.joystick.getJoysticks()[1] + end return { ship = { x = game.width / 2, @@ -66,6 +69,9 @@ local function initState() bulletTimer = game.ship.bullet.timerLimit, bullets = {}, asteroids = asteroids, + controls = { + joystick = joystick, + } } end @@ -88,12 +94,11 @@ function love.update(dt) end state.ship.angle = state.ship.angle % (2 * math.pi) - if love.keyboard.isDown('up') then - local shipSpeed = 100 - state.ship.speed.x = state.ship.speed.x + math.cos(state.ship.angle) * game.controls.ship.thrustRate * dt - state.ship.speed.y = state.ship.speed.y + math.sin(state.ship.angle) * game.controls.ship.thrustRate * dt + if state.controls.joystick ~= nil then + local thrust = state.controls.joystick:getGamepadAxis('triggerright') * game.controls.ship.maxThrust + state.ship.speed.x = state.ship.speed.x + math.cos(state.ship.angle) * thrust * dt + state.ship.speed.y = state.ship.speed.y + math.sin(state.ship.angle) * thrust * dt end - state.ship.x = (state.ship.x + state.ship.speed.x * dt) % game.width state.ship.y = (state.ship.y + state.ship.speed.y * dt) % game.height @@ -173,13 +178,13 @@ function love.draw() love.graphics.translate(x * game.width, y * game.height) love.graphics.setColor(0, 0, 1) love.graphics.circle('fill', state.ship.x, state.ship.y, game.ship.radius) - local shipCircleDistance = 20 + local shipCircleDistance = game.ship.radius * 2 / 3 love.graphics.setColor(0, 1, 1) love.graphics.circle( 'fill', state.ship.x + math.cos(state.ship.angle) * shipCircleDistance, state.ship.y + math.sin(state.ship.angle) * shipCircleDistance, - 5 + game.ship.radius / 6 ) for _, bullet in ipairs(state.bullets) do