Skip to content

Commit

Permalink
Merge pull request #20 from Noahkiq/master
Browse files Browse the repository at this point in the history
controller support
  • Loading branch information
Stabyourself authored Sep 12, 2018
2 parents 766ac80 + 4605100 commit 46c4736
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 18 deletions.
110 changes: 98 additions & 12 deletions game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3876,35 +3876,32 @@ function upkey(i)
end

function checkkey(s)
if s[1] == "joy" then
local jss = love.joystick.getJoysticks()
local js = jss[s[2]]

if not js then
return
end
local joysticks = love.joystick.getJoysticks()

if s[3] == "hat" then
if js:getHat(s[4]) == s[5] then
if s[1] == "joy" then
if joysticks[s[2]] == nil then
return false
elseif s[3] == "hat" then
if joysticks[s[2]]:getHat(s[4]) == s[5] then
return true
else
return false
end
elseif s[3] == "but" then
if js:isDown(s[4]) then
if joysticks[s[2]]:isDown(s[4]) then
return true
else
return false
end
elseif s[3] == "axe" then
if s[5] == "pos" then
if js:getAxis(s[4]) > joystickdeadzone then
if joysticks[s[2]]:getAxis(s[4]) > joystickdeadzone then
return true
else
return false
end
else
if js:getAxis(s[4]) < -joystickdeadzone then
if joysticks[s[2]]:getAxis(s[4]) < -joystickdeadzone then
return true
else
return false
Expand Down Expand Up @@ -3994,6 +3991,95 @@ function game_joystickreleased( joystick, button )
end
end

function game_joystickaxis(joystick, axis, value, stickmoved, shouldermoved)
if pausemenuopen then
return
end
if endpressbutton then
endgame()
return
end
for i = 1, players do
if not noupdate and objects["player"][i].controlsenabled then
local s1 = controls[i]["left"]
local s2 = controls[i]["right"]
if s1[1] == "joy" and joystick == s1[2] and s1[3] == "axe" and s1[4] == axis and stickmoved and value < 0 then
objects["player"][i]:leftkey()
return
elseif s2[1] == "joy" and joystick == s2[2] and s2[3] == "axe" and s2[4] == axis and stickmoved and value > 0 then
objects["player"][i]:rightkey()
return
end

if i ~= mouseowner then
local s = controls[i]["portal1"]
if s and s[1] == "joy" then
if s[3] == "axe" and s[4] == axis then
if joystick == s[2] and shouldermoved then
shootportal(i, 1, objects["player"][i].x+6/16, objects["player"][i].y+6/16, objects["player"][i].pointingangle)
return
end
end
end

local s = controls[i]["portal2"]
if s and s[1] == "joy" then
if s[3] == "axe" and s[4] == axis then
if joystick == s[2] and shouldermoved then
shootportal(i, 2, objects["player"][i].x+6/16, objects["player"][i].y+6/16, objects["player"][i].pointingangle)
return
end
end
end
end
end
end
end
function game_joystickhat(joystick, hat, direction)
if pausemenuopen then
return
end
if endpressbutton then
endgame()
return
end
for i = 1, players do
if not noupdate and objects["player"][i].controlsenabled then
local s1 = controls[i]["left"]
local s2 = controls[i]["right"]
if s1[1] == "joy" and joystick == s1[2] and s1[3] == "hat" and s1[4] == hat and s1[5]:find(direction) then
objects["player"][i]:leftkey()
return
elseif s2[1] == "joy" and joystick == s2[2] and s2[3] == "hat" and s2[4] == hat and s2[5]:find(direction) then
objects["player"][i]:rightkey()
return
end
end

if i ~= mouseowner then
local s = controls[i]["portal1"]
if s and s[1] == "joy" then
if s[3] == "hat" and s[4] == hat then
if joystick == s[2] and s[5]:find(direction) then
shootportal(i, 1, objects["player"][i].x+6/16, objects["player"][i].y+6/16, objects["player"][i].pointingangle)
return
end
end
end

local s = controls[i]["portal2"]
if s and s[1] == "joy" then
if s[3] == "hat" and s[4] == hat then
if joystick == s[2] and s[5]:find(direction) then
shootportal(i, 2, objects["player"][i].x+6/16, objects["player"][i].y+6/16, objects["player"][i].pointingangle)
return
end
end
end
end
end
end

function inrange(i, a, b, include)
if a > b then
b, a = a, b
Expand Down
73 changes: 73 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ function love.load()

love.graphics.setDefaultFilter("nearest", "nearest")

axisDeadZones = {}
joysticks = love.joystick.getJoysticks()
if #joysticks > 0 then
for i, v in ipairs(joysticks) do
axisDeadZones[i] = {}
for j=1, v:getAxisCount() do
axisDeadZones[i][j] = {}
axisDeadZones[i][j]["stick"] = true
axisDeadZones[i][j]["shoulder"] = true
end
for _, j in pairs({"leftx", "lefty", "rightx", "righty", "triggerleft", "triggerright"}) do
axisDeadZones[i][j] = {}
axisDeadZones[i][j]["stick"] = true
axisDeadZones[i][j]["shoulder"] = true
end
end
end

love.graphics.setBackgroundColor(0, 0, 0)


Expand Down Expand Up @@ -1338,6 +1356,61 @@ function love.joystickreleased(joystick, button)
end
end

function love.joystickaxis(joystick, axis, value)
local joysticks,found = love.joystick.getJoysticks(),false
for i,v in ipairs(joysticks) do
if v:getID() == joystick:getID() then
joystick,found = i,true
break
end
end

if found then
local stickmoved = false
local shouldermoved = false

--If this axis is a stick, get whether it just moved out of its deadzone
if math.abs(value) > joystickaimdeadzone and axisDeadZones[joystick][axis]["stick"] then
stickmoved = true
axisDeadZones[joystick][axis]["stick"] = false
elseif math.abs(value) < joystickaimdeadzone and not axisDeadZones[joystick][axis]["stick"] then
axisDeadZones[joystick][axis]["stick"] = true
end
--If this axis is a shoulder, get whether it just moved out of its deadzone
if value > 0 and axisDeadZones[joystick][axis]["shoulder"] then
shouldermoved = true
axisDeadZones[joystick][axis]["shoulder"] = false
elseif value < 0 and not axisDeadZones[joystick][axis]["shoulder"] then
axisDeadZones[joystick][axis]["shoulder"] = true
end
if gamestate == "menu" or gamestate == "options" then
menu_joystickaxis(joystick, axis, value, stickmoved, shouldermoved)
elseif gamestate == "game" then
game_joystickaxis(joystick, axis, value, stickmoved, shouldermoved)
end
end
end
function love.joystickhat(joystick, hat, direction)
local joysticks,found = love.joystick.getJoysticks(),false
for i,v in ipairs(joysticks) do
if v:getID() == joystick:getID() then
joystick,found = i,true
break
end
end

if found then
if gamestate == "menu" or gamestate == "options" then
menu_joystickhat(joystick, hat, direction)
elseif gamestate == "game" then
game_joystickhat(joystick, hat, direction)
end
end
end
-- love.gamepadpressed = love.joystickpressed
-- love.gamepadreleased = love.joystickreleased
-- love.gamepadaxis = love.joystickaxis

function round(num, idp) --Not by me
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
Expand Down
11 changes: 6 additions & 5 deletions mario.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1025,17 +1025,18 @@ function mario:updateangle()
elseif #controls[self.playernumber]["aimx"] > 0 then
local x, y

local joysticks = love.joystick.getJoysticks()
local s = controls[self.playernumber]["aimx"]
if s[1] == "joy" then
x = -love.joystick.getAxis(s[2], s[4])
if s[1] == "joy" and joysticks[s[2]] ~= nil then
x = -joysticks[s[2]]:getAxis(s[4])
if s[5] == "neg" then
x = -x
end
end

s = controls[self.playernumber]["aimy"]
if s[1] == "joy" then
y = -love.joystick.getAxis(s[2], s[4])
if s[1] == "joy" and joysticks[s[2]] ~= nil then
y = -joysticks[s[2]]:getAxis(s[4])
if s[5] == "neg" then
y = -y
end
Expand Down
10 changes: 9 additions & 1 deletion menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2044,14 +2044,22 @@ function menu_joystickreleased(joystick, button)

end

function menu_joystickaxis(joystick, axis, value, stickmoved, shouldermoved)

end

function menu_joystickhat(joystick, hat, direction)

end

function keypromptenter(t, ...)
local arg = {...}
if t == "key" and (arg[1] == ";" or arg[1] == "," or arg[1] == "," or arg[1] == "-") then
return
end
buttonerror = false
axiserror = false
local buttononly = {"run", "jump", "reload", "use", "portal1", "portal2"}
local buttononly = {"run", "jump", "reload", "use"}
local axisonly = {"aimx", "aimy"}
if t ~= "key" or arg[1] ~= "escape" then
if t == "key" then
Expand Down

0 comments on commit 46c4736

Please sign in to comment.