Vector2 is not a JSON-native type — JSON.stringify converts it to a string, which cannot be assigned back to a Vector2 return type on load. Positions are now saved as [x, y] float arrays and reconstructed as Vector2 in apply_save_data. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
66 lines
1.8 KiB
GDScript
66 lines
1.8 KiB
GDScript
## GameState — global game state: character positions, object states, current room.
|
|
extends Node
|
|
|
|
signal state_changed
|
|
signal character_moved(character_id: String, position: Vector2)
|
|
|
|
var _character_positions: Dictionary = {}
|
|
var _object_states: Dictionary = {}
|
|
var current_room: String = "reception"
|
|
var music_volume: float = 0.6
|
|
var sfx_volume: float = 1.0
|
|
|
|
|
|
func has_character_position(id: String) -> bool:
|
|
return _character_positions.has(id)
|
|
|
|
|
|
func get_character_position(id: String) -> Vector2:
|
|
return _character_positions.get(id, Vector2.ZERO)
|
|
|
|
|
|
func set_character_position(id: String, pos: Vector2) -> void:
|
|
_character_positions[id] = pos
|
|
character_moved.emit(id, pos)
|
|
state_changed.emit()
|
|
|
|
|
|
func get_object_state(id: String) -> String:
|
|
return _object_states.get(id, "idle")
|
|
|
|
|
|
func set_object_state(id: String, state: String) -> void:
|
|
_object_states[id] = state
|
|
state_changed.emit()
|
|
|
|
|
|
func get_save_data() -> Dictionary:
|
|
var positions: Dictionary = {}
|
|
for key: String in _character_positions:
|
|
var pos: Vector2 = _character_positions[key]
|
|
positions[key] = [pos.x, pos.y]
|
|
return {
|
|
"character_positions": positions,
|
|
"object_states": _object_states,
|
|
"current_room": current_room,
|
|
"music_volume": music_volume,
|
|
"sfx_volume": sfx_volume,
|
|
}
|
|
|
|
|
|
func apply_save_data(data: Dictionary) -> void:
|
|
if data.has("character_positions"):
|
|
_character_positions = {}
|
|
for key: String in data["character_positions"]:
|
|
var val: Variant = data["character_positions"][key]
|
|
if val is Array and val.size() >= 2:
|
|
_character_positions[key] = Vector2(val[0], val[1])
|
|
if data.has("object_states"):
|
|
_object_states = data["object_states"]
|
|
if data.has("current_room"):
|
|
current_room = data["current_room"]
|
|
if data.has("music_volume"):
|
|
music_volume = data["music_volume"]
|
|
if data.has("sfx_volume"):
|
|
sfx_volume = data["sfx_volume"]
|