- added vibrate-function that is delegate playEffect in Vibrator and IVibrator.

- old-vibrate-function now is loop-function.
- added token readonly in IGamepad.
- updated dependencies.
This commit is contained in:
Eugene Serb 2022-08-10 17:46:21 +03:00
parent 4a0b8cd480
commit d340a6e81d
6 changed files with 41 additions and 30 deletions

24
package-lock.json generated
View File

@ -4322,9 +4322,9 @@
"dev": true
},
"node_modules/colord": {
"version": "2.9.2",
"resolved": "https://registry.npmjs.org/colord/-/colord-2.9.2.tgz",
"integrity": "sha512-Uqbg+J445nc1TKn4FoDPS6ZZqAvEDnwrH42yo8B40JSOgSLxMZ/gt3h4nmCtPLQeXhjJJkqBx7SCY35WnIixaQ==",
"version": "2.9.3",
"resolved": "https://registry.npmjs.org/colord/-/colord-2.9.3.tgz",
"integrity": "sha512-jeC1axXpnb0/2nn/Y1LPuLdgXBLH7aDcHu4KEKfqw3CUhX7ZpfBSlPKyqXE6btIgEzfWtrX3/tyBCaCvXvMkOw==",
"dev": true
},
"node_modules/colorette": {
@ -11490,9 +11490,9 @@
}
},
"node_modules/webpack-dev-server": {
"version": "4.9.3",
"resolved": "https://registry.npmjs.org/webpack-dev-server/-/webpack-dev-server-4.9.3.tgz",
"integrity": "sha512-3qp/eoboZG5/6QgiZ3llN8TUzkSpYg1Ko9khWX1h40MIEUNS2mDoIa8aXsPfskER+GbTvs/IJZ1QTBBhhuetSw==",
"version": "4.10.0",
"resolved": "https://registry.npmjs.org/webpack-dev-server/-/webpack-dev-server-4.10.0.tgz",
"integrity": "sha512-7dezwAs+k6yXVFZ+MaL8VnE+APobiO3zvpp3rBHe/HmWQ+avwh0Q3d0xxacOiBybZZ3syTZw9HXzpa3YNbAZDQ==",
"dev": true,
"dependencies": {
"@types/bonjour": "^3.5.9",
@ -15178,9 +15178,9 @@
"dev": true
},
"colord": {
"version": "2.9.2",
"resolved": "https://registry.npmjs.org/colord/-/colord-2.9.2.tgz",
"integrity": "sha512-Uqbg+J445nc1TKn4FoDPS6ZZqAvEDnwrH42yo8B40JSOgSLxMZ/gt3h4nmCtPLQeXhjJJkqBx7SCY35WnIixaQ==",
"version": "2.9.3",
"resolved": "https://registry.npmjs.org/colord/-/colord-2.9.3.tgz",
"integrity": "sha512-jeC1axXpnb0/2nn/Y1LPuLdgXBLH7aDcHu4KEKfqw3CUhX7ZpfBSlPKyqXE6btIgEzfWtrX3/tyBCaCvXvMkOw==",
"dev": true
},
"colorette": {
@ -20481,9 +20481,9 @@
}
},
"webpack-dev-server": {
"version": "4.9.3",
"resolved": "https://registry.npmjs.org/webpack-dev-server/-/webpack-dev-server-4.9.3.tgz",
"integrity": "sha512-3qp/eoboZG5/6QgiZ3llN8TUzkSpYg1Ko9khWX1h40MIEUNS2mDoIa8aXsPfskER+GbTvs/IJZ1QTBBhhuetSw==",
"version": "4.10.0",
"resolved": "https://registry.npmjs.org/webpack-dev-server/-/webpack-dev-server-4.10.0.tgz",
"integrity": "sha512-7dezwAs+k6yXVFZ+MaL8VnE+APobiO3zvpp3rBHe/HmWQ+avwh0Q3d0xxacOiBybZZ3syTZw9HXzpa3YNbAZDQ==",
"dev": true,
"requires": {
"@types/bonjour": "^3.5.9",

View File

@ -9,7 +9,7 @@ interface IGamepad {
readonly mapping: string;
readonly axes: number[];
readonly buttons: IGamepadButton[];
vibrationActuator: IVibrationActuator;
readonly vibrationActuator: IVibrationActuator;
}
export default IGamepad;

View File

@ -2,14 +2,15 @@ import TPatternUnit from '@/models/TPatternUnit';
import IGamepad from '@/models/IGamepad';
interface IVibrator {
unit: IGamepad;
readonly id: number;
readonly canVibrate: boolean;
isVibrating: boolean;
unit: IGamepad;
interval: number;
update(): void;
loop(pattern: TPatternUnit[]): void;
vibrate(pattern: TPatternUnit): void;
reset(): void;
vibrate(pattern: TPatternUnit[]): void;
sleep(ms: number): Promise<number>;
}

View File

@ -3,10 +3,11 @@ import IGamepad from '@/models/IGamepad';
import IVibrator from '@/models/IVibrator';
class Vibrator implements IVibrator {
unit: IGamepad;
readonly id: number;
readonly canVibrate: boolean;
isVibrating: boolean;
unit: IGamepad;
interval: number;
constructor(unit: IGamepad) {
@ -23,18 +24,13 @@ class Vibrator implements IVibrator {
this.unit = gamepads[this.unit.index] as unknown as IGamepad;
}
reset(): void {
this.isVibrating = false;
this.unit.vibrationActuator.reset();
}
async vibrate(pattern: TPatternUnit[]) {
async loop(pattern: TPatternUnit[]) {
this.isVibrating = true;
const offsetTime = 10;
while (this.isVibrating === true) {
for (let i = 0; i < pattern.length; i++) {
if (this.isVibrating === true) {
this.unit.vibrationActuator.playEffect('dual-rumble', pattern[i]);
this.vibrate(pattern[i]);
await this.sleep(pattern[i].startDelay + pattern[i].duration - offsetTime);
} else {
return;
@ -43,6 +39,15 @@ class Vibrator implements IVibrator {
}
}
vibrate(pattern: TPatternUnit): void {
this.unit.vibrationActuator.playEffect('dual-rumble', pattern);
}
reset(): void {
this.isVibrating = false;
this.unit.vibrationActuator.reset();
}
sleep(ms: number): Promise<number> {
return new Promise(resolve => setTimeout(resolve, ms));
}

View File

@ -2,7 +2,7 @@ import { createStore, Store } from 'vuex';
import IRootState from './models/IRootState';
import MGamepads from '@/store/modules/MGamepads';
import MPatterns from '@/store/modules/MPatterns';
import TPatternUnit from '../models/TPatternUnit';
import TPatternUnit from '@/models/TPatternUnit';
const store: Store<IRootState> = createStore({
state: () => ({
@ -41,10 +41,7 @@ const store: Store<IRootState> = createStore({
}
if (context.getters.isActive === true) {
context.dispatch('reset');
context.dispatch(
'vibrate',
context.getters.patterns[context.getters.mode].pattern
);
context.dispatch('loop', context.getters.patterns[context.getters.mode].pattern);
} else {
context.dispatch('reset');
}
@ -53,7 +50,7 @@ const store: Store<IRootState> = createStore({
context.dispatch('setIsActive', false);
context.dispatch('setMode', 0);
context.dispatch('reset');
context.dispatch('vibrate', pattern);
context.dispatch('loop', pattern);
},
},
modules: {

View File

@ -45,9 +45,17 @@ const MGamepads: Module<IGamepadsState, IRootState> = {
}
});
},
vibrate: function (
loop: function (
context: ActionContext<IGamepadsState, IRootState>,
pattern: TPatternUnit[]
): void {
context.getters.gamepads.forEach((gamepad: Vibrator) => {
gamepad.loop(pattern);
});
},
vibrate: function (
context: ActionContext<IGamepadsState, IRootState>,
pattern: TPatternUnit
): void {
context.getters.gamepads.forEach((gamepad: Vibrator) => {
gamepad.vibrate(pattern);