using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace GrassCutter
{
public static class GameSettings
{
// ───────────────────────── sound ─────────────────────────
public static float Master { get => PlayerPrefs.GetFloat("s_master", 0.9f); set { PlayerPrefs.SetFloat("s_master", value); Apply(); } }
public static float Music { get => PlayerPrefs.GetFloat("s_music", 0.55f); set { PlayerPrefs.SetFloat("s_music", value); Apply(); } }
public static float Effects { get => PlayerPrefs.GetFloat("s_sfx", 1f); set { PlayerPrefs.SetFloat("s_sfx", value); Apply(); } }
public static float Voices { get => PlayerPrefs.GetFloat("s_voice", 1f); set { PlayerPrefs.SetFloat("s_voice", value); Apply(); } }
public static float PhoneVolume { get => PlayerPrefs.GetFloat("s_phone", 0.7f); set => PlayerPrefs.SetFloat("s_phone", value); }
/// Keep playing sound while the game is in the background.
public static bool BackgroundSound { get => PlayerPrefs.GetInt("s_bgsound", 1) == 1; set { PlayerPrefs.SetInt("s_bgsound", value ? 1 : 0); ApplyFocus(Application.isFocused); } }
/// Who you go as. Until you choose, someone at random, so a new crew doesn't arrive as eight Hanks.
public static int Character
{
get
{
int i = PlayerPrefs.GetInt("s_crew", -1);
if (i >= 0 && i < Roster.All.Length) return i;
i = Random.Range(0, Roster.All.Length);
PlayerPrefs.SetInt("s_crew", i);
return i;
}
set => PlayerPrefs.SetInt("s_crew", value);
}
// ───────────────────────── game ─────────────────────────
/// Interface language code ("en", "ru", "zh-Hans"…), kept as "s_lang". Until chosen: Steam's language, else the system's.
public static string Language { get => Loc.Code; set => Loc.Set(value); }
public static float Shake { get => PlayerPrefs.GetFloat("s_shake", 1f); set { PlayerPrefs.SetFloat("s_shake", value); Apply(); } }
/// The assistant chips in with tips.
public static bool Tips { get => PlayerPrefs.GetInt("s_tips", 1) == 1; set => PlayerPrefs.SetInt("s_tips", value ? 1 : 0); }
/// The key caps down the right of the screen.
public static bool KeyHints { get => PlayerPrefs.GetInt("s_hints", 1) == 1; set => PlayerPrefs.SetInt("s_hints", value ? 1 : 0); }
// ───────────────────────── controls ─────────────────────────
public static float Sensitivity { get => PlayerPrefs.GetFloat("s_sens", 1f); set { PlayerPrefs.SetFloat("s_sens", value); Apply(); } }
public static float PadSensitivity { get => PlayerPrefs.GetFloat("s_padsens", 1f); set { PlayerPrefs.SetFloat("s_padsens", value); Apply(); } }
public static bool InvertY { get => PlayerPrefs.GetInt("s_invy", 0) == 1; set { PlayerPrefs.SetInt("s_invy", value ? 1 : 0); Apply(); } }
public static bool SprintToggle { get => PlayerPrefs.GetInt("s_sprinttog", 0) == 1; set { PlayerPrefs.SetInt("s_sprinttog", value ? 1 : 0); Apply(); } }
// ───────────────────────── display ─────────────────────────
/// 0 fullscreen, 1 borderless window, 2 window. Before there was a choice it was a fullscreen yes/no ("s_fs").
public static int DisplayMode
{
get => PlayerPrefs.GetInt("s_dmode", PlayerPrefs.GetInt("s_fs", 1) == 1 ? 1 : 2);
set { PlayerPrefs.SetInt("s_dmode", value); ApplyScreen(); }
}
/// Width × height for fullscreen and the window; 0 × 0 is the desktop's own.
public static Vector2Int Resolution
{
get => new Vector2Int(PlayerPrefs.GetInt("s_resw", 0), PlayerPrefs.GetInt("s_resh", 0));
set { PlayerPrefs.SetInt("s_resw", value.x); PlayerPrefs.SetInt("s_resh", value.y); ApplyScreen(); }
}
public static bool VSync { get => PlayerPrefs.GetInt("s_vsync", 1) == 1; set { PlayerPrefs.SetInt("s_vsync", value ? 1 : 0); ApplyFrames(); } }
public static readonly int[] FpsCaps = { 30, 60, 90, 120, 144, 165, 240, 0 };
/// Frames a second without v-sync, an index into (0 there is no limit).
public static int FpsCap { get => Mathf.Clamp(PlayerPrefs.GetInt("s_fpscap", 6), 0, FpsCaps.Length - 1); set { PlayerPrefs.SetInt("s_fpscap", value); ApplyFrames(); } }
public static float Fov { get => PlayerPrefs.GetFloat("s_fov", 80f); set { PlayerPrefs.SetFloat("s_fov", value); Apply(); } }
/// Exposure on top of the day's, in stops (-1..1).
public static float Brightness { get => PlayerPrefs.GetFloat("s_bright", 0f); set => PlayerPrefs.SetFloat("s_bright", value); }
// ───────────────────────── graphics ─────────────────────────
/// 0 low, 1 medium, 2 high, 3 ultra, 4 own mix (what the rows below say).
public static int Quality { get => Mathf.Clamp(PlayerPrefs.GetInt("s_quality", 2), 0, 4); set { if (value < Custom) { Preset(value); PlayerPrefs.SetInt("s_quality", value); } else Freeze(); ApplyGraphics(); } }
public const int Custom = 4;
// a row nobody has set follows the preset (an own mix that lacks one follows high)
// low medium high ultra
static readonly float[] PScale = { 0.75f, 0.9f, 1f, 1f };
static readonly int[] PShadows = { 1, 2, 3, 4 };
static readonly int[] PMsaa = { 0, 1, 2, 3 };
static readonly int[] PGrass = { 0, 1, 2, 2 };
static readonly int[] PBloom = { 0, 1, 1, 1 };
static int P => Quality == Custom ? 2 : Quality;
static readonly string[] GraphicsKeys = { "s_rscale", "s_shadows", "s_msaa", "s_grass", "s_bloom" };
public static float RenderScale { get => PlayerPrefs.GetFloat("s_rscale", PScale[P]); set => SetOwn("s_rscale", value); }
/// 0 off, 1 low, 2 medium, 3 high, 4 ultra.
public static int Shadows { get => PlayerPrefs.GetInt("s_shadows", PShadows[P]); set => SetOwn("s_shadows", value); }
/// MSAA: 0 off, 1 2×, 2 4×, 3 8×.
public static int Antialias { get => PlayerPrefs.GetInt("s_msaa", PMsaa[P]); set => SetOwn("s_msaa", value); }
/// Blades of grass drawn: 0 fewer, 1 more, 2 all.
public static int GrassDetail { get => PlayerPrefs.GetInt("s_grass", PGrass[P]); set => SetOwn("s_grass", value); }
public static bool Bloom { get => PlayerPrefs.GetInt("s_bloom", PBloom[P]) == 1; set => SetOwn("s_bloom", value ? 1 : 0); }
/// A single row changed: the others keep what they show, and the preset becomes the player's own mix.
static void SetOwn(string key, float v)
{
Freeze();
PlayerPrefs.SetFloat(key, v);
ApplyGraphics();
}
static void SetOwn(string key, int v)
{
Freeze();
PlayerPrefs.SetInt(key, v);
ApplyGraphics();
}
static void Freeze()
{
if (Quality == Custom) return;
float scale = RenderScale;
int shadows = Shadows, msaa = Antialias, grass = GrassDetail;
bool bloom = Bloom;
PlayerPrefs.SetFloat("s_rscale", scale);
PlayerPrefs.SetInt("s_shadows", shadows);
PlayerPrefs.SetInt("s_msaa", msaa);
PlayerPrefs.SetInt("s_grass", grass);
PlayerPrefs.SetInt("s_bloom", bloom ? 1 : 0);
PlayerPrefs.SetInt("s_quality", Custom);
}
/// A preset: the rows go back to following it.
static void Preset(int q)
{
foreach (var k in GraphicsKeys) PlayerPrefs.DeleteKey(k);
}
/// A tab's settings as they come out of the box.
public static void Defaults(int tab)
{
string[][] keys =
{
new[] { "s_shake", "s_tips", "s_hints" },
new[] { "s_dmode", "s_fs", "s_resw", "s_resh", "s_vsync", "s_fpscap", "s_fov", "s_bright" },
new[] { "s_quality", "s_rscale", "s_shadows", "s_msaa", "s_grass", "s_bloom" },
new[] { "s_master", "s_music", "s_sfx", "s_voice", "s_phone", "s_bgsound" },
new[] { "s_sens", "s_padsens", "s_invy", "s_sprinttog" },
};
if (tab == 5) { Binds.ResetAll(); return; }
if (tab < 0 || tab >= keys.Length) return;
foreach (var k in keys[tab]) PlayerPrefs.DeleteKey(k);
Apply();
ApplyDisplay();
}
// ───────────────────────── applying ─────────────────────────
public static void Apply()
{
Sfx.MasterVolume = Master;
Sfx.MusicVolume = Music;
Sfx.SfxVolume = Effects;
Sfx.VoiceVolume = Voices;
Sfx.ApplyVolumes();
Sfx.RefreshMusicVolume();
Ctl.Sensitivity = Sensitivity;
Ctl.PadSensitivity = PadSensitivity;
Ctl.InvertY = InvertY;
Ctl.SprintToggle = SprintToggle;
CameraFx.BaseFov = Fov;
CameraFx.ShakeScale = Shake;
}
static bool s_hooked;
public static void ApplyDisplay()
{
if (!s_hooked)
{
s_hooked = true;
Application.focusChanged += ApplyFocus;
}
ApplyFrames();
ApplyGraphics();
ApplyScreen();
}
static void ApplyFocus(bool focused) => AudioListener.pause = !focused && !BackgroundSound;
static void ApplyFrames()
{
QualitySettings.vSyncCount = VSync ? 1 : 0;
int cap = FpsCaps[FpsCap];
Application.targetFrameRate = VSync ? -1 : cap > 0 ? cap : -1;
}
static bool Bot => CommandLine.Has("-autodemo") || CommandLine.Has("-mpdemo") || CommandLine.Has("-screenshot");
/// Fullscreen, borderless or a window, at the chosen size. Scripted runs keep the size from the command line.
static void ApplyScreen()
{
if (Application.isEditor || Bot) return;
var mode = DisplayMode switch { 0 => FullScreenMode.ExclusiveFullScreen, 1 => FullScreenMode.FullScreenWindow, _ => FullScreenMode.Windowed };
var r = Resolution;
int w = r.x, h = r.y;
if (mode == FullScreenMode.FullScreenWindow || w <= 0 || h <= 0) { w = Display.main.systemWidth; h = Display.main.systemHeight; }
if (mode == FullScreenMode.Windowed && (r.x <= 0 || r.y <= 0)) { w = Mathf.Min(1600, w); h = Mathf.Min(900, h); }
if (Screen.fullScreenMode != mode || Screen.width != w || Screen.height != h) Screen.SetResolution(w, h, mode);
}
/// The sizes this screen offers, biggest first, one per width × height.
public static List Resolutions()
{
var list = new List();
foreach (var r in Screen.resolutions)
{
var v = new Vector2Int(r.width, r.height);
if (v.x >= 1024 && !list.Contains(v)) list.Add(v);
}
var now = new Vector2Int(Screen.width, Screen.height);
if (!list.Contains(now)) list.Add(now);
list.Sort((a, b) => b.x != a.x ? b.x.CompareTo(a.x) : b.y.CompareTo(a.y));
return list;
}
static UniversalRenderPipelineAsset s_rp;
///
/// The top quality level always, tuned from the rows: render scale, shadows, MSAA, grass and bloom. Its pipeline
/// asset is copied once and the copy is what changes, so the project's asset is never touched.
///
public static void ApplyGraphics()
{
int top = QualitySettings.names.Length - 1;
if (s_rp == null)
{
if (top >= 0 && QualitySettings.GetQualityLevel() != top) QualitySettings.SetQualityLevel(top, true);
var src = (QualitySettings.renderPipeline ?? GraphicsSettings.defaultRenderPipeline) as UniversalRenderPipelineAsset;
if (src)
{
s_rp = Object.Instantiate(src);
s_rp.name = src.name + " (settings)";
QualitySettings.renderPipeline = s_rp;
}
}
int q = Quality;
QualitySettings.lodBias = q == 0 ? 1f : q == 1 ? 1.5f : 2f;
if (s_rp)
{
s_rp.renderScale = Mathf.Clamp(RenderScale, 0.5f, 1f);
s_rp.upscalingFilter = UpscalingFilterSelection.Auto;
int sh = Mathf.Clamp(Shadows, 0, 4);
s_rp.shadowDistance = new[] { 0f, 40f, 70f, 90f, 150f }[sh];
s_rp.shadowCascadeCount = new[] { 1, 1, 2, 4, 4 }[sh];
s_rp.mainLightShadowmapResolution = new[] { 1024, 1024, 2048, 2048, 4096 }[sh];
s_rp.msaaSampleCount = new[] { 1, 2, 4, 8 }[Mathf.Clamp(Antialias, 0, 3)];
}
GrassField.Detail = new[] { 0.5f, 0.75f, 1f }[Mathf.Clamp(GrassDetail, 0, 2)];
ApplyBloom();
}
/// Bloom on every post volume in the scene (their own copies of the profile, not the project's).
public static void ApplyBloom()
{
bool on = Bloom;
foreach (var v in Object.FindObjectsByType(FindObjectsSortMode.None))
if (v && v.profile && v.profile.TryGet(out UnityEngine.Rendering.Universal.Bloom b)) b.active = on;
}
}
}