2024-10-01 23:26:54 +00:00
|
|
|
class_name SaveGame extends Resource
|
|
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
## Save game data container
|
|
|
|
|
## This is primarily a data class - file I/O helpers are thin wrappers around ResourceSaver/Loader
|
2026-01-16 12:03:39 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
# === Computed Properties ===
|
2024-10-02 23:10:35 +00:00
|
|
|
|
2025-12-12 23:22:21 +00:00
|
|
|
var current_room_path: String:
|
2026-01-16 12:09:09 +00:00
|
|
|
get:
|
|
|
|
|
return Main.room_paths[current_room] if Main else ""
|
2024-10-02 23:10:35 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
var is_empty: bool:
|
|
|
|
|
get:
|
2026-01-16 11:08:25 +00:00
|
|
|
return not FileAccess.file_exists(filepath) or (current_room == State.rooms.NULL)
|
2026-01-16 12:03:39 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
# === Data Fields ===
|
|
|
|
|
|
|
|
|
|
@export var filepath: String = ""
|
|
|
|
|
@export var unique_save_name: String = ""
|
|
|
|
|
@export var current_room: State.rooms = State.rooms.NULL
|
|
|
|
|
@export_flags("Intro", "Childhood", "Voice Training", "Jui Jutsu") var mementos_complete: int = 0
|
|
|
|
|
@export_flags_2d_physics var sequences_enabled: int = 63
|
|
|
|
|
@export var board_state: Dictionary = {"cards": {}, "stickies": {}, "randoms": []}
|
|
|
|
|
@export var childhood_mementos: Dictionary = {"cards": {}, "stickies": {}, "randoms": []}
|
|
|
|
|
@export var is_childhood_board_complete: bool = false
|
|
|
|
|
@export var player_position: Vector3 = Vector3.ZERO
|
|
|
|
|
@export var player_yaw: float = 0.0
|
|
|
|
|
@export var player_pitch: float = 0.0
|
|
|
|
|
@export var last_saved: int = 0
|
|
|
|
|
@export var is_valid: bool = false
|
|
|
|
|
@export var is_demo: bool = false
|
|
|
|
|
|
2026-01-16 12:12:49 +00:00
|
|
|
var thumbnail: Texture = preload("res://import/interface-elements/empty_save_slot.png")
|
|
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
# === Editor Conveniences ===
|
|
|
|
|
|
2025-03-25 21:34:13 +00:00
|
|
|
@export var save_manually: bool = false:
|
|
|
|
|
set(val):
|
2026-01-16 12:09:09 +00:00
|
|
|
if val:
|
|
|
|
|
save_to_file(thumbnail)
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2024-10-06 09:38:15 +00:00
|
|
|
func _validate_property(property: Dictionary):
|
2026-01-16 12:09:09 +00:00
|
|
|
if property.name == "filepath":
|
2025-03-25 21:34:13 +00:00
|
|
|
property.usage |= PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED
|
2026-01-16 12:09:09 +00:00
|
|
|
if property.name in ["thumbnail", "is_valid", "is_empty"]:
|
2025-03-25 21:34:13 +00:00
|
|
|
property.usage |= PROPERTY_USAGE_READ_ONLY
|
2024-10-06 09:38:15 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
# === Static Helpers ===
|
|
|
|
|
|
|
|
|
|
## Generates a unique save filepath
|
|
|
|
|
static func generate_save_path() -> String:
|
|
|
|
|
var timestamp := "%s_%s" % [Time.get_date_string_from_system(), Time.get_time_string_from_system().replace(":", "-")]
|
|
|
|
|
var unique_name := "frame_of_mind_%s-%d" % [timestamp, randi()]
|
|
|
|
|
return "%s/%s.tres" % [State.user_saves_path, unique_name]
|
|
|
|
|
|
|
|
|
|
## Creates a new save game with generated filepath
|
|
|
|
|
static func create_new() -> SaveGame:
|
|
|
|
|
var save := SaveGame.new()
|
|
|
|
|
save.filepath = generate_save_path()
|
|
|
|
|
save.unique_save_name = save.filepath.get_file().get_basename()
|
|
|
|
|
save.is_valid = true
|
|
|
|
|
save.is_demo = OS.has_feature("Demo")
|
|
|
|
|
save.last_saved = int(Time.get_unix_time_from_system())
|
2026-01-16 12:03:39 +00:00
|
|
|
|
|
|
|
|
# Ensure save directory exists
|
2026-01-16 12:09:09 +00:00
|
|
|
if not DirAccess.dir_exists_absolute(save.filepath.get_base_dir()):
|
|
|
|
|
DirAccess.make_dir_absolute(save.filepath.get_base_dir())
|
|
|
|
|
|
|
|
|
|
print_debug("SaveGame: Created new save: %s" % save.filepath)
|
|
|
|
|
return save
|
|
|
|
|
|
|
|
|
|
## Creates a DEBUG save (not persisted to disk)
|
|
|
|
|
static func create_debug() -> SaveGame:
|
|
|
|
|
var save := SaveGame.new()
|
|
|
|
|
save.filepath = "DEBUG"
|
|
|
|
|
save.unique_save_name = "DEBUG"
|
|
|
|
|
save.is_valid = true
|
|
|
|
|
save.is_demo = OS.has_feature("Demo")
|
|
|
|
|
|
|
|
|
|
if OS.has_feature("debug") or OS.has_feature("demo"):
|
|
|
|
|
push_warning("Created DEBUG savegame. Progress will not be stored!")
|
|
|
|
|
else:
|
|
|
|
|
push_error("Created DEBUG savegame outside of demo/debug environment. This will lead to data loss!")
|
2026-01-16 12:03:39 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
return save
|
2026-01-16 12:03:39 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
## Loads an existing save from disk
|
2026-01-16 12:03:39 +00:00
|
|
|
static func load_from_file(save_filepath: String) -> SaveGame:
|
|
|
|
|
if not FileAccess.file_exists(save_filepath):
|
|
|
|
|
push_error("SaveGame: File does not exist: %s" % save_filepath)
|
|
|
|
|
return null
|
|
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
print_debug("SaveGame: Loading from: %s" % save_filepath)
|
2026-01-16 12:03:39 +00:00
|
|
|
|
|
|
|
|
var loaded: SaveGame = ResourceLoader.load(save_filepath, "", ResourceLoader.CACHE_MODE_IGNORE)
|
|
|
|
|
|
|
|
|
|
if not loaded:
|
|
|
|
|
push_error("Failed to load SaveGame resource from: %s" % save_filepath)
|
|
|
|
|
return null
|
|
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
# Update filepath metadata
|
2026-01-16 12:03:39 +00:00
|
|
|
loaded.filepath = save_filepath
|
|
|
|
|
loaded.unique_save_name = save_filepath.get_file().get_basename()
|
|
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
# Backwards compatibility
|
2026-01-16 12:03:39 +00:00
|
|
|
if "randoms" not in loaded.board_state:
|
|
|
|
|
loaded.board_state["randoms"] = []
|
|
|
|
|
|
|
|
|
|
# Load thumbnail separately (not stored in .tres)
|
2026-01-16 12:09:09 +00:00
|
|
|
_load_thumbnail(loaded)
|
2026-01-16 12:03:39 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
# Validate
|
|
|
|
|
loaded.is_valid = loaded._validate()
|
2026-01-16 12:03:39 +00:00
|
|
|
|
|
|
|
|
if not loaded.is_valid:
|
|
|
|
|
push_error("Validation of loaded save failed: %s" % save_filepath)
|
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
return loaded
|
|
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
## Helper to load thumbnail from separate PNG file
|
|
|
|
|
static func _load_thumbnail(save: SaveGame) -> void:
|
|
|
|
|
var thumbnail_path := "%s/thumbnails/%s.png" % [save.filepath.get_base_dir(), save.unique_save_name]
|
|
|
|
|
if FileAccess.file_exists(thumbnail_path):
|
|
|
|
|
var img := Image.load_from_file(thumbnail_path)
|
|
|
|
|
if img:
|
|
|
|
|
save.thumbnail = ImageTexture.create_from_image(img)
|
|
|
|
|
|
|
|
|
|
# === Instance Methods ===
|
|
|
|
|
|
|
|
|
|
## Captures current player position/rotation from State.player
|
2026-01-16 12:03:39 +00:00
|
|
|
func capture_player_state() -> void:
|
2026-01-16 12:09:09 +00:00
|
|
|
if not State.player:
|
|
|
|
|
return
|
2024-10-06 09:38:15 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
player_position = State.player.global_position
|
|
|
|
|
var yaw: Node3D = State.player.get_node("Yaw")
|
|
|
|
|
var pitch: Node3D = yaw.get_node("Pitch")
|
|
|
|
|
player_yaw = yaw.rotation.y
|
|
|
|
|
player_pitch = pitch.rotation.x
|
|
|
|
|
|
|
|
|
|
print_debug("SaveGame: Captured player state - pos: %s, yaw: %.2f, pitch: %.2f" % [player_position, player_yaw, player_pitch])
|
2025-12-13 23:53:48 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
## Saves to disk with thumbnail
|
|
|
|
|
func save_to_file(screen_shot: Texture) -> void:
|
2025-09-04 23:54:21 +00:00
|
|
|
if filepath == "DEBUG":
|
2026-01-16 12:09:09 +00:00
|
|
|
push_warning("SaveGame: DEBUG save skipped (intentional).")
|
2025-09-04 23:54:21 +00:00
|
|
|
return
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2025-10-29 21:49:29 +00:00
|
|
|
if current_room == State.rooms.NULL:
|
2026-01-16 12:09:09 +00:00
|
|
|
push_warning("SaveGame: Not saving empty savegame.")
|
2025-10-29 21:49:29 +00:00
|
|
|
return
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
print_debug("SaveGame: Saving to file: %s" % filepath)
|
2026-01-16 12:03:39 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
# Capture current state
|
|
|
|
|
capture_player_state()
|
2025-12-12 18:19:37 +00:00
|
|
|
last_saved = int(Time.get_unix_time_from_system())
|
2026-01-16 12:03:39 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
# Save thumbnail
|
|
|
|
|
_save_thumbnail(screen_shot)
|
|
|
|
|
|
|
|
|
|
# Save resource
|
|
|
|
|
var result := ResourceSaver.save(self, filepath)
|
|
|
|
|
if result != OK:
|
|
|
|
|
push_error("Failed to save resource to: %s (Error: %d)" % [filepath, result])
|
|
|
|
|
else:
|
|
|
|
|
print_debug("Successfully saved to: %s" % filepath)
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
## Processes and saves thumbnail as PNG
|
|
|
|
|
func _save_thumbnail(screen_shot: Texture) -> void:
|
|
|
|
|
var img: Image = screen_shot.get_image()
|
|
|
|
|
img.convert(Image.Format.FORMAT_RGB8)
|
|
|
|
|
img.linear_to_srgb()
|
|
|
|
|
img.resize(384, 216, Image.INTERPOLATE_LANCZOS)
|
|
|
|
|
img.crop(384, 216)
|
|
|
|
|
|
|
|
|
|
# Ensure thumbnails directory exists
|
2025-12-12 18:19:37 +00:00
|
|
|
var save_dir := DirAccess.open(filepath.get_base_dir())
|
2025-03-25 21:34:13 +00:00
|
|
|
if not save_dir.dir_exists("thumbnails"):
|
|
|
|
|
save_dir.make_dir("thumbnails")
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
var thumbnail_path := "%s/thumbnails/%s.png" % [filepath.get_base_dir(), unique_save_name]
|
|
|
|
|
img.save_png(thumbnail_path)
|
2026-01-16 12:03:39 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
## Validates save data integrity
|
|
|
|
|
func _validate() -> bool:
|
|
|
|
|
if current_room < 0 or current_room >= State.rooms.keys().size():
|
|
|
|
|
return false
|
|
|
|
|
return validate_board_state()
|
2025-12-08 09:43:02 +00:00
|
|
|
|
2026-01-16 12:09:09 +00:00
|
|
|
# === Helper Methods ===
|
2025-03-25 21:34:13 +00:00
|
|
|
|
|
|
|
|
func calculate_completed_sequences() -> int:
|
2026-01-16 12:09:09 +00:00
|
|
|
var i: int = mementos_complete - ((mementos_complete >> 1) & 0x55555555)
|
|
|
|
|
i = (i & 0x33333333) + ((i >> 2) & 0x33333333)
|
|
|
|
|
i = (i + (i >> 4)) & 0x0F0F0F0F
|
|
|
|
|
i *= 0x01010101
|
|
|
|
|
return i >> 24
|
2025-03-25 21:34:13 +00:00
|
|
|
|
|
|
|
|
func calculate_total_connections() -> int:
|
2026-01-16 12:09:09 +00:00
|
|
|
var connections := 0
|
2025-03-25 21:34:13 +00:00
|
|
|
for sticky_position in board_state.stickies.values():
|
|
|
|
|
connections += int(sticky_position is String)
|
|
|
|
|
return connections
|
|
|
|
|
|
|
|
|
|
func validate_board_state() -> bool:
|
2026-01-16 12:09:09 +00:00
|
|
|
if not board_state.has("cards") or not board_state.has("stickies"):
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
for card in board_state.cards.values():
|
|
|
|
|
if not card is Vector2:
|
|
|
|
|
push_error("Save %s: Corrupted cards" % unique_save_name)
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
for sticky in board_state.stickies.values():
|
|
|
|
|
if not (sticky is int or sticky is Vector2 or sticky is float or board_state.cards.has(sticky)):
|
|
|
|
|
push_error("Save %s: Corrupted sticky notes" % unique_save_name)
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
return true
|