class_name SpellState extends RefCounted # Per-run learned spell and cooldown state. Definitions remain immutable catalog data. var learned_spells: Array[StringName] = [] var _cooldowns: Dictionary[StringName, int] = {} func _init(initial_spells: Array[StringName] = []) -> void: for spell_id in initial_spells: learn(spell_id) func learn(spell_id: StringName) -> void: if spell_id.is_empty() or spell_id in learned_spells: return learned_spells.append(spell_id) learned_spells.sort() _cooldowns[spell_id] = 0 func knows(spell_id: StringName) -> bool: return spell_id in learned_spells func known_spell_ids() -> Array[StringName]: return learned_spells.duplicate() func cooldown_for(spell_id: StringName) -> int: return maxi(0, _cooldowns.get(spell_id, 0)) func is_ready(spell_id: StringName) -> bool: return cooldown_for(spell_id) == 0 func start_cooldown(spell_id: StringName, turns: int) -> void: _cooldowns[spell_id] = maxi(0, turns) func advance_turn() -> void: for spell_id in _cooldowns: _cooldowns[spell_id] = maxi(0, _cooldowns[spell_id] - 1) func regenerate_focus(caster: ActorState, amount: int = 1) -> int: if caster == null or amount <= 0: return 0 var previous := caster.focus caster.focus = clampi(caster.focus + amount, 0, caster.max_focus) return caster.focus - previous