Add game settings

This commit is contained in:
Flashfyre committed 2023-10-26 16:33:59 -04:00
1 parent 23a21af173
commit d3f55ad4ae
10 files changed
+388 -59

No files matched your search

+32 -3
View File
@@ -14,6 +14,7 @@ import { GameMode } from "../game-mode";
import { BattleType } from "../battle";
import TrainerData from "./trainer-data";
import { trainerConfigs } from "../data/trainer-type";
import { Setting, setSetting, settingDefaults } from "./settings";
interface SystemSaveData {
trainerId: integer;
@@ -81,8 +82,9 @@ export class GameData {
constructor(scene: BattleScene) {
this.scene = scene;
this.trainerId = Utils.randInt(65536);
this.secretId = Utils.randInt(65536);
this.loadSettings();
this.trainerId = Utils.randSeedInt(65536);
this.secretId = Utils.randSeedInt(65536);
this.unlocks = {
[Unlockables.ENDLESS_MODE]: false,
[Unlockables.MINI_BLACK_HOLE]: false
@@ -109,7 +111,7 @@ export class GameData {
}
private loadSystem(): boolean {
if (!localStorage.getItem('data'))
if (!localStorage.hasOwnProperty('data'))
return false;
const data = JSON.parse(atob(localStorage.getItem('data'))) as SystemSaveData;
@@ -133,6 +135,33 @@ export class GameData {
return true;
}
public saveSetting(setting: Setting, valueIndex: integer): boolean {
let settings: object = {};
if (localStorage.hasOwnProperty('settings'))
settings = JSON.parse(localStorage.getItem('settings'));
setSetting(this.scene, setting as Setting, valueIndex);
Object.keys(settingDefaults).forEach(s => {
if (s === setting)
settings[s] = valueIndex;
});
localStorage.setItem('settings', JSON.stringify(settings));
return true;
}
private loadSettings(): boolean {
if (!localStorage.hasOwnProperty('settings'))
return false;
const settings = JSON.parse(localStorage.getItem('settings'));
for (let setting of Object.keys(settings))
setSetting(this.scene, setting as Setting, settings[setting]);
}
saveSession(scene: BattleScene): boolean {
const sessionData = {
seed: scene.seed,
+52
View File
@@ -0,0 +1,52 @@
import BattleScene from "../battle-scene";
export enum Setting {
Game_Speed = "GAME_SPEED",
Master_Volume = "MASTER_VOLUME",
BGM_Volume = "BGM_VOLUME",
SE_Volume = "SE_VOLUME"
}
export interface SettingOptions {
[key: string]: string[]
}
export interface SettingDefaults {
[key: string]: integer
}
export const settingOptions: SettingOptions = {
[Setting.Game_Speed]: [ '1x', '1.25x', '1.5x', '2x', '2.5x', '3x', '4x', '5x' ],
[Setting.Master_Volume]: new Array(11).fill(null).map((_, i) => i ? (i * 10).toString() : 'Mute'),
[Setting.BGM_Volume]: new Array(11).fill(null).map((_, i) => i ? (i * 10).toString() : 'Mute'),
[Setting.SE_Volume]: new Array(11).fill(null).map((_, i) => i ? (i * 10).toString() : 'Mute')
};
export const settingDefaults: SettingDefaults = {
[Setting.Game_Speed]: 0,
[Setting.Master_Volume]: 5,
[Setting.BGM_Volume]: 10,
[Setting.SE_Volume]: 10
};
export function setSetting(scene: BattleScene, setting: Setting, value: integer): boolean {
switch (setting) {
case Setting.Game_Speed:
scene.gameSpeed = parseFloat(settingOptions[setting][value].replace('x', ''));
break;
case Setting.Master_Volume:
scene.masterVolume = value ? parseInt(settingOptions[setting][value]) * 0.01 : 0;
scene.updateSoundVolume();
break;
case Setting.BGM_Volume:
scene.bgmVolume = value ? parseInt(settingOptions[setting][value]) * 0.01 : 0;
scene.updateSoundVolume();
break;
case Setting.SE_Volume:
scene.seVolume = value ? parseInt(settingOptions[setting][value]) * 0.01 : 0;
scene.updateSoundVolume();
break;
}
return true;
}