-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathAxisInput.ts
More file actions
194 lines (182 loc) · 7.4 KB
/
Copy pathAxisInput.ts
File metadata and controls
194 lines (182 loc) · 7.4 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
import MatchMode from "@/systems/match_mode/MatchMode"
import { MatchModeType } from "@/systems/match_mode/MatchModeTypes"
import { TouchControlsAxes } from "@/ui/components/TouchControls"
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 an axis between -1 and 1. Can be a gamepad axis, two gamepad buttons, or two keyboard buttons. */
export default class AxisInput extends Input {
public posKeyCode: KeyCode
public posKeyModifiers: ModifierState
public negKeyCode: KeyCode
public negKeyModifiers: ModifierState
public gamepadAxisNumber: number
public touchControlAxis: TouchControlsAxes
public joystickInverted: boolean
public useGamepadButtons: boolean
public posGamepadButton: number
public negGamepadButton: 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} [posKeyCode] - The keyboard input that corresponds to a positive input value (1).
* @param {string} [negKeyCode] - The keyboard input that corresponds to a negative input value (-1).
* @param {number} [gamepadAxisNumber] - The gamepad axis that this input looks at if the scheme is set to use a gamepad.
* @param {boolean} [joystickInverted] - Inverts the input if a gamepad axis is used.
* @param {boolean} [useGamepadButtons] - If this is true and the scheme is set to use a gamepad, this axis will be between two buttons on the controller.
* @param {number} [posGamepadButton] - The gamepad button that corresponds to a positive input value (1).
* @param {number} [negGamepadButton] - The gamepad button that corresponds to a negative input value (-1).
* @param {ModifierState} [posKeyModifiers] - The key modifier state for the positive keyboard input.
* @param {ModifierState} [negKeyModifiers] - The key modifier state for the negative keyboard input.
*/
public constructor(
inputName: InputName,
posKeyCode?: KeyCode,
negKeyCode?: KeyCode,
gamepadAxisNumber?: number,
joystickInverted?: boolean,
useGamepadButtons?: boolean,
posGamepadButton?: number,
negGamepadButton?: number,
touchControlAxis?: TouchControlsAxes,
posKeyModifiers?: ModifierState,
negKeyModifiers?: ModifierState
) {
super(inputName)
this.posKeyCode = posKeyCode ?? ""
this.posKeyModifiers = posKeyModifiers ?? EMPTY_MODIFIER_STATE
this.negKeyCode = negKeyCode ?? ""
this.negKeyModifiers = negKeyModifiers ?? EMPTY_MODIFIER_STATE
this.gamepadAxisNumber = gamepadAxisNumber ?? -1
this.touchControlAxis = touchControlAxis ?? TouchControlsAxes.NONE
this.joystickInverted = joystickInverted ?? false
this.useGamepadButtons = useGamepadButtons ?? false
this.posGamepadButton = posGamepadButton ?? -1
this.negGamepadButton = negGamepadButton ?? -1
}
public static unbound(inputName: InputName) {
return new AxisInput(inputName)
}
public static onGamepadJoystick(inputName: InputName, gamepadAxisNumber: number, joystickInverted: boolean) {
return new AxisInput(
inputName,
undefined,
undefined,
gamepadAxisNumber,
joystickInverted,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined
)
}
public static onGamepadButtons(inputName: InputName, posGamepadButton: number, negGamepadButton: number) {
return new AxisInput(
inputName,
undefined,
undefined,
undefined,
undefined,
true,
posGamepadButton,
negGamepadButton,
undefined,
undefined,
undefined
)
}
public static onKeyboard(
inputName: InputName,
posKeyCode: KeyCode,
negKeyCode: KeyCode,
posKeyModifiers?: ModifierState,
negKeyModifiers?: ModifierState
) {
return new AxisInput(
inputName,
posKeyCode,
negKeyCode,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
posKeyModifiers,
negKeyModifiers
)
}
public static onKeyboardSingleKey(inputName: InputName, key: KeyCode, negKeyModifiers?: ModifierState) {
return new AxisInput(
inputName,
key,
key,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
negKeyModifiers
)
}
public static onTouchControl(inputName: InputName, touchControlAxis: TouchControlsAxes) {
return new AxisInput(
inputName,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
touchControlAxis,
undefined,
undefined
)
}
/**
* @param useGamepad Looks at the gamepad if true and the keyboard if false.
* @returns {number} KEYBOARD: 1 if positive pressed, -1 if negative pressed, or 0 if none or both are pressed.
* @returns {number} GAMEPAD: a number between -1 and 1 with a deadband in the middle.
*/
getValue(useGamepad: boolean, useTouchControls: boolean, playerSlot: number = 0): number {
const matchModeType = MatchMode.getInstance().getMatchModeType()
if (matchModeType === MatchModeType.MATCH_ENDED || matchModeType === MatchModeType.AUTONOMOUS) {
return 0
}
if (useGamepad) {
// Gamepad joystick axis
if (!this.useGamepadButtons)
return InputSystem.getGamepadAxis(this.gamepadAxisNumber, playerSlot) * (this.joystickInverted ? -1 : 1)
// Gamepad button axis
return (
(InputSystem.isGamepadButtonPressed(this.posGamepadButton, playerSlot) ? 1 : 0) -
(InputSystem.isGamepadButtonPressed(this.negGamepadButton, playerSlot) ? 1 : 0)
)
}
if (useTouchControls) {
return InputSystem.getTouchControlsAxis(this.touchControlAxis) * (this.joystickInverted ? -1 : 1)
}
// Keyboard button axis
return (
(InputSystem.isKeyPressed(this.posKeyCode, this.posKeyModifiers) ? 1 : 0) -
(InputSystem.isKeyPressed(this.negKeyCode, this.negKeyModifiers) ? 1 : 0)
)
}
keysUsed(playerSlot: number = 0): KeyDescriptor[] {
return [
this.describeKey(this.posKeyCode, this.posKeyModifiers),
this.describeKey(this.negKeyCode, this.negKeyModifiers),
this.describeGamepadBtn(this.posGamepadButton, playerSlot),
this.describeGamepadBtn(this.negGamepadButton, playerSlot),
this.describeGamepadAxis(this.gamepadAxisNumber, playerSlot),
this.describeTouchAxis(this.touchControlAxis),
]
}
}