-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeys.lua
138 lines (104 loc) · 2.51 KB
/
keys.lua
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
Signal = require('vendor/hump.signal')
Timer = require('vendor/hump.timer')
KEY_REPEAT = 0.2
LeftTimer = Timer.new()
RightTimer = Timer.new()
js = love.joystick.getJoysticks()[1]
-- Escape vavles.
Signal.register('escape', function()
Signal.emit('toggle-pause')
end)
Signal.register('q', function()
Signal.emit('quit')
end)
-- Left / Right support.
Signal.register('left', function()
-- LeftTimer = Timer.new()
RightTimer:clear()
Signal.emit('left-event')
LeftTimer:every(KEY_REPEAT, function()
Signal.emit('left-event')
end)
end)
Signal.register('right', function()
LeftTimer:clear()
Signal.emit('right-event')
RightTimer:every(KEY_REPEAT, function()
Signal.emit('right-event')
end)
end)
Signal.register('left-released', function()
LeftTimer:clear()
end)
Signal.register('right-released', function()
RightTimer:clear()
end)
once = false
Signal.register('register-joystick', function(joystick)
js = joystick
if not once then
Signal.register(js:getButtonCount(), function()
Signal.emit('toggle-pause')
end)
once = true
end
end)
js_l_hats_on = false
js_r_hats_on = false
function js_hat_manager(d)
if d == 'l' then
if not js_l_hats_on then
Signal.emit('left')
js_l_hats_on = true
end
elseif d == 'r' then
if not js_r_hats_on then
Signal.emit('right')
js_r_hats_on = true
end
else
if js_l_hats_on then
Signal.emit('left-released')
js_l_hats_on = false
end
if js_r_hats_on then
Signal.emit('right-released')
js_r_hats_on = false
end
return nil
end
end
js_l_axis_on = false
js_r_axis_on = false
function js_axis_manager(v)
if v < -0.5 then
if not js_l_axis_on then
Signal.emit('left')
js_l_axis_on = true
end
elseif v > 0.5 then
if not js_r_axis_on then
Signal.emit('right')
js_r_axis_on = true
end
else
if js_l_axis_on then
Signal.emit('left-released')
js_l_axis_on = false
end
if js_r_axis_on then
Signal.emit('right-released')
js_r_axis_on = false
end
return nil
end
end
function update(dt)
LeftTimer:update(dt)
RightTimer:update(dt)
if js then
js_axis_manager(js:getAxis(1))
js_hat_manager(js:getHat(1))
end
end
return update