102 lines
4.7 KiB
GDScript
102 lines
4.7 KiB
GDScript
class_name GameSettings
|
|
extends RefCounted
|
|
## The player's settings, kept in user://settings.cfg (ConfigFile) apart from the save game, and
|
|
## applied at start and on every change (SettingsMenu):
|
|
## * Audio: the volume of each audio bus (game/resources/audio_buses.tres).
|
|
## * Controls: the dead zones of the move stick (the move_* actions) and the aim stick
|
|
## (GameConfig.aim_dead_zone). Mouse aim follows the cursor directly, so it has no
|
|
## sensitivity to set.
|
|
## * Display: fullscreen, VSync, and the 3D resolution scale (the HUD stays sharp).
|
|
## * Gameplay: whether the tips show on the first day and night (Tutorial).
|
|
## * Co-op: your player name, and whether your microphone is sent in voice chat.
|
|
## Values missing from the file or out of range fall back to the defaults, so an old or
|
|
## hand-edited file never breaks the game.
|
|
|
|
const PATH := "user://settings.cfg"
|
|
const BUSES: Array[String] = ["Master", "Music", "SFX", "Ambience", "Voice"]
|
|
const BUS_NAMES := {"Master": "Master volume", "Music": "Music and stingers", "SFX": "Sound effects", "Ambience": "Ambience",
|
|
"Voice": "Voice chat (your friend)"}
|
|
const DEFAULT_VOLUME := {"Master": 0.8, "Music": 0.7, "SFX": 0.8, "Ambience": 0.6, "Voice": 1.0}
|
|
|
|
var volume := DEFAULT_VOLUME.duplicate()
|
|
var move_dead_zone := 0.2
|
|
var aim_dead_zone := 0.3
|
|
var fullscreen := false
|
|
var vsync := true
|
|
var resolution_scale := 1.0
|
|
var show_tips := true
|
|
## Co-op: your name for the lobby and the name tag, and whether your microphone is sent.
|
|
var player_name := ""
|
|
var microphone := true
|
|
|
|
|
|
static func load_from(path: String = PATH) -> GameSettings:
|
|
var g := GameSettings.new()
|
|
var f := ConfigFile.new()
|
|
if f.load(path) != OK:
|
|
return g
|
|
for bus in BUSES:
|
|
g.volume[bus] = _number(f.get_value("audio", bus.to_lower(), g.volume[bus]), 0.0, 1.0, g.volume[bus])
|
|
g.move_dead_zone = _number(f.get_value("controls", "move_dead_zone", g.move_dead_zone), 0.05, 0.9, g.move_dead_zone)
|
|
g.aim_dead_zone = _number(f.get_value("controls", "aim_dead_zone", g.aim_dead_zone), 0.05, 0.9, g.aim_dead_zone)
|
|
g.fullscreen = _flag(f.get_value("display", "fullscreen", g.fullscreen), g.fullscreen)
|
|
g.vsync = _flag(f.get_value("display", "vsync", g.vsync), g.vsync)
|
|
g.resolution_scale = _number(f.get_value("display", "resolution_scale", g.resolution_scale), 0.5, 1.0, g.resolution_scale)
|
|
g.show_tips = _flag(f.get_value("gameplay", "show_tips", g.show_tips), g.show_tips)
|
|
var n: Variant = f.get_value("coop", "player_name", "")
|
|
g.player_name = String(n).strip_edges().left(20) if n is String else ""
|
|
g.microphone = _flag(f.get_value("coop", "microphone", g.microphone), g.microphone)
|
|
return g
|
|
|
|
|
|
func save_to(path: String = PATH) -> Error:
|
|
var f := ConfigFile.new()
|
|
for bus in BUSES:
|
|
f.set_value("audio", bus.to_lower(), volume.get(bus, DEFAULT_VOLUME[bus]))
|
|
f.set_value("controls", "move_dead_zone", move_dead_zone)
|
|
f.set_value("controls", "aim_dead_zone", aim_dead_zone)
|
|
f.set_value("display", "fullscreen", fullscreen)
|
|
f.set_value("display", "vsync", vsync)
|
|
f.set_value("display", "resolution_scale", resolution_scale)
|
|
f.set_value("gameplay", "show_tips", show_tips)
|
|
f.set_value("coop", "player_name", player_name)
|
|
f.set_value("coop", "microphone", microphone)
|
|
return f.save(path)
|
|
|
|
|
|
## Applies everything: bus volumes, dead zones (into config too, when given), and the display
|
|
## (skipped without a real display, e.g. headless).
|
|
func apply(viewport: Viewport, config: GameConfig = null) -> void:
|
|
for bus in BUSES:
|
|
var i := AudioServer.get_bus_index(bus)
|
|
if i < 0:
|
|
continue
|
|
var v: float = volume.get(bus, DEFAULT_VOLUME[bus])
|
|
AudioServer.set_bus_volume_db(i, linear_to_db(maxf(v, 0.0001)))
|
|
AudioServer.set_bus_mute(i, v <= 0.001)
|
|
for action in ["move_left", "move_right", "move_forward", "move_back"]:
|
|
if InputMap.has_action(action):
|
|
InputMap.action_set_deadzone(action, move_dead_zone)
|
|
if config != null:
|
|
config.aim_dead_zone = aim_dead_zone
|
|
if viewport != null:
|
|
viewport.scaling_3d_scale = resolution_scale
|
|
if DisplayServer.get_name() == "headless":
|
|
return
|
|
var mode := DisplayServer.window_get_mode()
|
|
var want_full := fullscreen
|
|
var is_full := mode == DisplayServer.WINDOW_MODE_FULLSCREEN or mode == DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN
|
|
if want_full != is_full:
|
|
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN if want_full else DisplayServer.WINDOW_MODE_WINDOWED)
|
|
DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED if vsync else DisplayServer.VSYNC_DISABLED)
|
|
|
|
|
|
static func _number(v: Variant, lo: float, hi: float, fallback: float) -> float:
|
|
if not (v is float or v is int):
|
|
return fallback
|
|
var x := float(v)
|
|
return clampf(x, lo, hi) if is_finite(x) else fallback
|
|
|
|
|
|
static func _flag(v: Variant, fallback: bool) -> bool:
|
|
return bool(v) if v is bool else fallback
|