| 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 | export const DEFAULT_GRAPHICS_SETTINGS = Object.freeze({ |
| 19 | viewDistance: ViewDistance.long, |
| 20 | lighting: LightingMode.soft, |
| 21 | bloom: BloomMode.on |
| 22 | }); |
| 23 | |
| 24 | const STORAGE_KEY = "tile-mmo-graphics-settings"; |
| 25 | |
| 26 | export function normalizeGraphicsSettings (value) |
| 27 | { |
| 28 | const raw = value && typeof value === "object" ? value : {}; |
| 29 | const viewDistance = raw.viewDistance === ViewDistance.short |
| 30 | ? ViewDistance.short |
| 31 | : ViewDistance.long; |
| 32 | const lighting = raw.lighting === LightingMode.simple |
| 33 | ? LightingMode.simple |
| 34 | : LightingMode.soft; |
| 35 | const bloom = raw.bloom === BloomMode.off |
| 36 | ? BloomMode.off |
| 37 | : BloomMode.on; |
| 38 | return { viewDistance, lighting, bloom }; |
| 39 | } |
| 40 | |
| 41 | export function loadGraphicsSettings () |
| 42 | { |
| 43 | try |
| 44 | { |
| 45 | const text = localStorage.getItem(STORAGE_KEY); |
| 46 | if (!text) |
| 47 | { return { ...DEFAULT_GRAPHICS_SETTINGS }; } |
| 48 | return normalizeGraphicsSettings(JSON.parse(text)); |
| 49 | } |
| 50 | catch |
| 51 | { |
| 52 | return { ...DEFAULT_GRAPHICS_SETTINGS }; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | export function saveGraphicsSettings (settings) |
| 57 | { |
| 58 | const next = normalizeGraphicsSettings(settings); |
| 59 | try |
| 60 | { localStorage.setItem(STORAGE_KEY, JSON.stringify(next)); } |
| 61 | catch |
| 62 | { /* ignore quota / private mode */ } |
| 63 | return next; |
| 64 | } |
| 65 | |
| 66 | /** Map-ring Chebyshev radius for chunk subscriptions (entities always use VISIBLE_CHUNK_RADIUS). */ |
| 67 | export function staticChunkRadiusForViewDistance (viewDistance, longRadius, shortRadius) |
| 68 | { |
| 69 | return viewDistance === ViewDistance.short ? shortRadius : longRadius; |
| 70 | } |