-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathButtonInput.ts
More file actions
65 lines (57 loc) · 2.79 KB
/
Copy pathButtonInput.ts
File metadata and controls
65 lines (57 loc) · 2.79 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
import MatchMode from "@/systems/match_mode/MatchMode"
import { MatchModeType } from "@/systems/match_mode/MatchModeTypes"
import InputSystem from "../InputSystem"
import { EMPTY_MODIFIER_STATE, type InputName, type KeyDescriptor, type ModifierState } from "../InputTypes"
import type { KeyCode } from "../KeyboardTypes"
import Input from "./Input"
/** Represents any user input that is a single true/false button. */
export default class ButtonInput extends Input {
public keyCode: KeyCode
public keyModifiers: ModifierState
public gamepadButton: number
/**
* All optional params will remain unassigned if not value is given. This can be assigned later by the user through the configuration panel.
*
* @param {string} inputName - The name given to this input to identify it's function.
* @param {string} [keyCode] - The keyboard button for this input if a gamepad is not used.
* @param {number} [gamepadButton] - The gamepad button for this input if a gamepad is used.
* @param {ModifierState} [keyModifiers] - The key modifier state for the keyboard input.
*/
public constructor(inputName: InputName, keyCode?: KeyCode, gamepadButton?: number, keyModifiers?: ModifierState) {
super(inputName)
this.keyCode = keyCode ?? ""
this.keyModifiers = keyModifiers ?? EMPTY_MODIFIER_STATE
this.gamepadButton = gamepadButton ?? -1
}
/**
* @param useGamepad Looks at the gamepad if true and the keyboard if false.
* @returns 1 if pressed, 0 if not pressed or not found.
*/
getValue(useGamepad: boolean, _useTouchControls: boolean = false, playerSlot: number = 0): number {
const matchModeType = MatchMode.getInstance().getMatchModeType()
if (matchModeType === MatchModeType.MATCH_ENDED || matchModeType === MatchModeType.AUTONOMOUS) {
return 0
}
// Gamepad button input
if (useGamepad) {
return InputSystem.isGamepadButtonPressed(this.gamepadButton, playerSlot) ? 1 : 0
}
// Keyboard button input
return InputSystem.isKeyPressed(this.keyCode, this.keyModifiers) ? 1 : 0
}
keysUsed(playerSlot: number = 0): KeyDescriptor[] {
return [
this.describeKey(this.keyCode, this.keyModifiers),
this.describeGamepadBtn(this.gamepadButton, playerSlot),
]
}
static onGamepad(inputName: InputName, gamepadButton: number) {
return new ButtonInput(inputName, undefined, gamepadButton, undefined)
}
static onKeyboard(inputName: InputName, keyCode: KeyCode, keyModifiers?: ModifierState) {
return new ButtonInput(inputName, keyCode, undefined, keyModifiers)
}
static unbound(inputName: InputName) {
return new ButtonInput(inputName, undefined, undefined, undefined)
}
}