forked from SimonLarsen/duckmarines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.lua
276 lines (223 loc) · 5.97 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
--- Input base class
Input = {}
Input.__index = Input
Input.TYPE_NONE = 0
Input.TYPE_KEYBOARD = 1
Input.TYPE_MOUSE = 2
Input.TYPE_JOYSTICK = 3
Input.TYPE_BOT = 4
function Input.create()
local self = setmetatable({}, Input)
self.action = nil
self.clicked = false
return self
end
function Input:getAction()
return self.action
end
function Input:wasClicked()
return self.clicked
end
function Input:wasMenuPressed()
return self.menuPressed
end
function Input:isDown()
return false
end
function Input:clear()
self.action = nil
self.clicked = false
self.menuPressed = false
end
function Input:keypressed(k) end
function Input:mousepressed(x, y, button) end
function Input:mousereleased(x, y, button) end
function Input:joystickpressed(joystick, button) end
--- NullInput (does nothing)
NullInput = {}
NullInput.__index = NullInput
setmetatable(NullInput, Input)
function NullInput.create()
local self = setmetatable(Input.create(), NullInput)
return self
end
function NullInput:getMovement(dt)
return 0, 0, false
end
function NullInput:getType() return Input.TYPE_NONE end
--- Keyboard input
KeyboardInput = {}
KeyboardInput.__index = KeyboardInput
setmetatable(KeyboardInput, Input)
KeyboardInput.SPEED = 300
function KeyboardInput.create(lock)
local self = setmetatable(Input.create(), KeyboardInput)
self.type = Input.TYPE_KEYBOARD
self.lock = lock or false
return self
end
function KeyboardInput:getMovement(dt)
local dx = 0
local dy = 0
if not love.keyboard.isDown("space") or self.lock == false then
if love.keyboard.isDown("left") then
dx = dx - self.SPEED * dt end
if love.keyboard.isDown("right") then
dx = dx + self.SPEED * dt end
if love.keyboard.isDown("up") then
dy = dy - self.SPEED * dt end
if love.keyboard.isDown("down") then
dy = dy + self.SPEED * dt end
end
return dx, dy, false
end
function KeyboardInput:keypressed(k)
if k == "space" or k == "return" then
self.clicked = true
elseif love.keyboard.isDown("space") then
if k == "up" then
self.action = 0
elseif k == "right" then
self.action = 1
elseif k == "down" then
self.action = 2
elseif k == "left" then
self.action = 3
end
end
end
function KeyboardInput:isDown()
return love.keyboard.isDown("space")
end
function KeyboardInput:getType() return Input.TYPE_KEYBOARD end
--- Mouse input
MouseInput = {}
MouseInput.__index = MouseInput
setmetatable(MouseInput, Input)
function MouseInput.create(lock)
local self = setmetatable(Input.create(), MouseInput)
self.down = false
self.wait = 0 -- Workaround for stupid bug
if lock ~= nil then
self.lock = lock
else
self.lock = true
end
love.mouse.setPosition(WIDTH/2, HEIGHT/2)
return self
end
function MouseInput:getMovement(dt)
if self.lock == false or (self.down == false and self.wait == 0) then
local mx, my = love.mouse.getPosition()
love.mouse.setPosition(WIDTH/2, HEIGHT/2)
return mx-WIDTH/2, my-HEIGHT/2, false
else
self.wait = 0
return 0, 0, false
end
end
function MouseInput:mousepressed(x, y, button)
if button == 1 then
self.down = true
self.clicked = true
self.clickx = x
self.clicky = y
end
end
function MouseInput:mousereleased(x, y, button)
if button == 1 then
if self.down == true then
local dx = x - self.clickx
local dy = y - self.clicky
if dx^2 + dy^2 > 16 then
self.action = vecToDir(dx, dy)
end
self.down = false
self.wait = 1
love.mouse.setPosition(WIDTH/2, HEIGHT/2)
end
end
end
function MouseInput:isDown()
return love.mouse.isDown(1)
end
function MouseInput:getType() return Input.TYPE_MOUSE end
--- Joystick Input
JoystickInput = {}
JoystickInput.__index = JoystickInput
setmetatable(JoystickInput, Input)
JoystickInput.SPEED = 300
JoystickInput.DEADZONE = 0.25
function JoystickInput.create(joystick, lock)
local self = setmetatable(Input.create(), JoystickInput)
self.joystick = joystick
if joystick:isGamepad() then
local _
_, self.leftXAxis = joystick:getGamepadMapping("leftx")
_, self.leftYAxis = joystick:getGamepadMapping("lefty")
_, self.rightXAxis = joystick:getGamepadMapping("rightx")
_, self.rightYAxis = joystick:getGamepadMapping("righty")
_, self.buttony = joystick:getGamepadMapping("y")
_, self.buttonb = joystick:getGamepadMapping("b")
_, self.buttona = joystick:getGamepadMapping("a")
_, self.buttonx = joystick:getGamepadMapping("x")
_, self.buttonstart = joystick:getGamepadMapping("start")
else
self.leftXAxis = 1
self.leftYAxis = 2
self.rightXAxis = 3
self.rightYAxis = 4
self.buttony = 1
self.buttonb = 2
self.buttona = 3
self.buttonx = 4
self.buttonstart = 5
end
self.down = false
self.lock = lock or false
return self
end
function JoystickInput:getMovement(dt)
local dx = 0
local dy = 0
local leftx = self.joystick:getAxis(self.leftXAxis)
local lefty = self.joystick:getAxis(self.leftYAxis)
local rightx = self.joystick:getAxis(self.rightXAxis)
local righty = self.joystick:getAxis(self.rightYAxis)
if rightx and righty and not self:inDeadZone(rightx, righty) then
self.action = vecToDir(rightx, righty)
end
if leftx and lefty and not self:inDeadZone(leftx, lefty) then
if not self:isDown() or not self.lock then
dx = leftx * self.SPEED * dt
dy = lefty * self.SPEED * dt
end
end
return dx, dy, false
end
function JoystickInput:joystickpressed(joystick, button)
if joystick:getID() == self.joystick:getID() then
if button == self.buttona then
self.clicked = true
end
if button == self.buttonstart then
self.menuPressed = true
end
if button == self.buttony then
self.action = 0
elseif button == self.buttonb then
self.action = 1
elseif button == self.buttona then
self.action = 2
elseif button == self.buttonx then
self.action = 3
end
end
end
function JoystickInput:isDown()
return self.joystick:isDown(self.buttony, self.buttonb, self.buttona, self.buttonx)
end
function JoystickInput:inDeadZone(axis1, axis2)
return math.abs(axis1)^2 + math.abs(axis2)^2 < JoystickInput.DEADZONE^2
end
function JoystickInput:getType() return Input.TYPE_JOYSTICK end