Create persistence manager and connectors for chests and gates

This commit is contained in:
2026-03-19 14:26:57 -05:00
parent 31567f53fb
commit 9fcd24f8b5
14 changed files with 163 additions and 9 deletions

View File

@@ -0,0 +1,21 @@
extends Node
@export var chests: Array[NodePath]
# Private Methods
func _ready() -> void:
for relative_node_path in chests:
var chest: BaseChest = get_node(relative_node_path)
var node_path := chest.get_path()
chest.ChestOpened.connect(func(): _on_chest_opened(chest))
if PersistenceManager.HasData(node_path):
var chest_opened: bool = PersistenceManager.GetData(node_path)
if chest_opened:
chest.call_deferred("SetOpenedFromLoad")
func _on_chest_opened(chest: BaseChest) -> void:
var node_path := chest.get_path()
PersistenceManager.UpdateData(node_path, true)
PersistenceManager.SaveToDisk()

View File

@@ -0,0 +1 @@
uid://75g2fysjrqji

View File

@@ -0,0 +1,40 @@
extends Node
@export var gates: Array[NodePath]
# Private Methods
func _ready() -> void:
for node_path in gates:
_handle_spike_gate(node_path)
func _handle_spike_gate(node_path: NodePath) -> bool:
var node := get_node(node_path)
node_path = node.get_path()
if not node.name.begins_with("Spike Gate"):
return false
# Connect Signal
node.connect("Opened", func(): _on_spike_gate_opened(node))
if not PersistenceManager.HasData(node_path):
return true
var is_open: bool = PersistenceManager.GetData(node_path)
if not is_open:
return true
var collision_shape: CollisionShape2D = node.get_node("CollisionShape2D")
var animated_sprite: AnimatedSprite2D = node.get_node("AnimatedSprite2D")
node.set_deferred("is_open", true)
collision_shape.set_deferred("disabled", true)
animated_sprite.call_deferred("play", "open")
return true
func _on_spike_gate_opened(node: Node) -> void:
var path := node.get_path()
PersistenceManager.UpdateData(path, true)
PersistenceManager.SaveToDisk()

View File

@@ -0,0 +1 @@
uid://brsiegtrne15y

View File

@@ -0,0 +1,6 @@
[gd_scene format=3 uid="uid://w4mh31se58f4"]
[ext_resource type="Script" uid="uid://75g2fysjrqji" path="res://Persistence/Connectors/Scripts/chest_persistence_connector.gd" id="1_hytou"]
[node name="Chest Persistence Connector" type="Node" unique_id=281805027]
script = ExtResource("1_hytou")

View File

@@ -0,0 +1,6 @@
[gd_scene format=3 uid="uid://c5ymu08jotbfp"]
[ext_resource type="Script" uid="uid://brsiegtrne15y" path="res://Persistence/Connectors/Scripts/gate_persistence_connector.gd" id="1_0smsr"]
[node name="GatePersistenceConnector" type="Node" unique_id=1082904286]
script = ExtResource("1_0smsr")

View File

@@ -0,0 +1,51 @@
extends Node
var _save_dictionary: Dictionary[NodePath, Variant] = {}
# Public Methods
func UpdateData(node_path: NodePath, value: Variant) -> void:
_save_dictionary[node_path] = value
func GetData(node_path: NodePath) -> Variant:
if not _save_dictionary.has(node_path):
return null
return _save_dictionary[node_path]
func HasData(node_path: NodePath) -> bool:
return _save_dictionary.has(node_path)
func SaveToDisk() -> void:
var save_file := FileAccess.open("user://savegame.save", FileAccess.WRITE)
var json := JSON.stringify(_save_dictionary)
save_file.store_line(json)
func LoadFromDisk() -> void:
if not FileAccess.file_exists("user://savegame.save"):
return
var save_file = FileAccess.open("user://savegame.save", FileAccess.READ)
print("Loading save file from path: %s" % save_file.get_path_absolute())
var line := save_file.get_line()
var json = JSON.new()
var parse_result := json.parse(line)
if parse_result != OK:
print("Parsing result when loading file: [%s] in %s at line %d" % [json.get_error_message(), line, json.get_error_line()])
return
var data_dict: Dictionary = json.data
var node_dict: Dictionary[NodePath, Variant]
for key in data_dict:
var value: Variant = data_dict[key]
var node_path := NodePath(key)
node_dict[node_path] = value
_save_dictionary = node_dict
# Private Methods
func _ready() -> void:
LoadFromDisk()

View File

@@ -0,0 +1 @@
uid://dl2kg1qu0ymi7