-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.lua
More file actions
89 lines (74 loc) · 1.93 KB
/
input.lua
File metadata and controls
89 lines (74 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
--[[
Input events and input checking
Mappings:
]]--
Input = {}
Input.mappings = {
left = {
{"k", "left"},
{"j", "dpleft"},
{"ja", "leftx", -0.2}
},
right = {
{"k", "right"},
{"j", "dpright"},
{"ja", "leftx", 0.2}
},
jump = {
{"k", "space"},
{"k", "up"},
{"j", "a"},
{"j", "x"}
}
}
Input.joystick = nil
function love.joystickadded (j)
local joysticks = love.joystick.getJoysticks()
if joysticks then
Input.joystick = joysticks[1]
end
end
function love.joystickremoved (j)
local joysticks = love.joystick.getJoysticks()
if joysticks then
Input.joystick = joysticks[1]
end
end
-- I manually entered the mappings in the table above, so this isn't super
-- relevant, but...
function Input.addMapping (name, device, key)
local mapcode = {}
if (device == "keyboard") then
mapcode[1] = "k"
elseif (device == "joystick") then
mapcode[1] = "j"
end
mapcode[2] = key
if not Input.mappings[name] then
Input.mappings[mapping] = {}
end
table.insert(Input.mappings[name], mapcode)
return { name, mapcode }
end
function Input.isPressed (name)
if not Input.mappings[name] or not love.window.hasFocus() then
return false
end
for k, v in pairs(Input.mappings[name]) do
if v[1] == "k" then
if love.keyboard.isDown(v[2]) then return true end
elseif Input.joystick then
if v[1] == "j" then
if Input.joystick:isGamepadDown(v[2]) then return true end
elseif v[1] == "ja" then
local axis = Input.joystick:getGamepadAxis(v[2])
if v[3] >= 0 then
if axis >= v[3] then return true end
else
if axis <= v[3] then return true end
end
end
end
end
return false
end