-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.lua
More file actions
409 lines (354 loc) · 12.5 KB
/
Copy pathinput.lua
File metadata and controls
409 lines (354 loc) · 12.5 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
--------------------------------------------------------------------------------------
-- GAME INPUT MANAGER
-- A game-specific input management layer that sits between main.lua and the InputHandler
-- This module handles:
-- 1. Game-specific input states (menu, playing, paused)
-- 2. Input bindings for various game actions
-- 3. Unified input registration with the InputHandler
--------------------------------------------------------------------------------------
local InputHandler = require("lib.input_handler")
local DebugConsole = require("lib.debug_console")
local UI = require("ui")
local Input = {
currentGameState = "menu", -- Default state, will be updated
bindings = {
-- Default key bindings that can be overridden
up = {"w", "up"},
down = {"s", "down"},
left = {"a", "left"},
right = {"d", "right"},
jump = {"space"},
attack = {"f"},
interact = {"e"},
pause = {"escape"},
confirm = {"return", "kpenter"},
back = {"escape", "backspace"}
}
}
-- Initialize the input system
function Input.init(gameState, player, mainModule)
-- Store references
Input.gameState = gameState
Input.player = player
Input.mainModule = mainModule or _G -- Use global environment if not provided
-- Initialize the lower-level input handler
InputHandler.init(gameState)
-- Register all input handlers
Input.registerAllHandlers()
-- Set up default callbacks
Input.registerDefaultCallbacks()
return Input
end
-- Register default callbacks that integrate with common game functions
function Input.registerDefaultCallbacks()
Input.registerCallbacks({
-- Menu callbacks
onMenuConfirm = function()
if Input.mainModule.startGame then
Input.mainModule.startGame()
return true
end
return false
end,
-- Pause menu callbacks
onResumeGame = function()
if Input.mainModule.resumeGame then
Input.mainModule.resumeGame()
return true
end
return false
end,
-- Game state transition callbacks
onPauseGame = function()
if Input.mainModule.gameState == "playing" and not DebugConsole.visible then
Input.mainModule.gameState = "paused"
Input.setGameState(Input.mainModule.gameState)
UI.clear()
if Input.mainModule.GameUI then
Input.mainModule.GameUI.initPauseMenu(function()
if Input.mainModule.resumeGame then
Input.mainModule.resumeGame()
end
end)
end
return true
end
return false
end
})
end
-- Update game state in both this module and the InputHandler
function Input.setGameState(newState)
Input.currentGameState = newState
InputHandler.setGameState(newState)
return newState
end
-- Register all input handlers for all game states
function Input.registerAllHandlers()
-- Clear any existing handlers
-- InputHandler doesn't have a clear method, but we're overriding all handlers
-- Register menu state handlers
Input.registerMenuHandlers()
-- Register playing state handlers
Input.registerPlayingHandlers()
-- Register paused state handlers
Input.registerPausedHandlers()
-- Register global handlers (work in all states)
Input.registerGlobalHandlers()
-- Register UI handlers
Input.registerUIHandlers()
-- Register debug handlers
Input.registerDebugHandlers()
end
-- Register menu state input handlers
function Input.registerMenuHandlers()
-- Register key handlers for menu navigation
for _, key in ipairs(Input.bindings.confirm) do
InputHandler.registerKeyPressed("menu", key, function()
-- Delegate to the main game's onMenuConfirm function if it exists
if Input.callbacks and Input.callbacks.onMenuConfirm then
return Input.callbacks.onMenuConfirm()
end
return false
end)
end
for _, key in ipairs(Input.bindings.back) do
InputHandler.registerKeyPressed("menu", key, function()
-- Delegate to the main game's onMenuBack function if it exists
if Input.callbacks and Input.callbacks.onMenuBack then
return Input.callbacks.onMenuBack()
end
return false
end)
end
-- Menu navigation
for _, key in ipairs(Input.bindings.up) do
InputHandler.registerKeyPressed("menu", key, function()
-- Handle menu navigation up
if Input.callbacks and Input.callbacks.onMenuUp then
return Input.callbacks.onMenuUp()
end
return false
end)
end
for _, key in ipairs(Input.bindings.down) do
InputHandler.registerKeyPressed("menu", key, function()
-- Handle menu navigation down
if Input.callbacks and Input.callbacks.onMenuDown then
return Input.callbacks.onMenuDown()
end
return false
end)
end
end
-- Register playing state input handlers
function Input.registerPlayingHandlers()
-- Most of these are handled in the update function of the player
-- But we can handle state transitions here
-- Pause game
for _, key in ipairs(Input.bindings.pause) do
InputHandler.registerKeyPressed("playing", key, function()
if Input.callbacks and Input.callbacks.onPauseGame then
return Input.callbacks.onPauseGame()
end
return false
end)
end
-- For actions that need specific press handling (not just checking isDown)
for _, key in ipairs(Input.bindings.jump) do
InputHandler.registerKeyPressed("playing", key, function()
if Input.callbacks and Input.callbacks.onJump then
return Input.callbacks.onJump()
end
return false
end)
end
for _, key in ipairs(Input.bindings.attack) do
InputHandler.registerKeyPressed("playing", key, function()
if Input.callbacks and Input.callbacks.onAttack then
return Input.callbacks.onAttack()
end
return false
end)
end
for _, key in ipairs(Input.bindings.interact) do
InputHandler.registerKeyPressed("playing", key, function()
if Input.callbacks and Input.callbacks.onInteract then
return Input.callbacks.onInteract()
end
return false
end)
end
end
-- Register paused state input handlers
function Input.registerPausedHandlers()
-- Resume game
for _, key in ipairs(Input.bindings.pause) do
InputHandler.registerKeyPressed("paused", key, function()
if Input.callbacks and Input.callbacks.onResumeGame then
return Input.callbacks.onResumeGame()
end
return false
end)
end
-- Confirm selection
for _, key in ipairs(Input.bindings.confirm) do
InputHandler.registerKeyPressed("paused", key, function()
if Input.callbacks and Input.callbacks.onPauseConfirm then
return Input.callbacks.onPauseConfirm()
end
return false
end)
end
-- Back/cancel
for _, key in ipairs(Input.bindings.back) do
InputHandler.registerKeyPressed("paused", key, function()
if Input.callbacks and Input.callbacks.onPauseBack then
return Input.callbacks.onPauseBack()
end
return false
end)
end
-- Menu navigation
for _, key in ipairs(Input.bindings.up) do
InputHandler.registerKeyPressed("paused", key, function()
if Input.callbacks and Input.callbacks.onPauseMenuUp then
return Input.callbacks.onPauseMenuUp()
end
return false
end)
end
for _, key in ipairs(Input.bindings.down) do
InputHandler.registerKeyPressed("paused", key, function()
if Input.callbacks and Input.callbacks.onPauseMenuDown then
return Input.callbacks.onPauseMenuDown()
end
return false
end)
end
end
-- Register global input handlers (work in all states)
function Input.registerGlobalHandlers()
-- Debug console toggle
InputHandler.registerGlobalKeyPressed("`", function()
if DebugConsole then
DebugConsole.toggle()
return true -- Signal that this key was handled
end
return false
end)
-- Also register the tilde key as an alternative
InputHandler.registerGlobalKeyPressed("~", function()
if DebugConsole then
DebugConsole.toggle()
return true -- Signal that this key was handled
end
return false
end)
-- Debug keys
InputHandler.registerGlobalKeyPressed("f3", function()
if DebugConsole then
DebugConsole.print("FPS: " .. love.timer.getFPS(), {0.2, 1, 0.2, 1})
return true
end
return false
end)
end
-- Register UI input handlers
function Input.registerUIHandlers()
-- Register left mouse button handlers for UI interaction
InputHandler.registerGlobalMousePressed(1, function(x, y)
if UI and UI.handleMousePressed then
return UI.handleMousePressed(x, y, 1)
end
return false
end)
InputHandler.registerGlobalMouseReleased(1, function(x, y)
if UI and UI.handleMouseReleased then
return UI.handleMouseReleased(x, y, 1)
end
return false
end)
end
-- Register debug input handlers
function Input.registerDebugHandlers()
-- Add any debug-specific input handlers here
-- Example: Toggle hitboxes display, etc.
end
-- Register debug input handlers
function Input.registerDebugHandlers()
-- Add any debug-specific input handlers here
-- Example: Toggle hitboxes display, etc.
end
-- Register callbacks from the main game
function Input.registerCallbacks(callbacks)
Input.callbacks = callbacks
end
-- Helper function to get a movement vector for the player based on current input
function Input.getMovementVector()
-- Just delegate to the InputHandler
return InputHandler.getMovementVector()
end
-- Update function to be called from main.lua
function Input.update(dt)
InputHandler.update(dt)
end
-- Methods to check input state
function Input.isDown(action)
-- Map the action to its bound keys and check if any are down
local keys = Input.bindings[action]
if not keys then return false end
for _, key in ipairs(keys) do
if InputHandler.isDown(key) then
return true
end
end
return false
end
function Input.wasPressed(action)
-- Map the action to its bound keys and check if any were just pressed
local keys = Input.bindings[action]
if not keys then return false end
for _, key in ipairs(keys) do
if InputHandler.wasPressed(key) then
return true
end
end
return false
end
-- Method to rebind a key
function Input.rebindKey(action, newKeys)
if type(newKeys) == "string" then
newKeys = {newKeys}
end
Input.bindings[action] = newKeys
end
-- Function to get mouse position
function Input.getMousePosition()
return love.mouse.getPosition()
end
-- Function to check if a mouse button is down
function Input.isMouseDown(button)
return InputHandler.isMouseDown(button)
end
-- Low-level event handler functions that can be called directly from love.run
-- These delegate to InputHandler but could be customized here if needed
function Input.handleKeyPressed(key, scancode, isRepeat)
return InputHandler.handleKeyPressed(key, scancode, isRepeat)
end
function Input.handleKeyReleased(key, scancode)
return InputHandler.handleKeyReleased(key, scancode)
end
function Input.handleMousePressed(x, y, button, isTouch, presses)
return InputHandler.handleMousePressed(x, y, button, isTouch, presses)
end
function Input.handleMouseReleased(x, y, button, isTouch, presses)
return InputHandler.handleMouseReleased(x, y, button, isTouch, presses)
end
function Input.handleMouseMoved(x, y, dx, dy, isTouch)
return InputHandler.handleMouseMoved(x, y, dx, dy, isTouch)
end
function Input.handleWheelMoved(x, y)
return InputHandler.handleWheelMoved(x, y)
end
return Input