Skip to content

Commit

Permalink
Options files & fullscreen shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
Yolwoocle committed Sep 24, 2024
1 parent 2606e52 commit 9adee54
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ removeme_winlevel={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":true,"ctrl_pressed":true,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":90,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
toggle_fullscreen={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194342,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":true,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194309,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}

[layer_names]

Expand Down
36 changes: 33 additions & 3 deletions src/scripts/autoload/game_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,44 @@ var _discord_rpc_update_timer = 0.0
const STEAM_APP_ID = 3219110
var steam_interface: Node = null

var options_manager: OptionsManager
var achievement_manager: AchievementManager

var cursor = preload("res://assets/images/ui/cursor_big.png")
var cursor_click = preload("res://assets/images/ui/cursor_click_big.png")
var global_camera_scene: PackedScene = preload("res://scenes/camera/global_camera.tscn")

var camera: Camera2D
var is_fullscreen := false
var _loaded_fullscreen_option = false
var is_fullscreen: bool = true:
set(value):
is_fullscreen = value
update_fullscreen()
save_option("graphics", "is_fullscreen", is_fullscreen)

func _ready():
print("launched game")
options_manager = OptionsManager.new()

camera = global_camera_scene.instantiate()
add_child(camera)

process_mode = ProcessMode.PROCESS_MODE_ALWAYS

Input.set_custom_mouse_cursor(cursor)
#Input.set_custom_mouse_cursor(cursor_click, Input.CURSOR_IBEAM)
Input.set_custom_mouse_cursor(cursor)

_init_discord_rpc()
_init_steam()
_init_achievement_manager()


func _process(delta):
# It is necessary to wait one frame after the scene has been instanciated to avoid an ugly gray frame
if not _loaded_fullscreen_option:
is_fullscreen = load_option("graphics", "is_fullscreen", true)
_loaded_fullscreen_option = true

if discord_rich_presence:
_discord_rpc_update_timer -= delta
if _discord_rpc_update_timer < 0:
Expand All @@ -93,6 +107,9 @@ func _input(event):
if scene is Level:
scene.undo_action()

if event.is_action_pressed("toggle_fullscreen"):
toggle_fullscreen()

if event.is_action_pressed("removeme_achievement_test"):
print("about to grant ACH_TEST_01")
achievement_manager.grant("ACH_TEST_01")
Expand Down Expand Up @@ -137,11 +154,24 @@ func before_scene_change():

func toggle_fullscreen():
is_fullscreen = not is_fullscreen

func update_fullscreen():
if is_fullscreen:
DisplayServer.window_set_mode(DisplayServer.WindowMode.WINDOW_MODE_FULLSCREEN)
else:
DisplayServer.window_set_mode(DisplayServer.WindowMode.WINDOW_MODE_MAXIMIZED)


## Saves an option and returns whether it was saved successfully.
## NOTE: this could become quite slow if the options file are updated very frequently or is very big
func save_option(section: String, key: String, value: Variant) -> bool:
return options_manager.save(section, key, value)

## Return an option or a default value if impossible to load the options file.
## NOTE: this could become quite slow if the options file are read very frequently or is very big
func load_option(section: String, key: String, default_value: Variant):
return options_manager.load(section, key, default_value)

#################################################################

func is_discord_rpc_supported() -> bool:
Expand Down Expand Up @@ -199,4 +229,4 @@ func _init_achievement_manager():
print("Creating generic achievement manager")
pass

add_child(achievement_manager)
add_child(achievement_manager)
9 changes: 4 additions & 5 deletions src/scripts/autoload/level_data.gd
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ func new_load_level_data() -> void:
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
new_load_level_data()
var volume_config = ConfigFile.new()
volume_config.load("user://volume.cfg")
var master_value = volume_config.get_value("Sound", "Master", 1)
var music_value = volume_config.get_value("Sound", "Music", 1)

var master_value = GameManager.load_option("volume", "Master", 1)
var music_value = GameManager.load_option("volume", "Music", 1)
AudioServer.set_bus_volume_db(AudioServer.get_bus_index("Master"), linear_to_db(master_value))
AudioServer.set_bus_volume_db(AudioServer.get_bus_index("Music"), linear_to_db(music_value))
pass # Replace with function body.
Expand All @@ -128,4 +127,4 @@ func _input(event):
reload_scene()

func _process(delta: float) -> void:
pass
pass
22 changes: 22 additions & 0 deletions src/scripts/options/options_manager.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extends Node
class_name OptionsManager

const OPTIONS_FILE_PATH = "user://options.cfg"

var config = ConfigFile.new()

func _init():
var err = config.load(OPTIONS_FILE_PATH)
if err != OK:
print("Error loading options file. Defaults will be used. Error code: ", err)
else:
print("Loaded options file successfully.")


func save(section: String, key: String, value: Variant) -> bool:
config.set_value(section, key, value)
return config.save(OPTIONS_FILE_PATH) == OK


func load(section: String, key: String, default_value: Variant):
return config.get_value(section, key, default_value)
29 changes: 17 additions & 12 deletions src/scripts/ui/volume_slider.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ func _ready() -> void:
pass

func _on_value_changed(input_value: float) -> void:
var config = ConfigFile.new()
config.load("user://volume.cfg")
GameManager.save_option("volume", audio_bus_name, value)
AudioServer.set_bus_volume_db(_bus, linear_to_db(input_value))


var music_value = config.get_value("Sound", "Music", 1)
var master_value = config.get_value("Sound", "Master", 1)
config.set_value("Sound", "Music", music_value)
config.set_value("Sound", "Master", master_value)
if audio_bus_name == "Master":
config.set_value("Sound", "Master", input_value)
else:
config.set_value("Sound", "Music", input_value)
config.save("user://volume.cfg")
# func _old_on_value_changed(input_value: float) -> void:
# var config = ConfigFile.new()
# config.load("user://volume.cfg")

# var music_value = config.get_value("Sound", "Music", 1)
# var master_value = config.get_value("Sound", "Master", 1)
# config.set_value("Sound", "Music", music_value)
# config.set_value("Sound", "Master", master_value)
# if audio_bus_name == "Master":
# config.set_value("Sound", "Master", input_value)
# else:
# config.set_value("Sound", "Music", input_value)
# config.save("user://volume.cfg")

AudioServer.set_bus_volume_db(_bus, linear_to_db(input_value))
# AudioServer.set_bus_volume_db(_bus, linear_to_db(input_value))

0 comments on commit 9adee54

Please sign in to comment.