78 lines
3.2 KiB
GDScript
78 lines
3.2 KiB
GDScript
extends Node
|
|
|
|
const settings_file_name := "user://settings.save"
|
|
|
|
var fullscreen := false : set = _set_fullscreen, get = _get_fullscreen
|
|
func _get_fullscreen() -> bool:
|
|
return fullscreen
|
|
func _set_fullscreen(new_fullscreen: bool) -> void:
|
|
fullscreen = new_fullscreen
|
|
var new_state := DisplayServer.WindowMode.WINDOW_MODE_FULLSCREEN if new_fullscreen else DisplayServer.WindowMode.WINDOW_MODE_WINDOWED
|
|
DisplayServer.window_set_mode(new_state)
|
|
save_settings()
|
|
|
|
var sound_on_gravity_change := true : set = _set_sound_on_gravity_change, get = _get_sound_on_gravity_change
|
|
func _get_sound_on_gravity_change() -> bool:
|
|
return sound_on_gravity_change
|
|
func _set_sound_on_gravity_change(new_sound_on_gravity_change: bool) -> void:
|
|
sound_on_gravity_change = new_sound_on_gravity_change
|
|
save_settings()
|
|
|
|
var volume_music := 0.0 : set = _set_volume_music, get = _get_volume_music
|
|
func _get_volume_music() -> float:
|
|
return volume_music
|
|
func _set_volume_music(new_volume_music: float) -> void:
|
|
volume_music = new_volume_music
|
|
for i in AudioServer.bus_count:
|
|
var bus_name := AudioServer.get_bus_name(i)
|
|
if bus_name != "Master" and bus_name != "Sounds":
|
|
AudioServer.set_bus_volume_db(i, volume_music)
|
|
AudioServer.set_bus_mute(i, volume_music <= -15.0)
|
|
save_settings()
|
|
|
|
var volume_sounds := 0.0 : set = _set_volume_sounds, get = _get_volume_sounds
|
|
func _get_volume_sounds() -> float:
|
|
return volume_sounds
|
|
func _set_volume_sounds(new_volume_sounds: float) -> void:
|
|
var bus_index := AudioServer.get_bus_index("Sounds")
|
|
if bus_index == -1:
|
|
print("The Sounds audio bus is missing, can't set new volume!")
|
|
return
|
|
|
|
volume_sounds = new_volume_sounds
|
|
AudioServer.set_bus_mute(bus_index, volume_sounds <= -15.0)
|
|
AudioServer.set_bus_volume_db(bus_index, volume_sounds)
|
|
save_settings()
|
|
|
|
func _ready() -> void:
|
|
if not FileAccess.file_exists(settings_file_name):
|
|
print("The settings file has not been found, not reading from it")
|
|
return
|
|
|
|
var settings_file := FileAccess.open(settings_file_name, FileAccess.READ)
|
|
var json := JSON.new()
|
|
var json_string := settings_file.get_line()
|
|
|
|
if json.parse(json_string) != OK:
|
|
print("(SETTINGS) JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
|
|
return
|
|
|
|
if json.data.has("fullscreen") and typeof(json.data["fullscreen"]) == TYPE_BOOL:
|
|
fullscreen = json.data["fullscreen"]
|
|
if json.data.has("sound_on_gravity_change") and typeof(json.data["sound_on_gravity_change"]) == TYPE_BOOL:
|
|
sound_on_gravity_change = json.data["sound_on_gravity_change"]
|
|
if json.data.has("volume_music") and typeof(json.data["volume_music"]) == TYPE_FLOAT:
|
|
volume_music = json.data["volume_music"]
|
|
if json.data.has("volume_sounds") and typeof(json.data["volume_sounds"]) == TYPE_FLOAT:
|
|
volume_sounds = json.data["volume_sounds"]
|
|
|
|
func save_settings() -> void:
|
|
var settings_file := FileAccess.open(settings_file_name, FileAccess.WRITE)
|
|
var json_string := JSON.stringify({
|
|
"fullscreen": fullscreen,
|
|
"sound_on_gravity_change": sound_on_gravity_change,
|
|
"volume_music": volume_music,
|
|
"volume_sounds": volume_sounds,
|
|
})
|
|
settings_file.store_line(json_string)
|
|
settings_file.store_line("FOR YOUR SAFETY, ALWAYS CHECK IF THE DATA OF THE FILES YOU DOWNLOAD LOOKS OKAY")
|