Repository navigation
Expand file tree
/
Copy pathInputHandler.java
More file actions
55 lines (49 loc) · 1.77 KB
/
Copy pathInputHandler.java
File metadata and controls
55 lines (49 loc) · 1.77 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
import javafx.scene.input.KeyCode;
/**
* InputHandler processes and manages player inputs in the game.
* It tracks the most recent input and provides mechanisms to consume and check for pending inputs.
* @author Omar Sanad
* @author Alex Vesely
*/
public class InputHandler {
private boolean isInputPending;
private GameController.PlayerInput currentInput;
/**
* Registers a key press and maps it to a corresponding player input.
* @param code the KeyCode representing the player's input
*/
public void registerInput(KeyCode code) {
switch (code) {
case UP -> currentInput = GameController.PlayerInput.UP;
case DOWN -> currentInput = GameController.PlayerInput.DOWN;
case LEFT -> currentInput = GameController.PlayerInput.LEFT;
case RIGHT -> currentInput = GameController.PlayerInput.RIGHT;
default -> {
currentInput = null; // Reset or handle invalid input
isInputPending = false; // No valid input is pending
}
}
if (currentInput != null) {
isInputPending = true;
}
}
/**
* Checks if there is a pending player input.
* @return true if there is an input waiting to be consumed, false otherwise
*/
public boolean isInputPending() {
return isInputPending;
}
/**
* Consumes the current player input and marks it as processed.
* If no input is pending, returns {@code null}.
* @return the current player input if available, or {@code null} if no input is pending
*/
public GameController.PlayerInput consumeInput() {
if (isInputPending) {
isInputPending = false;
return currentInput;
}
return null;
}
}