2025-12-12 15:35:07 +00:00
|
|
|
class_name SaveGameList extends CenterContainer
|
2024-10-01 23:32:59 +00:00
|
|
|
|
2024-10-06 09:40:42 +00:00
|
|
|
signal picked(save_game: SaveGame)
|
|
|
|
|
|
|
|
|
|
var has_stage: bool = false:
|
|
|
|
|
set(stage):
|
|
|
|
|
has_stage = stage
|
|
|
|
|
visible = stage
|
|
|
|
|
save_buttons[0].grab_focus()
|
2024-10-01 23:32:59 +00:00
|
|
|
|
2024-10-07 09:23:19 +00:00
|
|
|
@export var saves: Array[SaveGame]
|
2024-10-06 09:40:42 +00:00
|
|
|
var save_buttons: Array[SaveGameDisplay]
|
2024-10-01 23:32:59 +00:00
|
|
|
@export var update_display: bool:
|
|
|
|
|
set(value):
|
2024-10-07 09:23:19 +00:00
|
|
|
load_games()
|
|
|
|
|
var scroll_container: ScrollContainer
|
2024-10-01 23:32:59 +00:00
|
|
|
|
2025-03-23 14:43:04 +00:00
|
|
|
var override_save_slot: bool = false
|
2024-10-06 09:40:42 +00:00
|
|
|
|
2025-03-25 21:34:13 +00:00
|
|
|
func _validate_property(property: Dictionary) -> void:
|
|
|
|
|
if property.name == "saves":
|
|
|
|
|
property.usage = PROPERTY_USAGE_READ_ONLY + PROPERTY_USAGE_SCRIPT_VARIABLE + PROPERTY_USAGE_EDITOR
|
|
|
|
|
|
|
|
|
|
|
2024-10-01 23:32:59 +00:00
|
|
|
func _ready() -> void:
|
2025-12-12 15:33:06 +00:00
|
|
|
var dir := DirAccess.open(State.user_saves_path)
|
|
|
|
|
|
|
|
|
|
# Create dir if needed.
|
2024-10-16 10:24:38 +00:00
|
|
|
if DirAccess.get_open_error() == ERR_INVALID_PARAMETER:
|
|
|
|
|
dir = DirAccess.open("user://")
|
2025-12-12 15:33:06 +00:00
|
|
|
dir.make_dir_recursive(State.user_saves_path)
|
|
|
|
|
|
|
|
|
|
if DirAccess.get_open_error() != OK:
|
|
|
|
|
printerr("Error while opening User Save Directory: %s" % error_string(DirAccess.get_open_error()))
|
|
|
|
|
|
2024-10-07 09:23:19 +00:00
|
|
|
load_games()
|
2025-12-12 15:33:06 +00:00
|
|
|
|
2025-03-25 21:34:13 +00:00
|
|
|
for i in range(save_buttons.size()):
|
|
|
|
|
save_buttons[i].pressed.connect(func(): picked.emit(saves[i]))
|
2024-10-01 23:32:59 +00:00
|
|
|
|
2024-10-07 09:23:19 +00:00
|
|
|
func load_games():
|
2025-03-25 21:34:13 +00:00
|
|
|
saves = []
|
2024-10-07 09:23:19 +00:00
|
|
|
var save_game_dir := DirAccess.open(State.user_saves_path)
|
|
|
|
|
var filepaths: PackedStringArray = save_game_dir.get_files()
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2024-10-07 09:23:19 +00:00
|
|
|
for path in filepaths:
|
|
|
|
|
if path.ends_with(".json"):
|
2025-03-25 21:34:13 +00:00
|
|
|
saves.append(SaveGame.new("%s/%s" % [State.user_saves_path, path.get_basename()]))
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2024-10-07 09:23:19 +00:00
|
|
|
saves.append(SaveGame.new())
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2024-10-07 09:23:19 +00:00
|
|
|
#purging the current state
|
2024-10-06 09:40:42 +00:00
|
|
|
save_buttons = []
|
2024-10-07 09:23:19 +00:00
|
|
|
if scroll_container != null:
|
2025-03-25 21:34:13 +00:00
|
|
|
scroll_container.queue_free()
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2024-10-16 10:24:38 +00:00
|
|
|
scroll_container = ScrollContainer.new()
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2024-10-07 09:23:19 +00:00
|
|
|
scroll_container.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
|
2025-03-25 21:34:13 +00:00
|
|
|
scroll_container.custom_minimum_size = Vector2(0, ProjectSettings.get_setting("display/window/size/viewport_height") - 256)
|
2024-10-07 09:23:19 +00:00
|
|
|
add_child(scroll_container)
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2025-03-25 21:34:13 +00:00
|
|
|
var save_box := VBoxContainer.new()
|
|
|
|
|
save_box.add_theme_constant_override("separation", 16)
|
|
|
|
|
scroll_container.add_child(save_box)
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2024-10-06 09:40:42 +00:00
|
|
|
for i in range(saves.size()):
|
|
|
|
|
var new_button := SaveGameDisplay.new(saves[i], i+1)
|
2025-03-25 21:34:13 +00:00
|
|
|
save_box.add_child(new_button)
|
2024-10-06 09:40:42 +00:00
|
|
|
save_buttons.append(new_button)
|
|
|
|
|
new_button.pressed.connect(func(): _on_game_picked(i))
|
|
|
|
|
|
|
|
|
|
func _on_game_picked(id: int):
|
2025-03-23 14:43:04 +00:00
|
|
|
if override_save_slot:
|
2024-10-06 09:40:42 +00:00
|
|
|
if saves[id].current_room == 0:
|
|
|
|
|
picked.emit(id)
|
|
|
|
|
else:
|
2025-12-08 17:30:02 +00:00
|
|
|
pass
|
|
|
|
|
#picked.emit(id)
|
|
|
|
|
#$Popup.show() #FIXME: This popup is missing
|
2024-10-06 09:40:42 +00:00
|
|
|
else:
|
|
|
|
|
picked.emit(id)
|
|
|
|
|
|
2025-03-23 14:43:04 +00:00
|
|
|
# This function is called when the user us supposed to choose a slot to load or create a new game.
|
|
|
|
|
func pick_save_slot(create_new_game: bool):
|
2025-03-25 21:34:13 +00:00
|
|
|
if saves.size() == 1:
|
|
|
|
|
picked.emit(saves[0])
|
|
|
|
|
else:
|
|
|
|
|
State.take_stage(self)
|
|
|
|
|
self.override_save_slot = create_new_game
|
2024-10-07 09:23:19 +00:00
|
|
|
|
|
|
|
|
func get_most_recent_save() -> SaveGame:
|
|
|
|
|
var most_recent_time := 0.0
|
|
|
|
|
var most_recent_index:int = 0
|
|
|
|
|
for i in range(saves.size()):
|
2024-10-16 10:24:38 +00:00
|
|
|
if saves[i].last_saved > most_recent_time and not saves[i].current_room == 0:
|
2024-10-07 09:23:19 +00:00
|
|
|
most_recent_index = i
|
2024-10-16 10:24:38 +00:00
|
|
|
most_recent_time = saves[i].last_saved
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2024-10-16 10:24:38 +00:00
|
|
|
return saves[most_recent_index] if most_recent_time > 0 else SaveGame.new()
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2025-03-25 21:34:13 +00:00
|
|
|
func has_existing_saves() -> bool:
|
|
|
|
|
return saves.size() > 1
|
|
|
|
|
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
|
|
|
if event.is_action_pressed("ui_cancel"):
|
|
|
|
|
State.leave_stage(self)
|