/
gamepad.ts
142 lines (114 loc) · 3.53 KB
/
gamepad.ts
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
import { IGamepad, INavigator, IWindow } from '../apis'
import { Control, TriggerControl } from '../core/control'
import { findButtonNumber, getButtonLabel } from '../maps/gamepad'
import store from '../store'
import { Vector2 } from '../utils/math'
export interface GamepadStick {
label: string
xAxis: number
yAxis: number
}
const gamepadSticks: { [id: string]: GamepadStick } = {
left: { label: 'Left stick', xAxis: 0, yAxis: 1 },
right: { label: 'Right stick', xAxis: 2, yAxis: 3 },
}
export class Gamepad {
private window: IWindow
private navigator: INavigator
private pressedButtons: Set<number> = new Set()
private gamepadIndex: number | undefined
private gamepadTimestamp = 0
constructor(
/* istanbul ignore next */
{ win = window, nav = navigator as any }: { win?: IWindow, nav?: INavigator } = {},
) {
this.window = win
this.navigator = nav
this.window.addEventListener('gamepadconnected', ({ gamepad }) => {
if (this.isConnected()) return
if (gamepad.mapping === 'standard') {
this.gamepadIndex = gamepad.index
store.preferGamepad = true
}
})
this.window.addEventListener('gamepaddisconnected', ({ gamepad }) => {
if (this.gamepadIndex !== gamepad.index) return
this.gamepadIndex = undefined
store.preferGamepad = false
})
}
public isConnected(): boolean {
return this.gamepadIndex !== undefined && this.gamepad.connected
}
private get gamepad(): IGamepad {
const gamepad = this.navigator.getGamepads()[this.gamepadIndex!]
if (gamepad.timestamp > this.gamepadTimestamp) {
store.preferGamepad = true
this.gamepadTimestamp = gamepad.timestamp
}
return gamepad
}
public button(button: number | string): TriggerControl<boolean> {
const buttonNumber = findButtonNumber(button)
const label = getButtonLabel(buttonNumber)
return {
label,
query: () => {
if (!this.isConnected()) return false
return this.gamepad.buttons[buttonNumber].pressed
},
fromGamepad: true,
trigger: {
label,
query: () => {
if (!this.isConnected()) return false
if (this.gamepad.buttons[buttonNumber].pressed) {
if (this.pressedButtons.has(buttonNumber)) return false
this.pressedButtons.add(buttonNumber)
return true
}
this.pressedButtons.delete(buttonNumber)
return false
},
fromGamepad: true,
},
}
}
public stick(stick: string | GamepadStick): Control<Vector2> {
let gpStick: GamepadStick
if (typeof stick === 'string') {
if (stick in gamepadSticks) {
gpStick = gamepadSticks[stick]
} else {
throw new Error(`Gamepad stick "${stick}" not found!`)
}
} else {
gpStick = stick
}
return {
label: gpStick.label,
query: () => {
if (!this.isConnected()) return new Vector2(0, 0)
return new Vector2(
this.gamepad.axes[gpStick.xAxis],
this.gamepad.axes[gpStick.yAxis],
)
},
}
}
public async vibrate(
duration: number,
{ weakMagnitude, strongMagnitude }: VibrationOptions = {},
): Promise<void> {
if (!this.isConnected()) return
const actuator = this.gamepad.vibrationActuator
if (!actuator || actuator.type !== 'dual-rumble') return
await actuator.playEffect('dual-rumble', {
duration, strongMagnitude, weakMagnitude,
})
}
}
interface VibrationOptions {
strongMagnitude?: number
weakMagnitude?: number
}