/
Input.lua
executable file
·312 lines (304 loc) · 9.37 KB
/
Input.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
function getInputPlayer(playerNumber)
local xInput,yInput=0,0
local buttonInput=false
local inputForceMagnitude=thrusterForceMagnitude
--player 1 responds to touch events also
if playerNumber==1 and touching then
-- deal with virtual gamepad touch events
local touches = love.touch.getTouches()
for i, id in ipairs(touches) do
local x, y = love.touch.getPosition(id)
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
-- relative (square) button size
local dPadButtonSizeX = dPadButtonSizeRel*width
local dPadButtonSizeY = dPadButtonSizeX
-- top left coordinate of virtual gamepad
local dPadTopLeftX=0
local dPadTopLeftY=height-3*dPadButtonSizeY
-- virtual gamepad button coordinates i,j (1,1) top left, (3,3) bottom right
local i = math.ceil((x-dPadTopLeftX)/dPadButtonSizeX)
local j = math.ceil((y-dPadTopLeftY)/dPadButtonSizeY)
if i>=1 and i<=3 and j>=1 and j<=3 then
xInput = i-2
yInput = j-2
end
local fireButtonSizeX = fireButtonSizeRel*width
local fireButtonSizeY = fireButtonSizeX
-- large virtual firebutton on the bottom right
local fireButtonTopLeftX=width-fireButtonSizeX
local fireButtonTopLeftY=height-fireButtonSizeY
if x>=fireButtonTopLeftX and x<=fireButtonTopLeftX+fireButtonSizeX
and y>=fireButtonTopLeftY and y<=fireButtonTopLeftY+fireButtonSizeY
then
buttonInput = true
end
end
end
-- normal controls
if controlTypes[controls[playerNumber]]=="Keyboard 1" then
if love.keyboard.isDown("left") then
xInput = -1
end
if love.keyboard.isDown("right") then
xInput = 1
end
if love.keyboard.isDown("right") and love.keyboard.isDown("left") then
xInput = 0
end
if love.keyboard.isDown("up") then
yInput = -1
end
if love.keyboard.isDown("down") then
yInput = 1
end
if love.keyboard.isDown("up") and love.keyboard.isDown("down") then
yInput = 0
end
if love.keyboard.isDown("rshift") then
buttonInput=true
end
elseif controlTypes[controls[playerNumber]]=="Keyboard 2" then
if love.keyboard.isDown("a") then
xInput = -1
end
if love.keyboard.isDown("d") then
xInput = 1
end
if love.keyboard.isDown("a") and love.keyboard.isDown("s") then
xInput = 0
end
if love.keyboard.isDown("w") then
yInput = -1
end
if love.keyboard.isDown("s") then
yInput = 1
end
if love.keyboard.isDown("w") and love.keyboard.isDown("s") then
yInput = 0
end
if love.keyboard.isDown("lshift") then
buttonInput=true
end
else
local joystickNum=controls[playerNumber]-2
local joyXInput,joyYInput = 0,0
if ( joystickNum <= numJoysticks ) then
local joystick = joysticks[joystickNum]
if (joystick:isGamepad()) then
joyXInput = joystick:getGamepadAxis("leftx")
joyYInput = joystick:getGamepadAxis("lefty")
if joystick:isGamepadDown("dpup") then
joyYInput = -1
elseif joystick:isGamepadDown("dpdown") then
joyYInput = 1
end
if joystick:isGamepadDown("dpleft") then
joyXInput = -1
elseif joystick:isGamepadDown("dpright") then
joyXInput = 1
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()
end
if joystick:getHatCount()>=1 then
local hatDirection = joystick:getHat(1)
if hatDirection == 'u' then
joyYInput = -1
joyXInput = 0
elseif hatDirection == 'd' then
joyYInput = 1
elseif hatDirection == 'l' then
joyXInput = -1
elseif hatDirection == 'r' then
joyXInput = 1
elseif hatDirection == 'ld' then
joyXInput = -1
joyYInput = 1
elseif hatDirection == 'lu' then
joyXInput = -1
joyYInput = -1
elseif hatDirection == 'rd' then
joyXInput = 1
joyYInput = 1
elseif hatDirection == 'ru' then
joyXInput = 1
joyYInput = -1
end
end
if numButtons>0 then
if anyButtonPressed(joystick, numButtons) then
buttonInput=true
end
end
end
if joyXInput > 0.25 then
xInput = 1
elseif joyXInput < -0.25 then
xInput = -1
else
xInput = 0
end
if joyYInput > 0.25 then
yInput = 1
elseif joyYInput < -0.25 then
yInput = -1
else
yInput = 0
end
end
end
return xInput*inputForceMagnitude,yInput*inputForceMagnitude,buttonInput
end
function anyButtonPressed(joystick,numButtons)
local returnValue=false
if numButtons>0 then
for i=1,numButtons do
if joystick:isDown(i) then
returnValue=true
end
end
end
return returnValue
end
function love.gamepadpressed(joystick, button)
-- press fire on title screen to start single player game with joystick controls
if (displayingTitleScreen
and button ~= "dpup"
and button ~= "dpdown"
and button ~= "dpleft"
and button ~= "dpright") then
if (button == "back") then
love.keypressed('q')
end
for i=1,#joysticks,1 do
if joysticks[i]:getID()==joystick:getID() then
numPlayers=1
for j=1,5,1 do
if controls[j]==i+2 then
numPlayers=j
end
end
startGame()
end
end
elseif (gameLost
and button ~= "dpup"
and button ~= "dpdown"
and button ~= "dpleft"
and button ~= "dpright"
and button ~= "back") then
titleScreen()
end
end
function love.keypressed(key, scancode, isrepeat)
if displayingTitleScreen then
if key == 'q' then
love.event.quit()
elseif key == '1' then
numPlayers=1
startGame()
elseif key == '2' then
numPlayers=2
startGame()
elseif key == '3' then
numPlayers=3
startGame()
elseif key == '4' then
numPlayers=4
startGame()
elseif key == '5' then
numPlayers=5
startGame()
elseif key == 'c' then
displayingControls = true
displayingTitleScreen = false
end
end
if displayingControls then
if key == '1' then
cycleControls(1)
elseif key == '2' then
cycleControls(2)
elseif key == '3' then
cycleControls(3)
elseif key == '4' then
cycleControls(4)
elseif key == '5' then
cycleControls(5)
elseif key == 'q' then
displayingControls = false
displayingTitleScreen = true
end
elseif key == 'q' then
titleScreen()
end
if key == 'p' then
if gameIsPaused == false then
pauseGame()
else
unpauseGame()
end
elseif key == 'y' then
won()
elseif key == 'n' then
if currentLevel>1 then
currentLevel=currentLevel-1
initializeLevel(currentLevel)
end
elseif key == 'b' then
if currentLevel<numLevels then
currentLevel=currentLevel+1
initializeLevel(currentLevel)
waitForClick()
end
elseif key == 'h' then
if love.graphics.getSupported("pixeleffect") then
drawShadows=not drawShadows
end
elseif key == "space" then
waitingForClick=false
--elseif key == 'c' and gameLost==true then
-- continueGame()
elseif key == 'f' then
cycleScreenModes()
elseif key == 't' then
toggleScaling()
elseif key == 'o' then
showFPS= not showFPS
end
end
function cycleControls(playerNumber)
controls[playerNumber]=controls[playerNumber]+1
if controls[playerNumber]>#controlTypes then
controls[playerNumber]=1
end
end
function love.mousepressed(x, y, button, istouch)
if gameIsPaused==false and waitingForClick and button == 1 then
waitingForClick=false
end
if istouch then
touching = true
else
touching = false
end
if displayingTitleScreen then
if button == 1 then
numPlayers=1
startGame()
end
elseif gameLost then
if button == 1 then
titleScreen()
end
end
end