2022-07-21 19:15:35 +03:00
|
|
|
import TPatternUnit from '@/models/TPatternUnit';
|
|
|
|
import IGamepad from '@/models/IGamepad';
|
|
|
|
import IVibrator from '@/models/IVibrator';
|
2022-07-14 17:50:28 +03:00
|
|
|
|
2022-07-24 18:56:41 +03:00
|
|
|
class Vibrator implements IVibrator {
|
2022-07-14 17:50:28 +03:00
|
|
|
readonly id: number;
|
|
|
|
readonly canVibrate: boolean;
|
|
|
|
isVibrating: boolean;
|
|
|
|
unit: IGamepad;
|
2022-07-17 15:16:28 +03:00
|
|
|
pattern: TPatternUnit[];
|
2022-08-08 22:09:56 +03:00
|
|
|
interval: number;
|
2022-07-14 17:50:28 +03:00
|
|
|
|
|
|
|
constructor(unit: IGamepad) {
|
|
|
|
this.unit = unit;
|
|
|
|
this.id = Date.now();
|
|
|
|
this.canVibrate = (this.unit.vibrationActuator) ? true : false;
|
|
|
|
this.isVibrating = false;
|
|
|
|
this.pattern = [];
|
2022-08-08 22:09:56 +03:00
|
|
|
|
|
|
|
this.update = this.update.bind(this);
|
|
|
|
this.interval = setInterval(this.update, 1);
|
2022-07-14 17:50:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
update(): void {
|
|
|
|
const gamepads = navigator.getGamepads();
|
2022-08-08 22:09:56 +03:00
|
|
|
this.unit = gamepads[this.unit.index] as unknown as IGamepad;
|
2022-07-14 17:50:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
reset(): void {
|
|
|
|
this.isVibrating = false;
|
|
|
|
this.unit.vibrationActuator.reset();
|
|
|
|
}
|
|
|
|
|
2022-07-17 15:16:28 +03:00
|
|
|
async vibrate(pattern: TPatternUnit[]) {
|
2022-07-14 17:50:28 +03:00
|
|
|
this.isVibrating = true;
|
|
|
|
this.pattern = pattern;
|
|
|
|
while (this.isVibrating === true) {
|
|
|
|
for (let i = 0; i < this.pattern.length; i++) {
|
|
|
|
if (this.isVibrating === true) {
|
|
|
|
this.unit.vibrationActuator.playEffect('dual-rumble', this.pattern[i]);
|
2022-08-05 13:03:37 +03:00
|
|
|
await this.sleep(this.pattern[i].startDelay + this.pattern[i].duration);
|
2022-07-14 17:50:28 +03:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(ms: number): Promise<number> {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
}
|
2022-07-21 17:22:33 +03:00
|
|
|
}
|
|
|
|
|
2022-07-24 18:56:41 +03:00
|
|
|
export default Vibrator;
|
|
|
|
|