From f239bc1b164606b14af9418ef5833398e1c910a0 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Sun, 23 Mar 2025 13:54:20 -0400 Subject: [PATCH] Creating GameSettingsManager autoload and migrated Graphics settings --- autoloads/game_settings_manager.gd | 82 +++++++++++++++++++ autoloads/game_settings_manager.gd.uid | 1 + autoloads/globals.gd | 44 +--------- project.godot | 1 + .../game_settings/game_settings_resource.gd | 8 ++ .../game_settings_resource.gd.uid | 1 + .../game_settings/game_settings_resource.tres | 9 ++ resources/game_settings/graphics_settings.gd | 9 ++ .../game_settings/graphics_settings.gd.uid | 1 + .../game_settings/graphics_settings.tres | 11 +++ scenes/player/player.gd | 2 +- scenes/ui/menus/settings_menu.gd | 13 +-- 12 files changed, 132 insertions(+), 50 deletions(-) create mode 100644 autoloads/game_settings_manager.gd create mode 100644 autoloads/game_settings_manager.gd.uid create mode 100644 resources/game_settings/game_settings_resource.gd create mode 100644 resources/game_settings/game_settings_resource.gd.uid create mode 100644 resources/game_settings/game_settings_resource.tres create mode 100644 resources/game_settings/graphics_settings.gd create mode 100644 resources/game_settings/graphics_settings.gd.uid create mode 100644 resources/game_settings/graphics_settings.tres diff --git a/autoloads/game_settings_manager.gd b/autoloads/game_settings_manager.gd new file mode 100644 index 0000000..e3b72e8 --- /dev/null +++ b/autoloads/game_settings_manager.gd @@ -0,0 +1,82 @@ +extends Node + + +#region Graphics Settings Signals +signal apply_graphics_settings +signal graphics_resolution_changed(resolution: Vector2i) +signal graphics_fullscreen_changed(fullscreen_enabled: bool) +signal graphics_vsync_changed(vsync_enabled: bool) +signal graphics_fov_changed(fov: int) +#endregion + +## See documentation to where this path is: https://docs.godotengine.org/en/stable/tutorials/io/data_paths.html#accessing-persistent-user-data-user[br][br] +## Default Paths:[br] +## * Windows: %APPDATA%\Godot\app_userdata\[project_name][br] +## * macOS: ~/Library/Application Support/Godot/app_userdata/[project_name][br] +## * Linux: ~/.local/share/godot/app_userdata/[project_name][br] +var save_data_path: String = "user://game_data/" +var settings_file_name: String = "game_settings.tres" + +var settings_file_path: String = save_data_path + settings_file_name +var settings: GameSettingsResource = GameSettingsResource.new() + + +func _init() -> void: + graphics_fov_changed.connect(_on_graphics_fov_changed) + graphics_fullscreen_changed.connect(_on_graphics_fullscreen_changed) + graphics_resolution_changed.connect(_on_graphics_resolution_changed) + graphics_vsync_changed.connect(_on_graphics_vsync_changed) + apply_graphics_settings.connect(_on_apply_graphics_settings) + + +#region Graphics Settings +func _on_apply_graphics_settings() -> void: + if settings.graphics.fullscreen: + DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN) + else: + DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED) + + get_window().size = settings.graphics.resolution + + if settings.graphics.vsync: + DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED) + else: + DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED) + + save_settings() + +func _on_graphics_fov_changed(fov: int) -> void: + settings.graphics.fov = fov + +func _on_graphics_fullscreen_changed(fullscreen_enabled: bool) -> void: + settings.graphics.fullscreen = fullscreen_enabled + +func _on_graphics_resolution_changed(resolution: Vector2i) -> void: + settings.graphics.resolution = resolution + +func _on_graphics_vsync_changed(vsync_enabled: bool) -> void: + settings.graphics.vsync = vsync_enabled +#endregion + +#region Saving and Loading Settings +func load_settings(apply_after_load: bool = true) -> void: + if !FileAccess.file_exists(settings_file_path): + printerr("Failed to load game settings. File does not exist: ", settings_file_path) + return + + settings = ResourceLoader.load(settings_file_path) + if settings == null: + printerr("Failed to load game settings. Unknown format? ", settings_file_path) + return + + if apply_after_load: + apply_graphics_settings.emit() + +func save_settings() -> void: + if !DirAccess.dir_exists_absolute(save_data_path): + DirAccess.make_dir_absolute(save_data_path) + + var result: int = ResourceSaver.save(settings, settings_file_path) + if result != OK: + printerr("Failed to save game settings: ", result) +#endregion diff --git a/autoloads/game_settings_manager.gd.uid b/autoloads/game_settings_manager.gd.uid new file mode 100644 index 0000000..bc62d5b --- /dev/null +++ b/autoloads/game_settings_manager.gd.uid @@ -0,0 +1 @@ +uid://dxe2y12f412lp diff --git a/autoloads/globals.gd b/autoloads/globals.gd index 8999c9c..32f5524 100644 --- a/autoloads/globals.gd +++ b/autoloads/globals.gd @@ -1,51 +1,9 @@ extends Node -signal graphics_settings_changed(resolution: Vector2, fullscreen: bool, vsync: bool) -signal graphics_fov_changed(fov: int) - - const BLOCK_PREFAB: PackedScene = preload("res://scenes/blocks/block.tscn") const DROPPED_BLOCK_PREFAB: PackedScene = preload("res://scenes/blocks/dropped_block.tscn") - +# TODO: Move the following into the GameSettingsManager var enable_waila: bool = true ## Enable `What Am I Looking At` UI var enable_block_highlight: bool = true - -# TODO: Replace the following with a Resource -var graphics_settings: Dictionary[String, Variant] = { - "resolution": Vector2i.ZERO, - "fov": 75, - "fullscreen": false, - "vsync": false, -} - - -func _init() -> void: - graphics_settings_changed.connect(_on_graphics_settings_changed) - graphics_fov_changed.connect(_on_graphics_fov_changed) - - -func apply_graphics_settings() -> void: - if graphics_settings.fullscreen: - DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN) - else: - DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED) - - get_window().size = graphics_settings.resolution - - if graphics_settings.vsync: - DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED) - else: - DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED) - - -func _on_graphics_fov_changed(fov: int) -> void: - graphics_settings.fov = fov - -func _on_graphics_settings_changed(resolution: Vector2i, fullscreen: bool, vsync: bool) -> void: - graphics_settings.resolution = resolution - graphics_settings.fullscreen = fullscreen - graphics_settings.vsync = vsync - - apply_graphics_settings() diff --git a/project.godot b/project.godot index 0f0c81e..a8bb277 100644 --- a/project.godot +++ b/project.godot @@ -24,6 +24,7 @@ InventoryManager="*res://autoloads/inventory_manager.gd" DBItems="*res://autoloads/db_items.gd" SaveGameManager="*res://save_load/autoloads/save_game_manager.gd" SignalManager="*res://autoloads/signal_manager.gd" +GameSettingsManager="*res://autoloads/game_settings_manager.gd" [debug] diff --git a/resources/game_settings/game_settings_resource.gd b/resources/game_settings/game_settings_resource.gd new file mode 100644 index 0000000..5befa64 --- /dev/null +++ b/resources/game_settings/game_settings_resource.gd @@ -0,0 +1,8 @@ +## The resource for saving game settings. +## This should be not be saved with saves (for game syncing reasons) +class_name GameSettingsResource +extends Resource + + +@export var game_version: String = ProjectSettings.get_setting("application/config/version") +@export var graphics: GraphicsSettingsResource = GraphicsSettingsResource.new() diff --git a/resources/game_settings/game_settings_resource.gd.uid b/resources/game_settings/game_settings_resource.gd.uid new file mode 100644 index 0000000..1a87000 --- /dev/null +++ b/resources/game_settings/game_settings_resource.gd.uid @@ -0,0 +1 @@ +uid://cffw77d120p56 diff --git a/resources/game_settings/game_settings_resource.tres b/resources/game_settings/game_settings_resource.tres new file mode 100644 index 0000000..a100b47 --- /dev/null +++ b/resources/game_settings/game_settings_resource.tres @@ -0,0 +1,9 @@ +[gd_resource type="Resource" script_class="GameSettingsResource" load_steps=2 format=3 uid="uid://b3kkeyyos7a7"] + +[ext_resource type="Script" uid="uid://cffw77d120p56" path="res://resources/game_settings/game_settings_resource.gd" id="1_fe7s4"] + +[resource] +script = ExtResource("1_fe7s4") +game_version = null +graphics = [] +metadata/_custom_type_script = "uid://cffw77d120p56" diff --git a/resources/game_settings/graphics_settings.gd b/resources/game_settings/graphics_settings.gd new file mode 100644 index 0000000..6a89209 --- /dev/null +++ b/resources/game_settings/graphics_settings.gd @@ -0,0 +1,9 @@ +## The resource for saving game settings. +## This should be not be saved with saves (for game syncing reasons) +class_name GraphicsSettingsResource +extends Resource + +@export var resolution: Vector2i = Vector2i(1280, 720) +@export var fullscreen: bool = false +@export var vsync: bool = false +@export var fov: int = 75 diff --git a/resources/game_settings/graphics_settings.gd.uid b/resources/game_settings/graphics_settings.gd.uid new file mode 100644 index 0000000..0e96168 --- /dev/null +++ b/resources/game_settings/graphics_settings.gd.uid @@ -0,0 +1 @@ +uid://mfghfem8im6o diff --git a/resources/game_settings/graphics_settings.tres b/resources/game_settings/graphics_settings.tres new file mode 100644 index 0000000..5898ec0 --- /dev/null +++ b/resources/game_settings/graphics_settings.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" script_class="GraphicsSettingsResource" load_steps=2 format=3 uid="uid://b07gyfa776057"] + +[ext_resource type="Script" uid="uid://mfghfem8im6o" path="res://resources/game_settings/graphics_settings.gd" id="1_3m2to"] + +[resource] +script = ExtResource("1_3m2to") +resolution = Vector2i(1280, 720) +fullscreen = false +vsync = false +fov = 75 +metadata/_custom_type_script = "uid://mfghfem8im6o" diff --git a/scenes/player/player.gd b/scenes/player/player.gd index e0e1387..bd9e58a 100644 --- a/scenes/player/player.gd +++ b/scenes/player/player.gd @@ -49,7 +49,7 @@ func _physics_process(delta: float) -> void: move_and_slide() func _ready() -> void: - Globals.graphics_fov_changed.connect(_on_graphics_fov_changed) + GameSettingsManager.graphics_fov_changed.connect(_on_graphics_fov_changed) func _apply_gravity(delta: float) -> void: diff --git a/scenes/ui/menus/settings_menu.gd b/scenes/ui/menus/settings_menu.gd index bb37a85..97b3e6e 100644 --- a/scenes/ui/menus/settings_menu.gd +++ b/scenes/ui/menus/settings_menu.gd @@ -21,15 +21,16 @@ func _on_enable_waila_toggled(toggled_on: bool) -> void: #region Graphics Settings func _on_fov_slider_changed(value: float) -> void: fov_value_label.text = str(int(value)) - Globals.graphics_fov_changed.emit(int(fov_slider.value)) + GameSettingsManager.graphics_fov_changed.emit(int(fov_slider.value)) func _on_graphics_apply_button_pressed() -> void: var values: Array = resolution_input.text.split_floats("x") - Globals.graphics_settings_changed.emit( - Vector2i(values[0], values[1]), - fullscreen_input.button_pressed, - vsync_input.button_pressed - ) + + GameSettingsManager.graphics_resolution_changed.emit(Vector2i(values[0], values[1])) + GameSettingsManager.graphics_fullscreen_changed.emit(fullscreen_input.button_pressed) + GameSettingsManager.graphics_vsync_changed.emit(vsync_input.button_pressed) + GameSettingsManager.apply_graphics_settings.emit() + #endregion #region Audio Settings