Skip to content

Commit

Permalink
improve gamepad and touch controls (fixes Switch)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneSchwettmann committed Nov 22, 2021
1 parent 317dcd6 commit e1e7f11
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 58 deletions.
8 changes: 4 additions & 4 deletions src/Draw.lua
Expand Up @@ -58,7 +58,7 @@ function love.draw()

-- draw instruction on top left
love.graphics.setColor(0,0,0,1)
love.graphics.print("Use mouse (P1), arrows+shift/joy. (P2), q quit, p pause", 10, 7)
love.graphics.print("P1 mouse/joy1, P2 arrows+shift/joy2, q quit, p pause", 10, 7)
love.graphics.print("Score: "..score, 400, 7)
love.graphics.print("Level: "..currentLevel.." of "..numLevels,500,7)
love.graphics.print("Lives: ",600,7)
Expand Down Expand Up @@ -116,12 +116,12 @@ function drawTitleScreen()
love.graphics.rectangle("fill", floor(centerX-200), floor(centerY-5-75+150), 400, 160 )
love.graphics.setColor(0,0,0,1)
verticalSpacing = 25
love.graphics.printf("1 or Click - Start 1 player game", 0, centerY-10-2.5*verticalSpacing+150,width,"center")
love.graphics.printf("2 - Start 2 player game", 0, centerY-10-1.5*verticalSpacing+150,width,"center")
love.graphics.printf("Fire(1) or 1 or Click or Touch - Start 1 player game", 0, centerY-10-2.5*verticalSpacing+150,width,"center")
love.graphics.printf("Fire(2) or 2 - Start 2 player game", 0, centerY-10-1.5*verticalSpacing+150,width,"center")
love.graphics.printf("s - Mouse Sensitivity: " ..mouseScaleX / 100, 0, centerY-10-0.5*verticalSpacing+150,width,"center")
love.graphics.printf("f - Toggle fullscreen", 0, centerY-10+0.5*verticalSpacing+150,width,"center")
love.graphics.printf("d - Toggle scaling (fullscreen only)", 0, centerY-10+1.5*verticalSpacing+150,width,"center")
love.graphics.printf("q - Quit", 0, centerY-10+2.5*verticalSpacing+150,width,"center")
love.graphics.printf("q or Select - Quit", 0, centerY-10+2.5*verticalSpacing+150,width,"center")
end
function drawPauseScreen()
Expand Down
181 changes: 127 additions & 54 deletions src/Input.lua
Expand Up @@ -3,8 +3,9 @@ function getInputPlayer(playerNumber)
if playerNumber==1 then
local mouseX, mouseY = love.mouse.getPosition()
if touching then
xInput=mouseScaleX*(mouseX-mouseXOld)
yInput=mouseScaleY*(mouseY-mouseYOld)
mouseX, mouseY = love.touch.getPosition( touchId )
xInput=200*(mouseX-mouseXOld)
yInput=200*(mouseY-mouseYOld)
mouseXOld=mouseX
mouseYOld=mouseY
else
Expand All @@ -31,58 +32,83 @@ function getInputPlayer(playerNumber)
if love.keyboard.isDown("up") and love.keyboard.isDown("down") then
yInput = 0
end
if love.keyboard.isDown("rshift") or love.keyboard.isDown("lshift") then
xInput = xInput * 2
yInput = yInput * 2
end
local joyXInput,joyYInput = 0,0
local joystick
if numJoysticks>0 then
joystick = joysticks[1]
local numButtons = joystick:getButtonCount()
if joystick:getAxisCount()>=1 then
joyXInput,joyYInput = joystick:getAxes()
joyXInput,joyYInput=joyXInput * 500,joyYInput * 500
end
if joystick:getHatCount()>=1 then
local hatDirection = joystick:getHat(1)
if hatDirection == 'u' then
joyYInput = -500
elseif hatDirection == 'd' then
joyYInput = 500
elseif hatDirection == 'l' then
joyXInput = -500
elseif hatDirection == 'r' then
joyXInput = 500
elseif hatDirection == 'ld' then
joyXInput = -500
joyYInput = 500
elseif hatDirection == 'lu' then
joyXInput = -500
joyYInput = -500
elseif hatDirection == 'rd' then
joyXInput = 500
joyYInput = 500
elseif hatDirection == 'ru' then
joyXInput = 500
joyYInput = -500
end
end
if norm(joyXInput,joyYInput) >= 0.15 * 500 then
if numButtons>0 then
if anyButtonPressed(joystick,numButtons) then
joyXInput,joyYInput=2 * joyXInput, 2 * joyYInput
end
if math.abs(joyXInput)>math.abs(xInput) then
xInput=joyXInput
end
if math.abs(joyYInput)>math.abs(yInput) then
yInput=joyYInput
end
end
end
end
end
if love.keyboard.isDown("rshift") or love.keyboard.isDown("lshift") then
xInput = xInput * 2
yInput = yInput * 2
end
end
-- Joystick input for player 1 and 2
local joyXInput,joyYInput = 0,0
local buttonInput = false
local joystick
if numJoysticks>=playerNumber then
joystick = joysticks[playerNumber]
if joystick:isGamepad() then
joyXInput = joystick:getGamepadAxis("leftx")
joyYInput = joystick:getGamepadAxis("lefty")
joyXInput,joyYInput=joyXInput * 500,joyYInput * 500
if joystick:isGamepadDown("dpup") then
joyYInput = -500
elseif joystick:isGamepadDown("dpdown") then
joyYInput = 500
end
if joystick:isGamepadDown("dpleft") then
joyXInput = -500
elseif joystick:isGamepadDown("dpright") then
joyXInput = 500
end
if joystick:isGamepadDown("a", "b", "x", "y") then
buttonInput = true
end
if joystick:isGamepadDown("back") then
love.keypressed("q")
end
else
local numButtons = joystick:getButtonCount()
if joystick:getAxisCount()>=1 then
joyXInput,joyYInput = joystick:getAxes()
joyXInput,joyYInput=joyXInput * 500,joyYInput * 500
end
if joystick:getHatCount()>=1 then
local hatDirection = joystick:getHat(1)
if hatDirection == 'u' then
joyYInput = -500
elseif hatDirection == 'd' then
joyYInput = 500
elseif hatDirection == 'l' then
joyXInput = -500
elseif hatDirection == 'r' then
joyXInput = 500
elseif hatDirection == 'ld' then
joyXInput = -500
joyYInput = 500
elseif hatDirection == 'lu' then
joyXInput = -500
joyYInput = -500
elseif hatDirection == 'rd' then
joyXInput = 500
joyYInput = 500
elseif hatDirection == 'ru' then
joyXInput = 500
joyYInput = -500
end
if anyButtonPressed(joystick,numButtons) then
buttonInput = true;
end
end
end
if norm(joyXInput,joyYInput) >= 0.15 * 500 then
if buttonInput then
joyXInput,joyYInput=2 * joyXInput, 2 * joyYInput
end
if math.abs(joyXInput)>math.abs(xInput) then
xInput=joyXInput
end
if math.abs(joyYInput)>math.abs(yInput) then
yInput=joyYInput
end
end
end
return xInput,yInput
end

Expand All @@ -104,6 +130,7 @@ function love.mousepressed(x, y, button, istouch)
end
if istouch then
touching = true
touchId = 1
else
touching = false
end
Expand All @@ -119,6 +146,52 @@ function love.mousepressed(x, y, button, istouch)
end
end

function love.touchpressed(id, x, y, dx, dy, pressure)
if gameIsPaused==false and waitingForClick then
waitingForClick=false
end
touching = true
touchId = id
mouseXOld=x
mouseYOld=y

if displayingTitleScreen then
numPlayers=1
startGame()
elseif gameLost then
titleScreen()
end
end

function love.touchreleased(id, x, y, dx, dy, pressure)
touching = false
touchId = nil
end

function love.gamepadpressed(joystick, button)
if (button ~= "dpup"
and button ~= "dpdown"
and button ~= "dpleft"
and button ~= "dpright") then
if (button == "back") then
love.keypressed('q')
elseif gameIsPaused==false and waitingForClick then
waitingForClick=false
elseif displayingTitleScreen then
for i=1,#joysticks,1 do
if joysticks[i]:getID()==joystick:getID() then
if (i==1 or i==2) then
numPlayers=i
startGame()
end
end
end
elseif gameLost then
titleScreen()
end
end
end

function love.keypressed(key, unicode)
if displayingTitleScreen then
if key == 'q' then
Expand Down

0 comments on commit e1e7f11

Please sign in to comment.