Fix: counter-attack animation fires even when defender absorbs damage with defense

Previously the counterattack chain was only triggered when both units appeared
in damaged_iids (HP decreased).  If the defender's defense stat absorbs the
attacker's hit entirely, their HP doesn't change, so they were never in
damaged_iids — the counterattack animation was silently skipped.

Fix: record _last_attack_target_iid in send_attack() and use it in
_play_damage_animations as the primary defender source.  The tracked target is
consumed (cleared) on first use so it never bleeds into later state updates.
The old fallback (any other damaged unit) is kept for cases where no tracked
target is available.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-03-16 13:58:36 -04:00
co-authored by Claude Happy
parent f5f0dd91e0
commit cf771f8531
@@ -159,6 +159,7 @@ var _local_is_p1 : bool = false # true when Auth.user_id == playerOneAccountI
var _prev_unit_hp : Dictionary = {} # instanceId → int, HP snapshot for damage detection
var _prev_unit_pos: Dictionary = {} # instanceId → Vector2i, position snapshot for move detection
var _move_anims : Dictionary = {} # instanceId → Tween (active movement tweens)
var _last_attack_target_iid : String = "" # target of the most recent send_attack() call
# Delayed rebuild: when animations need time to play we don't destroy unit nodes immediately.
var _pending_rebuild : Dictionary = {} # latest state waiting for the delayed rebuild
@@ -1008,14 +1009,25 @@ func _play_damage_animations(state: Dictionary) -> bool:
if damaged_iids.is_empty() and dying_iids.is_empty():
return false
# ── Detect counterattack: attacker AND another unit both took damage ──────
# ── Detect counterattack: attacker took damage while mid-attack ─────────
# The defender may have blocked all HP damage (defense stat), so we can't
# rely on them being in damaged_iids. Use the tracked target from the last
# send_attack() call as the primary source, and fall back to any other
# damaged unit if the tracked target isn't available.
var tracked_target : String = _last_attack_target_iid
_last_attack_target_iid = "" # consume; don't bleed into the next update
var is_counterattack : bool = false
var defender_iid : String = ""
if attacker_iid != "" and damaged_iids.has(attacker_iid) and damaged_iids.size() >= 2:
for iid: String in damaged_iids:
if iid != attacker_iid:
defender_iid = iid
break
if attacker_iid != "" and damaged_iids.has(attacker_iid):
# Prefer the unit we explicitly attacked (works even when they took 0 damage).
if tracked_target != "" and tracked_target != attacker_iid:
defender_iid = tracked_target
else:
# Fallback: any other damaged unit (original behaviour).
for iid: String in damaged_iids:
if iid != attacker_iid:
defender_iid = iid
break
is_counterattack = defender_iid != ""
# ── Schedule animations ───────────────────────────────────────────────────
@@ -1506,6 +1518,7 @@ func _handle_board_click(mouse_pos: Vector2) -> void:
Mode.UNIT_SELECTED:
if not unit_id.is_empty():
if _is_enemy_unit(unit_id):
_last_attack_target_iid = unit_id
_play_unit_anim(_sel_unit_id, "attack")
Game.send_attack(_sel_unit_id, unit_id)
_set_mode(Mode.NONE)