using System;
using UnityEngine;
namespace Luneraria.UI
{
///
/// Player preferences of the HUD and options (port of scripts/autoload/preferences.gd as far as
/// the HUD uses it): UI size, reduced motion, volume, mute, full screen; since U5.4 also the
/// ambient occlusion (SSAO). Stored in PlayerPrefs. Tests can set values without touching the
/// stored ones (Persist = false); "-luneSsao 0|1" on the command line overrides the SSAO choice
/// for one session (benchmark).
///
public static class LunePreferences
{
public static event Action Changed;
/// False in tests: values change for the session only.
public static bool Persist = true;
static float _uiScale = -1f;
static int _reducedMotion = -1;
static float _volume = -1f;
static int _muted = -1;
static int _fullscreen = -1;
static int _ambientOcclusion = -1;
static int _extendedRules = -1;
///
/// S1: start matches with the extended rules (champion income, barracks, buyback,
/// Mondsegen; bots may pick Sylvara); on by default. Off = the reference rules of the port.
///
public static bool ExtendedRules
{
get
{
if (_extendedRules < 0)
_extendedRules = PlayerPrefs.GetInt("lune.extended_rules", 1);
return _extendedRules == 1;
}
set { _extendedRules = value ? 1 : 0; Save("lune.extended_rules", _extendedRules); }
}
public static float UiScale
{
get
{
if (_uiScale < 0f)
_uiScale = PlayerPrefs.GetFloat("lune.ui_scale", 1f);
return _uiScale;
}
set { _uiScale = value; Save("lune.ui_scale", value); }
}
public static bool ReducedMotion
{
get
{
if (_reducedMotion < 0)
_reducedMotion = PlayerPrefs.GetInt("lune.reduced_motion", 0);
return _reducedMotion == 1;
}
set { _reducedMotion = value ? 1 : 0; Save("lune.reduced_motion", _reducedMotion); }
}
public static float Volume
{
get
{
if (_volume < 0f)
_volume = PlayerPrefs.GetFloat("lune.volume", .8f);
return _volume;
}
set { _volume = Mathf.Clamp01(value); Save("lune.volume", _volume); }
}
public static bool Muted
{
get
{
if (_muted < 0)
_muted = PlayerPrefs.GetInt("lune.muted", 0);
return _muted == 1;
}
set { _muted = value ? 1 : 0; Save("lune.muted", _muted); }
}
public static bool Fullscreen
{
get
{
if (_fullscreen < 0)
_fullscreen = PlayerPrefs.GetInt("lune.fullscreen", Screen.fullScreen ? 1 : 0);
return _fullscreen == 1;
}
set { _fullscreen = value ? 1 : 0; Save("lune.fullscreen", _fullscreen); }
}
/// Screen-space ambient occlusion of the renderer (U5.4); on by default.
public static bool AmbientOcclusion
{
get
{
if (_ambientOcclusion < 0)
{
string[] args = Environment.GetCommandLineArgs();
int at = Array.IndexOf(args, "-luneSsao");
_ambientOcclusion = at >= 0 && at + 1 < args.Length ? (args[at + 1] == "0" ? 0 : 1) : PlayerPrefs.GetInt("lune.ambient_occlusion", 1);
}
return _ambientOcclusion == 1;
}
set { _ambientOcclusion = value ? 1 : 0; Save("lune.ambient_occlusion", _ambientOcclusion); }
}
static void Save(string key, float value)
{
if (Persist)
PlayerPrefs.SetFloat(key, value);
Changed?.Invoke();
}
static void Save(string key, int value)
{
if (Persist)
PlayerPrefs.SetInt(key, value);
Changed?.Invoke();
}
/// Writes pending values (Godot Preferences.commit).
public static void Commit()
{
if (Persist)
PlayerPrefs.Save();
}
/// Session values back to defaults (tests).
public static void ResetSession(float uiScale = 1f, bool reducedMotion = false)
{
Persist = false;
_uiScale = uiScale;
_reducedMotion = reducedMotion ? 1 : 0;
Changed?.Invoke();
}
}
}