use joystick for thrust, derive ship cockpit
This commit is contained in:
parent
ae1616299b
commit
6c1338e3c2
1 changed files with 14 additions and 9 deletions
|
|
@ -24,7 +24,7 @@ local function initGame()
|
||||||
controls = {
|
controls = {
|
||||||
ship = {
|
ship = {
|
||||||
turnRate = 10,
|
turnRate = 10,
|
||||||
thrustRate = 100,
|
maxThrust = 200,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -51,7 +51,10 @@ local function initState()
|
||||||
asteroid.angle = love.math.random() * (2 * math.pi)
|
asteroid.angle = love.math.random() * (2 * math.pi)
|
||||||
asteroid.stage = #game.asteroids.stages
|
asteroid.stage = #game.asteroids.stages
|
||||||
end
|
end
|
||||||
|
local joystick = nil
|
||||||
|
if love.joystick.getJoystickCount() == 1 then
|
||||||
|
joystick = love.joystick.getJoysticks()[1]
|
||||||
|
end
|
||||||
return {
|
return {
|
||||||
ship = {
|
ship = {
|
||||||
x = game.width / 2,
|
x = game.width / 2,
|
||||||
|
|
@ -66,6 +69,9 @@ local function initState()
|
||||||
bulletTimer = game.ship.bullet.timerLimit,
|
bulletTimer = game.ship.bullet.timerLimit,
|
||||||
bullets = {},
|
bullets = {},
|
||||||
asteroids = asteroids,
|
asteroids = asteroids,
|
||||||
|
controls = {
|
||||||
|
joystick = joystick,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -88,12 +94,11 @@ function love.update(dt)
|
||||||
end
|
end
|
||||||
state.ship.angle = state.ship.angle % (2 * math.pi)
|
state.ship.angle = state.ship.angle % (2 * math.pi)
|
||||||
|
|
||||||
if love.keyboard.isDown('up') then
|
if state.controls.joystick ~= nil then
|
||||||
local shipSpeed = 100
|
local thrust = state.controls.joystick:getGamepadAxis('triggerright') * game.controls.ship.maxThrust
|
||||||
state.ship.speed.x = state.ship.speed.x + math.cos(state.ship.angle) * game.controls.ship.thrustRate * dt
|
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) * game.controls.ship.thrustRate * dt
|
state.ship.speed.y = state.ship.speed.y + math.sin(state.ship.angle) * thrust * dt
|
||||||
end
|
end
|
||||||
|
|
||||||
state.ship.x = (state.ship.x + state.ship.speed.x * dt) % game.width
|
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
|
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.translate(x * game.width, y * game.height)
|
||||||
love.graphics.setColor(0, 0, 1)
|
love.graphics.setColor(0, 0, 1)
|
||||||
love.graphics.circle('fill', state.ship.x, state.ship.y, game.ship.radius)
|
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.setColor(0, 1, 1)
|
||||||
love.graphics.circle(
|
love.graphics.circle(
|
||||||
'fill',
|
'fill',
|
||||||
state.ship.x + math.cos(state.ship.angle) * shipCircleDistance,
|
state.ship.x + math.cos(state.ship.angle) * shipCircleDistance,
|
||||||
state.ship.y + math.sin(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
|
for _, bullet in ipairs(state.bullets) do
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue