Browse Source

Creating GameSettingsManager autoload and migrated Graphics settings

pull/11/head
Ryan Reed 1 month ago
parent
commit
f239bc1b16
12 changed files with 132 additions and 50 deletions
  1. +82
    -0
      autoloads/game_settings_manager.gd
  2. +1
    -0
      autoloads/game_settings_manager.gd.uid
  3. +1
    -43
      autoloads/globals.gd
  4. +1
    -0
      project.godot
  5. +8
    -0
      resources/game_settings/game_settings_resource.gd
  6. +1
    -0
      resources/game_settings/game_settings_resource.gd.uid
  7. +9
    -0
      resources/game_settings/game_settings_resource.tres
  8. +9
    -0
      resources/game_settings/graphics_settings.gd
  9. +1
    -0
      resources/game_settings/graphics_settings.gd.uid
  10. +11
    -0
      resources/game_settings/graphics_settings.tres
  11. +1
    -1
      scenes/player/player.gd
  12. +7
    -6
      scenes/ui/menus/settings_menu.gd

+ 82
- 0
autoloads/game_settings_manager.gd View File

@ -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

+ 1
- 0
autoloads/game_settings_manager.gd.uid View File

@ -0,0 +1 @@
uid://dxe2y12f412lp

+ 1
- 43
autoloads/globals.gd View File

@ -1,51 +1,9 @@
extends Node 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 BLOCK_PREFAB: PackedScene = preload("res://scenes/blocks/block.tscn")
const DROPPED_BLOCK_PREFAB: PackedScene = preload("res://scenes/blocks/dropped_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_waila: bool = true ## Enable `What Am I Looking At` UI
var enable_block_highlight: bool = true 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()

+ 1
- 0
project.godot View File

@ -24,6 +24,7 @@ InventoryManager="*res://autoloads/inventory_manager.gd"
DBItems="*res://autoloads/db_items.gd" DBItems="*res://autoloads/db_items.gd"
SaveGameManager="*res://save_load/autoloads/save_game_manager.gd" SaveGameManager="*res://save_load/autoloads/save_game_manager.gd"
SignalManager="*res://autoloads/signal_manager.gd" SignalManager="*res://autoloads/signal_manager.gd"
GameSettingsManager="*res://autoloads/game_settings_manager.gd"
[debug] [debug]


+ 8
- 0
resources/game_settings/game_settings_resource.gd View File

@ -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()

+ 1
- 0
resources/game_settings/game_settings_resource.gd.uid View File

@ -0,0 +1 @@
uid://cffw77d120p56

+ 9
- 0
resources/game_settings/game_settings_resource.tres View File

@ -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"

+ 9
- 0
resources/game_settings/graphics_settings.gd View File

@ -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

+ 1
- 0
resources/game_settings/graphics_settings.gd.uid View File

@ -0,0 +1 @@
uid://mfghfem8im6o

+ 11
- 0
resources/game_settings/graphics_settings.tres View File

@ -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"

+ 1
- 1
scenes/player/player.gd View File

@ -49,7 +49,7 @@ func _physics_process(delta: float) -> void:
move_and_slide() move_and_slide()
func _ready() -> void: 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: func _apply_gravity(delta: float) -> void:


+ 7
- 6
scenes/ui/menus/settings_menu.gd View File

@ -21,15 +21,16 @@ func _on_enable_waila_toggled(toggled_on: bool) -> void:
#region Graphics Settings #region Graphics Settings
func _on_fov_slider_changed(value: float) -> void: func _on_fov_slider_changed(value: float) -> void:
fov_value_label.text = str(int(value)) 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: func _on_graphics_apply_button_pressed() -> void:
var values: Array = resolution_input.text.split_floats("x") 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 #endregion
#region Audio Settings #region Audio Settings


Loading…
Cancel
Save