Move all C# scripts from flat structure to organized folders: - Core/ for game logic (Game, GameState, GameSettings) - UI/Menus/ and UI/Dialogs/ for user interface - Systems/ for reusable systems (Save, Localization) - Data/ for data models and configuration - Added framework for future: Gameplay/, Story/, Modding/ Update all .tscn scene files to reference new script paths. Fix timing issue in AdvancedSaveDialog focus handling.
148 lines
4.9 KiB
C#
148 lines
4.9 KiB
C#
using Godot;
|
|
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace TheGame
|
|
{
|
|
public class GameSettings
|
|
{
|
|
public bool Fullscreen { get; set; } = false;
|
|
public int MasterVolume { get; set; } = 100;
|
|
public int BackgroundVolume { get; set; } = 100;
|
|
public int EffectsVolume { get; set; } = 100;
|
|
public int RadioVolume { get; set; } = 100;
|
|
public string Language { get; set; } = "eng";
|
|
|
|
private static GameSettings _instance;
|
|
private static readonly string _settingsPath;
|
|
|
|
static GameSettings()
|
|
{
|
|
// Spara bredvid binären (executable directory)
|
|
string executableDir = OS.GetExecutablePath().GetBaseDir();
|
|
_settingsPath = Path.Combine(executableDir, "settings.json");
|
|
}
|
|
|
|
public static GameSettings Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = LoadSettings();
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
private static GameSettings LoadSettings()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(_settingsPath))
|
|
{
|
|
string jsonString = File.ReadAllText(_settingsPath);
|
|
var settings = JsonSerializer.Deserialize<GameSettings>(jsonString);
|
|
if (settings != null)
|
|
{
|
|
GD.Print($"Settings loaded from: {_settingsPath}");
|
|
return settings;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
GD.PrintErr($"Failed to load settings: {ex.Message}");
|
|
}
|
|
|
|
// Returnera standardinställningar om fil inte finns eller laddning misslyckas
|
|
GD.Print("Using default settings");
|
|
return new GameSettings();
|
|
}
|
|
|
|
public void SaveSettings()
|
|
{
|
|
try
|
|
{
|
|
string jsonString = JsonSerializer.Serialize(this, new JsonSerializerOptions
|
|
{
|
|
WriteIndented = true
|
|
});
|
|
File.WriteAllText(_settingsPath, jsonString);
|
|
GD.Print($"Settings saved to: {_settingsPath}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
GD.PrintErr($"Failed to save settings: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public void ApplyVideoSettings()
|
|
{
|
|
if (Fullscreen)
|
|
{
|
|
DisplayServer.WindowSetMode(DisplayServer.WindowMode.Fullscreen);
|
|
}
|
|
else
|
|
{
|
|
DisplayServer.WindowSetMode(DisplayServer.WindowMode.Windowed);
|
|
}
|
|
}
|
|
|
|
public void ApplyAudioSettings()
|
|
{
|
|
// Konvertera från 1-100 till dB (Godot använder dB för ljud)
|
|
float masterDb = ConvertVolumeToDb(MasterVolume);
|
|
float backgroundDb = ConvertVolumeToDb(BackgroundVolume);
|
|
float effectsDb = ConvertVolumeToDb(EffectsVolume);
|
|
float radioDb = ConvertVolumeToDb(RadioVolume);
|
|
|
|
// Sätt bus-volym (dessa måste skapas i Audio Bus Layout)
|
|
try
|
|
{
|
|
AudioServer.SetBusVolumeDb(AudioServer.GetBusIndex("Master"), masterDb);
|
|
|
|
int backgroundIndex = AudioServer.GetBusIndex("Background");
|
|
if (backgroundIndex != -1)
|
|
AudioServer.SetBusVolumeDb(backgroundIndex, backgroundDb);
|
|
|
|
int effectsIndex = AudioServer.GetBusIndex("Effects");
|
|
if (effectsIndex != -1)
|
|
AudioServer.SetBusVolumeDb(effectsIndex, effectsDb);
|
|
|
|
int radioIndex = AudioServer.GetBusIndex("Radio");
|
|
if (radioIndex != -1)
|
|
AudioServer.SetBusVolumeDb(radioIndex, radioDb);
|
|
|
|
GD.Print($"Applied audio settings: Master={MasterVolume}, Background={BackgroundVolume}, Effects={EffectsVolume}, Radio={RadioVolume}");
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
GD.PrintErr($"Failed to apply audio settings: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private float ConvertVolumeToDb(int volume)
|
|
{
|
|
if (volume <= 0)
|
|
return -80.0f; // Tyst
|
|
|
|
// Konvertera 1-100 till -60dB till 0dB
|
|
return -60.0f + (volume / 100.0f) * 60.0f;
|
|
}
|
|
|
|
public void ApplyLanguageSettings()
|
|
{
|
|
LocalizationManager.Instance.SetLanguage(Language);
|
|
}
|
|
|
|
public void ApplyAllSettings()
|
|
{
|
|
ApplyVideoSettings();
|
|
ApplyAudioSettings();
|
|
ApplyLanguageSettings();
|
|
SaveSettings();
|
|
}
|
|
}
|
|
} |