diff --git a/src/HighScores.gd b/src/HighScores.gd index 616649f..44b7b64 100644 --- a/src/HighScores.gd +++ b/src/HighScores.gd @@ -1,5 +1,6 @@ extends Node +const HIGHSCORE_FILENAME = "user://highscores.json" var scores = [{"name": "Nobody", "score": 0}] @@ -8,6 +9,9 @@ class ScoreSorter: return a["score"] > b["score"] +func _ready() -> void: + load_highscores() + func sort_scores() -> void: scores.sort_custom(ScoreSorter, "sort_descending") @@ -15,3 +19,38 @@ func add_score(score) -> void: scores.push_back(score) sort_scores() print(scores) + save_highscores() + + +func load_highscores() -> void: + prints(self, "Loading highscores from filesystem...") + + # Try to load the highscore file + var highscore_file := File.new() + if not highscore_file.file_exists(HIGHSCORE_FILENAME): + prints(self, "Highscore file does not exist yet.") + return + + var err = highscore_file.open(HIGHSCORE_FILENAME, File.READ) + if err != OK: + printerr("Error opening highscore file to read: ", err) + return + + # Parse highscore file as JSON + var highscore_json = highscore_file.get_as_text() + scores = parse_json(highscore_json) + +func save_highscores() -> void: + prints(self, "Saving highscores to filesystem...") + + # Try to save the highscore file + var highscore_file := File.new() + + var err = highscore_file.open(HIGHSCORE_FILENAME, File.WRITE) + if err != OK: + printerr("Error opening highscore file to write: ", err) + return + + # Dump highscore to JSON + var highscore_json = to_json(scores) + highscore_file.store_line(highscore_json) diff --git a/src/Settings.gd b/src/Settings.gd index cb2d026..c40581e 100644 --- a/src/Settings.gd +++ b/src/Settings.gd @@ -1,13 +1,66 @@ extends Node -# General settings +# Constants +const CONFIG_FILENAME = "user://settings.json" + +# General settings (persistent) var sfx_enabled := true var music_enabled := true -# Game mode +# Game mode (not persistent) enum GameMode { MODE_SOLO = 0, MODE_MULTI_CHAOS = 1, MODE_MULTI_HOTSEAT = 2, } var game_mode: int = GameMode.MODE_SOLO + + +func _ready() -> void: + # Load settings from filesystem + load_settings() + + +func from_dict(data: Dictionary) -> void: + sfx_enabled = data.get("sfx_enabled", true) + music_enabled = data.get("music_enabled", true) + + +func to_dict() -> Dictionary: + return { + "sfx_enabled": sfx_enabled, + "music_enabled": music_enabled, + } + + +func load_settings() -> void: + # Try to load the config file + var config_file := File.new() + if not config_file.file_exists(CONFIG_FILENAME): + prints(self, "Config file does not exist yet.") + return + + var err = config_file.open(CONFIG_FILENAME, File.READ) + if err != OK: + printerr("Error opening config file to read: ", err) + return + + # Parse config file as JSON + var config_json = config_file.get_as_text() + from_dict(parse_json(config_json)) + + +func save_settings() -> void: + prints(self, "Saving settings to filesystem...") + + # Try to save the config file + var config_file := File.new() + + var err = config_file.open(CONFIG_FILENAME, File.WRITE) + if err != OK: + printerr("Error opening config file to write: ", err) + return + + # Dump config to JSON + var config_json = to_json(to_dict()) + config_file.store_line(config_json) diff --git a/src/screens/title_screen/TitleScreen.gd b/src/screens/title_screen/TitleScreen.gd index 784a0bd..415c73f 100644 --- a/src/screens/title_screen/TitleScreen.gd +++ b/src/screens/title_screen/TitleScreen.gd @@ -26,6 +26,9 @@ func start_game() -> void: if loading_game: return + # Persist settings + Settings.save_settings() + print("Start game: Switch to MainGame scene") loading_game = true