main init
This commit is contained in:
58
Library/mobile/kit/haptics.ts
Normal file
58
Library/mobile/kit/haptics.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
// Kit haptics (v1) - vibration pulses, silent no-op outside the native app.
|
||||
// From _Tools\Library\mobile\kit (XP-Battleship/src/haptics.ts). Needs @capacitor/haptics.
|
||||
|
||||
import { Capacitor } from "@capacitor/core";
|
||||
import { Haptics, ImpactStyle } from "@capacitor/haptics";
|
||||
|
||||
let storageKey = "app_haptics_off";
|
||||
let off = false;
|
||||
|
||||
/** Per-device preference (like mute) - not part of the save / cloud sync. */
|
||||
export function configureHaptics(key: string): void {
|
||||
storageKey = `${key}_haptics_off`;
|
||||
try {
|
||||
off = localStorage.getItem(storageKey) === "1";
|
||||
} catch {
|
||||
off = false;
|
||||
}
|
||||
}
|
||||
|
||||
export function isHapticsEnabled(): boolean {
|
||||
return !off;
|
||||
}
|
||||
|
||||
export function toggleHaptics(): boolean {
|
||||
off = !off;
|
||||
try {
|
||||
localStorage.setItem(storageKey, off ? "1" : "0");
|
||||
} catch {
|
||||
// session only
|
||||
}
|
||||
return !off;
|
||||
}
|
||||
|
||||
async function impact(style: ImpactStyle): Promise<void> {
|
||||
if (!Capacitor.isNativePlatform() || off) return;
|
||||
try {
|
||||
await Haptics.impact({ style });
|
||||
} catch {
|
||||
// unsupported device
|
||||
}
|
||||
}
|
||||
|
||||
let lastLight = 0;
|
||||
/** Frequent events - throttled so fast gameplay does not become constant buzzing. */
|
||||
export function hapticLight(throttleMs = 150): void {
|
||||
const now = performance.now();
|
||||
if (now - lastLight < throttleMs) return;
|
||||
lastLight = now;
|
||||
void impact(ImpactStyle.Light);
|
||||
}
|
||||
|
||||
export function hapticMedium(): void {
|
||||
void impact(ImpactStyle.Medium);
|
||||
}
|
||||
|
||||
export function hapticHeavy(): void {
|
||||
void impact(ImpactStyle.Heavy);
|
||||
}
|
||||
Reference in New Issue
Block a user