Save data

close #1
This commit is contained in:
math 2024-05-28 02:58:12 +02:00
parent 78cc721580
commit cb12fe5a2d
5 changed files with 81 additions and 46 deletions

View file

@ -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)
for element in month.days.get_children():
if element is Day:
all_days.append(element)
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)
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)

View file

@ -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])

View file

@ -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):

View file

@ -1,5 +0,0 @@
class_name Period
extends Node
var begin = {}
var end = {}

View file

@ -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])