diff --git a/script/calendar.gd b/script/calendar.gd index ff398be..6357be2 100644 --- a/script/calendar.gd +++ b/script/calendar.gd @@ -4,11 +4,9 @@ extends Control @export var month_scene: PackedScene @onready var year_label = $CenterContainer/VBoxContainer/YearLabel -@onready var year = $CenterContainer/VBoxContainer/Year -@onready var period_manager = $PeriodManager +@onready var months = $CenterContainer/VBoxContainer/Months var last_focused_day = null -var new_focused_day = null var all_days = [] @@ -24,20 +22,46 @@ func _ready(): month.month = i month.day_toggled.connect(_on_day_toggled) month.fill_days.connect(_on_fill_days) - year.add_child(month) + months.add_child(month) + + if FileAccess.file_exists("user://savegame.save"): + load_days() + + for month in months.get_children(): + if month is Month: + for element in month.days.get_children(): + if element is Day: + all_days.append(element) - for element in month.days.get_children(): - if element is Day: - all_days.append(element) + +func load_days(): + var save_game = FileAccess.open("user://savegame.save", FileAccess.READ) + while save_game.get_position() < save_game.get_length(): + var json_string = save_game.get_line() + var json = JSON.new() + + var parse_result = json.parse(json_string) + if not parse_result == OK: + print("JSON parse error: %s in %s at line %s" % [json.get_error_message(), json_string, json.get_error_line()]) + continue + + var node_data = json.get_data() + var month = get_month(node_data["month"]) + month.instantiate_day(node_data["day"], node_data["is_pressed"]) + + +func get_month(month): + for child in months.get_children(): + if child is Month and child.month == month: + return child func _on_day_toggled(date): - # period_manager.add(date) - # Update last focused day. var focused_node = get_viewport().gui_get_focus_owner() if focused_node is Day: last_focused_day = focused_node + save() func _on_fill_days(shift_clicked_day): @@ -64,4 +88,24 @@ func _on_fill_days(shift_clicked_day): if toggle: day.set_pressed_no_signal(true) if last_day == day: - return + break + + save() + + +func save(): + var save_game = FileAccess.open("user://savegame.save", FileAccess.WRITE) + var saved_nodes = get_tree().get_nodes_in_group("persist") + + for node in saved_nodes: + if node.scene_file_path.is_empty(): + print("persistant node '%s' is not an instanced scene, skipped" % node.name) + continue + + if !node.has_method("save"): + print("persistent node '%s' is missing a save() function, skipped" % node.name) + continue + + var node_data = node.call("save") + var json_string = JSON.stringify(node_data) + save_game.store_line(json_string) diff --git a/script/day.gd b/script/day.gd index cfe098b..a975c01 100644 --- a/script/day.gd +++ b/script/day.gd @@ -35,5 +35,14 @@ func is_before(other_day): return false +func save(): + return { + "year": date.year, + "month": date.month, + "day": date.day, + "is_pressed": is_pressed() + } + + func dump(): print("%s - %s - %s" % [date.year, date.month, date.day]) diff --git a/script/month.gd b/script/month.gd index fb24b2c..63748e7 100644 --- a/script/month.gd +++ b/script/month.gd @@ -11,7 +11,7 @@ var year: int var month: int @onready var month_label = $HBoxContainer/MonthLabel -@onready var days = $HBoxContainer/DaysGridContainer +@onready var days = $HBoxContainer/Days # Called when the node enters the scene tree for the first time. @@ -26,13 +26,24 @@ func _ready(): for i in range(first_day.weekday): var empty_label = Label.new() days.add_child(empty_label) + + # Instanciate days in no save game + if FileAccess.file_exists("user://savegame.save"): + return + for i in range(1, days_in_month + 1): - var day = day_scene.instantiate() - day.set_date(year, month, i) - day.text = "%s" % [i] - day.day_toggled.connect(_on_day_toggled) - day.fill_days.connect(_on_fill_days) - days.add_child(day) + instantiate_day(i, false) + + +func instantiate_day(day, is_pressed): + var new_day = day_scene.instantiate() + days.add_child(new_day) + new_day.set_date(year, month, day) + new_day.text = "%s" % [day] + new_day.day_toggled.connect(_on_day_toggled) + new_day.fill_days.connect(_on_fill_days) + new_day.set_pressed_no_signal(is_pressed) + new_day.add_to_group("persist") func _on_day_toggled(date): diff --git a/script/period.gd b/script/period.gd deleted file mode 100644 index 528a39f..0000000 --- a/script/period.gd +++ /dev/null @@ -1,5 +0,0 @@ -class_name Period -extends Node - -var begin = {} -var end = {} diff --git a/script/periodmanager.gd b/script/periodmanager.gd deleted file mode 100644 index 331153b..0000000 --- a/script/periodmanager.gd +++ /dev/null @@ -1,24 +0,0 @@ -extends Node - -var periods = [] -var last_period = null - - -func add(date): - if last_period != null: - last_period.end = date - last_period = null - dump() - return - - # If no new period has begun - var period = Period.new() - period.begin = date - periods.append(period) - last_period = period - dump() - - -func dump(): - for period in periods: - print("%s - %s" % [period.begin, period.end])