class_name FarmState extends RefCounted ## The single source of truth for everything that persists through the vacation: farm assets, ## scarecrow health, campaign progress, and the nightly loss records. Scene nodes only display ## it. to_dict() / from_dict() round-trip every field, also through JSON (the morning retry ## snapshot now, save games later). Plain asset counts become per-unit records in Step 3. const VERSION := 1 const KEYS: Array[String] = [ "night", "nights_survived", "phase", "phase_time_left", "scarecrow_max_hp", "scarecrow_hp", "crops_total", "crops", "animals_total", "animals", "supplies_total", "supplies", "night_log", ] const LOG_KEYS: Array[String] = ["night", "crops_lost", "animals_lost", "supplies_lost"] ## Current night (1-based). The morning, day, dusk, night, and dawn of a cycle all belong to it. var night: int = 1 var nights_survived: int = 0 var phase: CampaignController.Phase = CampaignController.Phase.MORNING ## Game seconds left in a timed phase (0 in the untimed morning and terminal phases). var phase_time_left: float = 0.0 var scarecrow_max_hp: int = 10 var scarecrow_hp: int = 10 ## Per category: the starting count (0 = not in play yet) and what remains on the farm. var crops_total: int = 0 var crops: int = 0 var animals_total: int = 0 var animals: int = 0 var supplies_total: int = 0 var supplies: int = 0 ## One entry per completed night, closed when its dawn ends: ## {"night", "crops_lost", "animals_lost", "supplies_lost"}. var night_log: Array[Dictionary] = [] static func create(cfg: GameConfig) -> FarmState: var s := FarmState.new() s.scarecrow_max_hp = cfg.scarecrow_max_hp s.scarecrow_hp = cfg.scarecrow_max_hp s.crops_total = cfg.start_crops s.crops = cfg.start_crops s.animals_total = cfg.start_animals s.animals = cfg.start_animals s.supplies_total = cfg.start_supplies s.supplies = cfg.start_supplies return s ## D2: every category in play is gone. Categories not in play (starting count 0) are ignored. func is_stripped() -> bool: var in_play := false for pair: Vector2i in [Vector2i(crops_total, crops), Vector2i(animals_total, animals), Vector2i(supplies_total, supplies)]: if pair.x > 0: in_play = true if pair.y > 0: return false return in_play func asset_counts() -> PackedInt32Array: return PackedInt32Array([crops, animals, supplies]) ## Records the night that just ended, given the asset counts when it started. func log_night(start: PackedInt32Array) -> void: var now := asset_counts() if start.size() != now.size(): start = now night_log.append({ "night": night, "crops_lost": maxi(start[0] - now[0], 0), "animals_lost": maxi(start[1] - now[1], 0), "supplies_lost": maxi(start[2] - now[2], 0), }) func to_dict() -> Dictionary: var entries: Array = [] for entry in night_log: entries.append(entry.duplicate()) return { "version": VERSION, "night": night, "nights_survived": nights_survived, "phase": int(phase), "phase_time_left": phase_time_left, "scarecrow_max_hp": scarecrow_max_hp, "scarecrow_hp": scarecrow_hp, "crops_total": crops_total, "crops": crops, "animals_total": animals_total, "animals": animals, "supplies_total": supplies_total, "supplies": supplies, "night_log": entries, } ## Rebuilds a state from to_dict() output (numbers may arrive as floats after JSON). Returns ## null for another version, missing keys, or values that break the state's invariants. static func from_dict(d: Dictionary) -> FarmState: if int(d.get("version", -1)) != VERSION: return null for key in KEYS: if not d.has(key): return null var phase_value := int(d["phase"]) if not CampaignController.Phase.values().has(phase_value): return null if not d["night_log"] is Array: return null var s := FarmState.new() s.night = int(d["night"]) s.nights_survived = int(d["nights_survived"]) s.phase = phase_value as CampaignController.Phase s.phase_time_left = float(d["phase_time_left"]) s.scarecrow_max_hp = int(d["scarecrow_max_hp"]) s.scarecrow_hp = int(d["scarecrow_hp"]) s.crops_total = int(d["crops_total"]) s.crops = int(d["crops"]) s.animals_total = int(d["animals_total"]) s.animals = int(d["animals"]) s.supplies_total = int(d["supplies_total"]) s.supplies = int(d["supplies"]) for raw in d["night_log"]: if not raw is Dictionary: return null var entry := {} for key in LOG_KEYS: if not (raw as Dictionary).has(key): return null entry[key] = int(raw[key]) s.night_log.append(entry) return s if s.is_valid() else null func is_valid() -> bool: return night >= 1 and nights_survived >= 0 and nights_survived <= night \ and phase_time_left >= 0.0 \ and scarecrow_max_hp >= 1 and scarecrow_hp >= 0 and scarecrow_hp <= scarecrow_max_hp \ and crops >= 0 and crops <= crops_total \ and animals >= 0 and animals <= animals_total \ and supplies >= 0 and supplies <= supplies_total