summaryrefslogtreecommitdiff
path: root/src/save_data.gd
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <david@luevano.xyz>2022-06-04 23:00:58 -0600
committerDavid Luevano Alvarado <david@luevano.xyz>2022-06-04 23:00:58 -0600
commit36abc689d783774ce4f2d7b5a1bb621d8684be45 (patch)
treea3dbed6069fbd8d9a588c7510ce581af3e847af0 /src/save_data.gd
parent9a2bcf02c2623c8f3e8f5e74e70b3c0333790484 (diff)
added more ui for after gameplay, generalized basic food
Diffstat (limited to 'src/save_data.gd')
-rw-r--r--src/save_data.gd42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/save_data.gd b/src/save_data.gd
new file mode 100644
index 0000000..cbb3ed8
--- /dev/null
+++ b/src/save_data.gd
@@ -0,0 +1,42 @@
+extends Node
+
+const DATA_PATH: String = "user://data.save"
+
+var _stats: Stats
+
+
+func _ready() -> void:
+ _load_data()
+
+
+# called when setting "stats" and thus saving
+func save_data(stats: Stats) -> void:
+ _stats = stats
+ var file: File = File.new()
+ file.open(DATA_PATH, File.WRITE)
+ file.store_line(to_json(_stats.get_stats()))
+ file.close()
+
+
+func get_stats() -> Stats:
+ return _stats
+
+
+func _load_data() -> void:
+ # create an empty file if not present to avoid error while loading settings
+ _handle_new_file()
+
+ var file = File.new()
+ file.open(DATA_PATH, File.READ)
+ _stats = Stats.new()
+ _stats.set_stats(parse_json(file.get_line()))
+ file.close()
+
+
+func _handle_new_file() -> void:
+ var file: File = File.new()
+ if not file.file_exists(DATA_PATH):
+ file.open(DATA_PATH, File.WRITE)
+ _stats = Stats.new()
+ file.store_line(to_json(_stats.get_stats()))
+ file.close()