Skip to content

Commit

Permalink
Vibrate the device on game lost (#454)
Browse files Browse the repository at this point in the history
* vibrate on lost

* add preference in setting view

* move to constants, slight naming change
  • Loading branch information
kosamari authored May 15, 2019
1 parent d547130 commit 70a4121
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/services/preact-canvas/components/game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { bind } from "src/utils/bind";
import { GameChangeCallback } from "../..";
import { StateChange } from "../../../../gamelogic";
import { Cell, PlayMode } from "../../../../gamelogic/types";
import { vibrationLength } from "../../../../utils/constants";
import initFocusHandling from "../../../../utils/focus-visible";
import { isFeaturePhone } from "../../../../utils/static-display";
import Board from "../board";
Expand Down Expand Up @@ -47,6 +48,7 @@ export interface Props {
toRevealTotal: number;
useMotion: boolean;
bestTime?: number;
useVibration: boolean;
}

interface State {
Expand Down Expand Up @@ -186,6 +188,9 @@ export default class Game extends Component<Props, State> {
this._tryAgainBtn
) {
this._tryAgainBtn.focus();
if (this.props.useVibration) {
navigator.vibrate(vibrationLength);
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/services/preact-canvas/components/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ interface Props {
disableAnimationBtn: boolean;
texturePromise: Promise<any>;
supportsSufficientWebGL: boolean;
useVibration: boolean;
onVibrationPrefChange: () => void;
}

interface State {}
Expand All @@ -48,7 +50,9 @@ export default class Settings extends Component<Props, State> {
motion,
texturePromise,
supportsSufficientWebGL,
disableAnimationBtn
disableAnimationBtn,
useVibration,
onVibrationPrefChange
}: Props) {
const closeBtn = isFeaturePhone ? (
<button
Expand Down Expand Up @@ -87,6 +91,12 @@ export default class Settings extends Component<Props, State> {
>
Animations {motion ? "on" : "off"}
</button>
<button
class={useVibration ? btnOnStyle : btnOffStyle}
onClick={onVibrationPrefChange}
>
Vibrate {useVibration ? "on" : "off"}
</button>
<About
motion={motion}
texturePromise={texturePromise}
Expand Down
21 changes: 18 additions & 3 deletions src/services/preact-canvas/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ interface State {
motionPreference: boolean;
gameInPlay: boolean;
allowIntroAnim: boolean;
vibrationPreference: boolean;
}

export type GameChangeCallback = (stateChange: GameStateChange) => void;
Expand All @@ -123,7 +124,8 @@ export default class Root extends Component<Props, State> {
settingsOpen: false,
motionPreference: true,
gameInPlay: false,
allowIntroAnim: true
allowIntroAnim: true,
vibrationPreference: true
};
private previousFocus: HTMLElement | null = null;

Expand All @@ -144,7 +146,8 @@ export default class Root extends Component<Props, State> {
lazyImport!.initOffline();

this.setState({
motionPreference: await lazyImport!.shouldUseMotion()
motionPreference: await lazyImport!.shouldUseMotion(),
vibrationPreference: await lazyImport!.getVibrationPreference()
});
});

Expand Down Expand Up @@ -220,7 +223,8 @@ export default class Root extends Component<Props, State> {
motionPreference,
gameInPlay,
bestTime,
allowIntroAnim
allowIntroAnim,
vibrationPreference
}: State
) {
let mainComponent: VNode;
Expand All @@ -242,6 +246,8 @@ export default class Root extends Component<Props, State> {
}
supportsSufficientWebGL={lazyImport!.supportsSufficientWebGL}
texturePromise={texturePromise}
useVibration={vibrationPreference}
onVibrationPrefChange={this._onVibrationPrefChange}
/>
)}
/>
Expand Down Expand Up @@ -272,6 +278,7 @@ export default class Root extends Component<Props, State> {
onDangerModeChange={this._onDangerModeChange}
useMotion={motionPreference}
bestTime={bestTime}
useVibration={vibrationPreference}
/>
)}
/>
Expand Down Expand Up @@ -348,6 +355,14 @@ export default class Root extends Component<Props, State> {
setMotionPreference(motionPreference);
}

@bind
private async _onVibrationPrefChange() {
const vibrationPreference = !this.state.vibrationPreference;
this.setState({ vibrationPreference });
const { setVibrationPreference } = await lazyImportReady;
setVibrationPreference(vibrationPreference);
}

@bind
private _onDangerModeChange(dangerMode: boolean) {
this.setState({ dangerMode });
Expand Down
8 changes: 7 additions & 1 deletion src/services/preact-canvas/lazy-load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import {
setMotionPreference,
shouldUseMotion
} from "../state/motion-preference";
import {
getVibrationPreference,
setVibrationPreference
} from "../state/vibration-preference";

export {
supportsSufficientWebGL,
Expand All @@ -36,5 +40,7 @@ export {
lazyGenerateTextures,
setMotionPreference,
getMotionPreference,
shouldUseMotion
shouldUseMotion,
getVibrationPreference,
setVibrationPreference
};
21 changes: 21 additions & 0 deletions src/services/state/vibration-preference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { get, set } from "idb-keyval";

const DEFAULT: boolean = true;

/**
* Set vibration preference (true means will vibrate)
*
* @param vibrate
*/
export async function setVibrationPreference(vibrate: boolean): Promise<void> {
await set("vibrate", vibrate);
}

export async function getVibrationPreference(): Promise<boolean> {
const vibrate = await get("vibrate");
if (typeof vibrate === "boolean") {
return vibrate;
}
// if no value is assigned to "vibrate", return default value
return DEFAULT;
}
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ export const forceMotionMode =
: forceMotionParam === "1"
? true
: undefined;

export const vibrationLength = 300;

0 comments on commit 70a4121

Please sign in to comment.