| 1 | /** Client graphics preferences (persisted in localStorage). */ |
| 2 | |
| 3 | export const ViewDistance = Object.freeze({ |
| 4 | long: "long", |
| 5 | short: "short" |
| 6 | }); |
| 7 | |
| 8 | export const LightingMode = Object.freeze({ |
| 9 | soft: "soft", |
| 10 | simple: "simple" |
| 11 | }); |
| 12 | |
| 13 | export const BloomMode = Object.freeze({ |
| 14 | on: "on", |
| 15 | off: "off" |
| 16 | }); |
| 17 | |
| 18 | /** Soft edge blend between blendable ground tiles (grass, dirt, …). */ |
| 19 | export const SoftTerrainMode = Object.freeze({ |
| 20 | on: "on", |
| 21 | off: "off" |
| 22 | }); |
| 23 | |
| 24 | export const DEFAULT_GRAPHICS_SETTINGS = Object.freeze({ |
| 25 | viewDistance: ViewDistance.long, |
| 26 | lighting: LightingMode.soft, |
| 27 | bloom: BloomMode.on, |
| 28 | softTerrain: SoftTerrainMode.on |
| 29 | }); |
| 30 | |
| 31 | const STORAGE_KEY = "questward-graphics-settings"; |
| 32 | |
| 33 | export function normalizeGraphicsSettings (value) |
| 34 | { |
| 35 | const raw = value && typeof value === "object" ? value : {}; |
| 36 | const viewDistance = raw.viewDistance === ViewDistance.short |
| 37 | ? ViewDistance.short |
| 38 | : ViewDistance.long; |
| 39 | const lighting = raw.lighting === LightingMode.simple |
| 40 | ? LightingMode.simple |
| 41 | : LightingMode.soft; |
| 42 | const bloom = raw.bloom === BloomMode.off |
| 43 | ? BloomMode.off |
| 44 | : BloomMode.on; |
| 45 | const softTerrain = raw.softTerrain === SoftTerrainMode.off |
| 46 | ? SoftTerrainMode.off |
| 47 | : SoftTerrainMode.on; |
| 48 | return { viewDistance, lighting, bloom, softTerrain }; |
| 49 | } |
| 50 | |
| 51 | export function loadGraphicsSettings () |
| 52 | { |
| 53 | try |
| 54 | { |
| 55 | const text = localStorage.getItem(STORAGE_KEY); |
| 56 | if (!text) |
| 57 | { return { ...DEFAULT_GRAPHICS_SETTINGS }; } |
| 58 | return normalizeGraphicsSettings(JSON.parse(text)); |
| 59 | } |
| 60 | catch |
| 61 | { |
| 62 | return { ...DEFAULT_GRAPHICS_SETTINGS }; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | export function saveGraphicsSettings (settings) |
| 67 | { |
| 68 | const next = normalizeGraphicsSettings(settings); |
| 69 | try |
| 70 | { localStorage.setItem(STORAGE_KEY, JSON.stringify(next)); } |
| 71 | catch |
| 72 | { /* ignore quota / private mode */ } |
| 73 | return next; |
| 74 | } |
| 75 | |
| 76 | /** Map-ring Chebyshev radius for chunk subscriptions (entities always use VISIBLE_CHUNK_RADIUS). */ |
| 77 | export function staticChunkRadiusForViewDistance (viewDistance, longRadius, shortRadius) |
| 78 | { |
| 79 | return viewDistance === ViewDistance.short ? shortRadius : longRadius; |
| 80 | } |