40 lines
1.1 KiB
GDScript
40 lines
1.1 KiB
GDScript
class_name VillageStateRecord
|
|
extends RefCounted
|
|
|
|
const SCHEMA_VERSION := 1
|
|
|
|
var data: Dictionary
|
|
|
|
func _init(record_data: Dictionary = {}) -> void:
|
|
data = record_data.duplicate(true)
|
|
|
|
static func capture(village: SimVillage) -> VillageStateRecord:
|
|
return VillageStateRecord.new({
|
|
"schema_version": SCHEMA_VERSION,
|
|
"food": village.food,
|
|
"wood": village.wood,
|
|
"safety": village.safety,
|
|
"knowledge": village.knowledge
|
|
})
|
|
|
|
static func from_dictionary(record_data: Dictionary) -> VillageStateRecord:
|
|
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
|
return null
|
|
if not record_data.has_all(["food", "wood", "safety", "knowledge"]):
|
|
return null
|
|
return VillageStateRecord.new(record_data)
|
|
|
|
func restore(debug_logs: bool) -> SimVillage:
|
|
var village := SimVillage.new()
|
|
village.debug_logs = debug_logs
|
|
village.food = float(data["food"])
|
|
village.wood = float(data["wood"])
|
|
village.safety = float(data["safety"])
|
|
village.knowledge = float(data["knowledge"])
|
|
village.update_modifiers()
|
|
village.update_priorities()
|
|
return village
|
|
|
|
func to_dictionary() -> Dictionary:
|
|
return data.duplicate(true)
|