From d340a6e81ddbd1d6c57c87a46cd8342e2bf4fd8b Mon Sep 17 00:00:00 2001 From: Eugene Serb <46799701+eugene-serb@users.noreply.github.com> Date: Wed, 10 Aug 2022 17:46:21 +0300 Subject: [PATCH] - 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. --- package-lock.json | 24 ++++++++++++------------ src/models/IGamepad.ts | 2 +- src/models/IVibrator.ts | 5 +++-- src/models/Vibrator.ts | 21 +++++++++++++-------- src/store/index.ts | 9 +++------ src/store/modules/MGamepads.ts | 10 +++++++++- 6 files changed, 41 insertions(+), 30 deletions(-) diff --git a/package-lock.json b/package-lock.json index b3c89bd..fea7bde 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/src/models/IGamepad.ts b/src/models/IGamepad.ts index c5a7b2b..d24fe99 100644 --- a/src/models/IGamepad.ts +++ b/src/models/IGamepad.ts @@ -9,7 +9,7 @@ interface IGamepad { readonly mapping: string; readonly axes: number[]; readonly buttons: IGamepadButton[]; - vibrationActuator: IVibrationActuator; + readonly vibrationActuator: IVibrationActuator; } export default IGamepad; diff --git a/src/models/IVibrator.ts b/src/models/IVibrator.ts index faecd7e..fd1b02f 100644 --- a/src/models/IVibrator.ts +++ b/src/models/IVibrator.ts @@ -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; } diff --git a/src/models/Vibrator.ts b/src/models/Vibrator.ts index a2ab22f..c5ed7d5 100644 --- a/src/models/Vibrator.ts +++ b/src/models/Vibrator.ts @@ -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 { return new Promise(resolve => setTimeout(resolve, ms)); } diff --git a/src/store/index.ts b/src/store/index.ts index fabcdec..9f8738a 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -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 = createStore({ state: () => ({ @@ -41,10 +41,7 @@ const store: Store = 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 = createStore({ context.dispatch('setIsActive', false); context.dispatch('setMode', 0); context.dispatch('reset'); - context.dispatch('vibrate', pattern); + context.dispatch('loop', pattern); }, }, modules: { diff --git a/src/store/modules/MGamepads.ts b/src/store/modules/MGamepads.ts index f0a3780..5948613 100644 --- a/src/store/modules/MGamepads.ts +++ b/src/store/modules/MGamepads.ts @@ -45,9 +45,17 @@ const MGamepads: Module = { } }); }, - vibrate: function ( + loop: function ( context: ActionContext, pattern: TPatternUnit[] + ): void { + context.getters.gamepads.forEach((gamepad: Vibrator) => { + gamepad.loop(pattern); + }); + }, + vibrate: function ( + context: ActionContext, + pattern: TPatternUnit ): void { context.getters.gamepads.forEach((gamepad: Vibrator) => { gamepad.vibrate(pattern);