Fix vibration on iphone

This commit is contained in:
schmelczerandras 2020-10-28 16:34:11 +01:00
parent 7162dc69b9
commit 45b6588701
4 changed files with 16 additions and 10 deletions

View file

@ -29,6 +29,7 @@ import { PlayerDecision } from './join-form-handler';
import { GameObjectContainer } from './objects/game-object-container'; import { GameObjectContainer } from './objects/game-object-container';
import { OptionsHandler } from './options-handler'; import { OptionsHandler } from './options-handler';
import parser from 'socket.io-msgpack-parser'; import parser from 'socket.io-msgpack-parser';
import { VibrationHandler } from './vibration-handler';
export class Game extends CommandReceiver { export class Game extends CommandReceiver {
public gameObjects = new GameObjectContainer(this); public gameObjects = new GameObjectContainer(this);
@ -123,11 +124,7 @@ export class Game extends CommandReceiver {
protected commandExecutors: CommandExecutors = { protected commandExecutors: CommandExecutors = {
[ServerAnnouncement.type]: (c: ServerAnnouncement) => [ServerAnnouncement.type]: (c: ServerAnnouncement) =>
(this.lastAnnouncementText = c.text), (this.lastAnnouncementText = c.text),
[PlayerDiedCommand.type]: (c: PlayerDiedCommand) => { [PlayerDiedCommand.type]: (c: PlayerDiedCommand) => VibrationHandler.vibrate(150),
if (OptionsHandler.options.vibrationEnabled) {
navigator.vibrate(150);
}
},
[UpdateGameState.type]: (c: UpdateGameState) => (this.lastGameState = c), [UpdateGameState.type]: (c: UpdateGameState) => (this.lastGameState = c),
[GameEnd.type]: (c: GameEnd) => { [GameEnd.type]: (c: GameEnd) => {
const team = const team =

View file

@ -1,10 +1,9 @@
import { vec2 } from 'gl-matrix'; import { vec2 } from 'gl-matrix';
import { Renderer } from 'sdf-2d'; import { Renderer } from 'sdf-2d';
import { Circle, Id, PlayerCharacterBase, CharacterTeam, settings } from 'shared'; import { Circle, Id, PlayerCharacterBase, CharacterTeam, settings } from 'shared';
import { OptionsHandler } from '../options-handler';
import { BlobShape } from '../shapes/blob-shape'; import { BlobShape } from '../shapes/blob-shape';
import { SoundHandler, Sounds } from '../sound-handler'; import { SoundHandler, Sounds } from '../sound-handler';
import { VibrationHandler } from '../vibration-handler';
import { ViewObject } from './view-object'; import { ViewObject } from './view-object';
export class PlayerCharacterView extends PlayerCharacterBase implements ViewObject { export class PlayerCharacterView extends PlayerCharacterBase implements ViewObject {
@ -52,8 +51,8 @@ export class PlayerCharacterView extends PlayerCharacterBase implements ViewObje
public step(deltaTimeInSeconds: number): void { public step(deltaTimeInSeconds: number): void {
if (this.previousHealth > this.health) { if (this.previousHealth > this.health) {
if (this.isMainCharacter && OptionsHandler.options.vibrationEnabled) { if (this.isMainCharacter) {
navigator.vibrate(Math.min(200, (this.previousHealth - this.health) * 4)); VibrationHandler.vibrate(Math.min(200, (this.previousHealth - this.health) * 4));
} }
this.previousHealth = this.health; this.previousHealth = this.health;

View file

@ -1,4 +1,5 @@
import { SoundHandler, Sounds } from './sound-handler'; import { SoundHandler, Sounds } from './sound-handler';
import { VibrationHandler } from './vibration-handler';
interface Options { interface Options {
vibrationEnabled: boolean; vibrationEnabled: boolean;
@ -47,7 +48,7 @@ export abstract class OptionsHandler {
} }
if (this.checked && k === 'vibrationEnabled') { if (this.checked && k === 'vibrationEnabled') {
navigator.vibrate(100); VibrationHandler.vibrate(100);
} }
SoundHandler.play(Sounds.click); SoundHandler.play(Sounds.click);

View file

@ -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);
}
}
}