Files
Ratdoku/scripts/SaveGame.gd
T
Ratdoku Builder af4951baf8 feat(input): guard Guest Book taps and add double-tap placement
Guest Book: only open a card on a short stationary tap, ignore drags and flings, and guard the detail Close button after opening. Board: add a persisted double-tap placement mode with deferred single taps, while hold stays the default and right-click works in both modes.
2026-12-02 20:10:44 -06:00

121 lines
2.6 KiB
GDScript

extends Node
## Persistent settings and room progress.
## Autoloaded as `SaveGame` (see project.godot).
signal settings_changed
signal progress_changed
const PATH := "user://ratdoku_save.json"
const DEFAULTS := {
"patterns": true,
"show_timer": true,
"reduce_motion": false,
"hold_seconds": 0.35,
"double_tap_place": false,
}
var data := {}
func _ready() -> void:
load_game()
func _default_data() -> Dictionary:
return {
"version": 1,
"completed_rooms": [],
"unlocked_guests": [],
"settings": DEFAULTS.duplicate(true),
"last_room": 0,
}
func load_game() -> void:
data = _default_data()
if FileAccess.file_exists(PATH):
var file := FileAccess.open(PATH, FileAccess.READ)
if file != null:
var parsed = JSON.parse_string(file.get_as_text())
file.close()
if typeof(parsed) == TYPE_DICTIONARY:
for key in parsed:
data[key] = parsed[key]
if typeof(data.get("settings")) != TYPE_DICTIONARY:
data["settings"] = DEFAULTS.duplicate(true)
for key in DEFAULTS:
if not data["settings"].has(key):
data["settings"][key] = DEFAULTS[key]
if typeof(data.get("completed_rooms")) != TYPE_ARRAY:
data["completed_rooms"] = []
if typeof(data.get("unlocked_guests")) != TYPE_ARRAY:
data["unlocked_guests"] = []
func save_game() -> void:
var file := FileAccess.open(PATH, FileAccess.WRITE)
if file == null:
return
file.store_string(JSON.stringify(data, "\t"))
file.close()
func get_setting(key: String):
return data["settings"].get(key, DEFAULTS.get(key))
func set_setting(key: String, value) -> void:
data["settings"][key] = value
save_game()
settings_changed.emit()
func is_guest_unlocked(id: String) -> bool:
var guest := Guests.by_id(id)
if guest.is_empty():
return false
if int(guest.get("unlock_room", 0)) == 0:
return true
return data["unlocked_guests"].has(id)
func complete_room(room_id: String, room_number: int) -> String:
if not data["completed_rooms"].has(room_id):
data["completed_rooms"].append(room_id)
var last: int = max(int(data.get("last_room", 0)), room_number)
data["last_room"] = last
var unlocked := ""
var guest := Guests.for_room(room_number)
if not guest.is_empty():
var id := str(guest["id"])
if not data["unlocked_guests"].has(id):
data["unlocked_guests"].append(id)
unlocked = id
save_game()
progress_changed.emit()
return unlocked
func reset_progress() -> void:
data["completed_rooms"] = []
data["unlocked_guests"] = []
data["last_room"] = 0
save_game()
progress_changed.emit()
func unlocked_count(guests: Array) -> int:
var total := 0
for guest in guests:
if is_guest_unlocked(str(guest["id"])):
total += 1
return total