GameBasicSettings.ts 1.47 KB
import { LocalStorage } from "simba-localstorage";
import { debounce } from "simba-utils";
import { SettingEvents } from "../../event/BaseEvents";

let musicVolume = LocalStorage.getFloat("_musicVolume", 1);
let soundVolume = LocalStorage.getFloat("_soundVolume", 1);
let enableVibration = LocalStorage.getBoolean("_enableVibration", true);

const saveMusicVolume = debounce(() => {
    LocalStorage.setFloat("_musicVolume", musicVolume);
}, 500);

const saveSoundVolume = debounce(() => {
    LocalStorage.setFloat("_soundVolume", soundVolume);
}, 500);

const saveEnableVibration = debounce(() => {
    LocalStorage.setBoolean("_enableVibration", enableVibration);
}, 500);

export const GameBasicSettings = {
    get musicVolume() { return musicVolume; },
    set musicVolume(v: number) {
        v = Math.min(1, Math.max(0, v));
        if (v != musicVolume) {
            musicVolume = v;
            SettingEvents.MusicVolumeChange.emit(musicVolume);
            saveMusicVolume();
        }
    },
    get soundVolume() { return soundVolume; },
    set soundVolume(v: number) {
        v = Math.min(1, Math.max(0, v));
        if (v != soundVolume) {
            soundVolume = v;
            SettingEvents.SoundVolumeChange.emit(soundVolume);
            saveSoundVolume();
        }
    },
    get enableVibration() { return enableVibration; },
    set enableVibration(v: boolean) {
        enableVibration = v;
        saveEnableVibration();
    }
}