class_name LimbWounds extends RefCounted # ASCII parity: one recoverable wound per body slot, with severity 1..3. const SLOTS: Array[StringName] = [&"head", &"torso", &"arms", &"legs"] const DEFAULT_DURATION := 8 const MAX_SEVERITY := 3 var _wounds: Dictionary[StringName, Dictionary] = {} func apply(slot: StringName, severity: int = 1, duration: int = DEFAULT_DURATION, source: String = "") -> void: if not SLOTS.has(slot): return var existing: Dictionary = _wounds.get(slot, {}) _wounds[slot] = { "severity": maxi(int(existing.get("severity", 0)), clampi(severity, 1, MAX_SEVERITY)), "duration": maxi(int(existing.get("duration", 0)), maxi(1, duration)), "source": source if not source.is_empty() else String(existing.get("source", "")), } func severity_at(slot: StringName) -> int: return int(_wounds.get(slot, {}).get("severity", 0)) func duration_at(slot: StringName) -> int: return int(_wounds.get(slot, {}).get("duration", 0)) func source_at(slot: StringName) -> String: return String(_wounds.get(slot, {}).get("source", "")) func tick(recovery_multiplier: float = 1.0) -> Array[StringName]: var recovered: Array[StringName] = [] var step := 2 if recovery_multiplier >= 2.0 else 1 for slot in _wounds.keys(): var wound: Dictionary = _wounds[slot] wound["duration"] = int(wound["duration"]) - step if int(wound["duration"]) <= 0: _wounds.erase(slot) recovered.append(slot) else: _wounds[slot] = wound return recovered func active_wounds() -> Array[Dictionary]: var active: Array[Dictionary] = [] for slot in SLOTS: if _wounds.has(slot): var wound: Dictionary = _wounds[slot].duplicate(true) wound["slot"] = slot active.append(wound) return active func to_dictionary() -> Dictionary: return _wounds.duplicate(true) static func from_dictionary(data: Dictionary = {}) -> LimbWounds: var wounds := LimbWounds.new() for raw_slot in data: var slot := StringName(raw_slot) var wound: Dictionary = data[raw_slot] wounds.apply(slot, int(wound.get("severity", 1)), int(wound.get("duration", DEFAULT_DURATION)), String(wound.get("source", ""))) return wounds