diff --git a/frontend/src/scripts/game.ts b/frontend/src/scripts/game.ts index d2610a6..13eaab4 100644 --- a/frontend/src/scripts/game.ts +++ b/frontend/src/scripts/game.ts @@ -29,6 +29,7 @@ import { PlayerDecision } from './join-form-handler'; import { GameObjectContainer } from './objects/game-object-container'; import { OptionsHandler } from './options-handler'; import parser from 'socket.io-msgpack-parser'; +import { VibrationHandler } from './vibration-handler'; export class Game extends CommandReceiver { public gameObjects = new GameObjectContainer(this); @@ -123,11 +124,7 @@ export class Game extends CommandReceiver { protected commandExecutors: CommandExecutors = { [ServerAnnouncement.type]: (c: ServerAnnouncement) => (this.lastAnnouncementText = c.text), - [PlayerDiedCommand.type]: (c: PlayerDiedCommand) => { - if (OptionsHandler.options.vibrationEnabled) { - navigator.vibrate(150); - } - }, + [PlayerDiedCommand.type]: (c: PlayerDiedCommand) => VibrationHandler.vibrate(150), [UpdateGameState.type]: (c: UpdateGameState) => (this.lastGameState = c), [GameEnd.type]: (c: GameEnd) => { const team = diff --git a/frontend/src/scripts/objects/player-character-view.ts b/frontend/src/scripts/objects/player-character-view.ts index abd8ccb..e453cc9 100644 --- a/frontend/src/scripts/objects/player-character-view.ts +++ b/frontend/src/scripts/objects/player-character-view.ts @@ -1,10 +1,9 @@ import { vec2 } from 'gl-matrix'; import { Renderer } from 'sdf-2d'; import { Circle, Id, PlayerCharacterBase, CharacterTeam, settings } from 'shared'; -import { OptionsHandler } from '../options-handler'; - import { BlobShape } from '../shapes/blob-shape'; import { SoundHandler, Sounds } from '../sound-handler'; +import { VibrationHandler } from '../vibration-handler'; import { ViewObject } from './view-object'; export class PlayerCharacterView extends PlayerCharacterBase implements ViewObject { @@ -52,8 +51,8 @@ export class PlayerCharacterView extends PlayerCharacterBase implements ViewObje public step(deltaTimeInSeconds: number): void { if (this.previousHealth > this.health) { - if (this.isMainCharacter && OptionsHandler.options.vibrationEnabled) { - navigator.vibrate(Math.min(200, (this.previousHealth - this.health) * 4)); + if (this.isMainCharacter) { + VibrationHandler.vibrate(Math.min(200, (this.previousHealth - this.health) * 4)); } this.previousHealth = this.health; diff --git a/frontend/src/scripts/options-handler.ts b/frontend/src/scripts/options-handler.ts index 3a59bdc..cd61431 100644 --- a/frontend/src/scripts/options-handler.ts +++ b/frontend/src/scripts/options-handler.ts @@ -1,4 +1,5 @@ import { SoundHandler, Sounds } from './sound-handler'; +import { VibrationHandler } from './vibration-handler'; interface Options { vibrationEnabled: boolean; @@ -47,7 +48,7 @@ export abstract class OptionsHandler { } if (this.checked && k === 'vibrationEnabled') { - navigator.vibrate(100); + VibrationHandler.vibrate(100); } SoundHandler.play(Sounds.click); diff --git a/frontend/src/scripts/vibration-handler.ts b/frontend/src/scripts/vibration-handler.ts new file mode 100644 index 0000000..7873ff7 --- /dev/null +++ b/frontend/src/scripts/vibration-handler.ts @@ -0,0 +1,9 @@ +import { OptionsHandler } from './options-handler'; + +export abstract class VibrationHandler { + public static vibrate(time: number): void { + if (OptionsHandler.options.vibrationEnabled) { + navigator?.vibrate(time); + } + } +}