Fix: full counter-attack animation chain (attack → damaged → counter → damaged)
Previously both units played "damaged" simultaneously after the attacker's attack animation. Now when both units take damage in the same state update (= counterattack), the full chain fires: 1. Mob 1 finishes Attack animation 2. Mob 2 plays Damaged (hit by Mob 1) 3. Mob 2 plays Attack (counterattack) 4. Mob 1 plays Damaged (hit by Mob 2) + Mob 2 returns to Idle Implementation: - _override_oneshot(iid, cb): replaces a pending animation_finished ONE_SHOT callback without leaving stale signal connections - _play_counter_attack(defender, attacker): plays defender "attack", then triggers attacker "damaged" + defender "idle" via _override_oneshot - _play_counter_chain(defender, attacker, delay): tween-delayed entry into the chain; starts with defender "damaged" then hands off to _play_counter_attack - _play_damage_animations: detects counterattack (attacker_iid AND another unit both in damaged_iids), routes to _play_counter_chain; falls through to the existing simple-damage path otherwise 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:
@@ -915,28 +915,81 @@ func _is_attack_track(full_name: String) -> bool:
|
||||
return false
|
||||
|
||||
|
||||
## Replaces the pending animation_finished one-shot callback for [param iid].
|
||||
## Disconnects the previous callback (stored in _oneshot_callbacks) and
|
||||
## connects [param cb] as a new CONNECT_ONE_SHOT handler.
|
||||
func _override_oneshot(iid: String, cb: Callable) -> void:
|
||||
var ap : AnimationPlayer = _anim_players.get(iid, null)
|
||||
if ap == null or not is_instance_valid(ap):
|
||||
return
|
||||
if _oneshot_callbacks.has(iid):
|
||||
var old_cb := _oneshot_callbacks[iid] as Callable
|
||||
if ap.animation_finished.is_connected(old_cb):
|
||||
ap.animation_finished.disconnect(old_cb)
|
||||
_oneshot_callbacks.erase(iid)
|
||||
ap.animation_finished.connect(cb, CONNECT_ONE_SHOT)
|
||||
_oneshot_callbacks[iid] = cb
|
||||
|
||||
|
||||
## Counterattack phase 2: defender plays "attack", then triggers attacker "damaged".
|
||||
func _play_counter_attack(defender_iid: String, attacker_iid: String) -> void:
|
||||
if not _unit_bodies.has(defender_iid): return
|
||||
if not is_instance_valid(_unit_bodies.get(defender_iid) as Node): return
|
||||
_play_unit_anim(defender_iid, "attack", false)
|
||||
# After the counter-attack finishes: defender → idle, attacker → damaged.
|
||||
_override_oneshot(defender_iid, func(_n: String) -> void:
|
||||
_play_unit_anim(defender_iid, "idle", true)
|
||||
if _unit_bodies.has(attacker_iid) and \
|
||||
is_instance_valid(_unit_bodies.get(attacker_iid) as Node):
|
||||
_play_unit_anim(attacker_iid, "damaged", false)
|
||||
)
|
||||
|
||||
|
||||
## Full counterattack chain (starts after [param delay] seconds):
|
||||
## delay → defender "damaged" → defender "attack" → attacker "damaged" + defender "idle"
|
||||
func _play_counter_chain(defender_iid: String, attacker_iid: String, delay: float) -> void:
|
||||
var t := create_tween()
|
||||
t.tween_callback(func() -> void:
|
||||
if not _unit_bodies.has(defender_iid) or \
|
||||
not is_instance_valid(_unit_bodies.get(defender_iid) as Node):
|
||||
return
|
||||
_play_unit_anim(defender_iid, "damaged", false)
|
||||
_override_oneshot(defender_iid, func(_n: String) -> void:
|
||||
_play_counter_attack(defender_iid, attacker_iid)
|
||||
)
|
||||
).set_delay(delay)
|
||||
|
||||
|
||||
## Detect HP decreases and unit deaths vs previous state, and fire animations.
|
||||
## Sequences damage animations after any in-progress attack:
|
||||
## attacker → "damaged" queued behind attack via animation_finished override
|
||||
## defender → "damaged" delayed by the attack's remaining duration
|
||||
##
|
||||
## Counterattack scenario (attacker AND a second unit both took damage this update):
|
||||
## delay(attack_remaining) → defender "damaged" → defender "attack" → attacker "damaged"
|
||||
##
|
||||
## Simple damage scenario:
|
||||
## attacker mid-attack + took damage → queue "damaged" behind its attack
|
||||
## other unit, attack in progress → delay "damaged" by attack_remaining
|
||||
## no attack in progress → play "damaged" immediately
|
||||
##
|
||||
## Returns true if any animation was triggered (used to decide rebuild delay).
|
||||
func _play_damage_animations(state: Dictionary) -> bool:
|
||||
if _prev_unit_hp.is_empty():
|
||||
return false
|
||||
|
||||
# Find the remaining time of any currently-playing attack animation so we can
|
||||
# delay the defender's "damaged" to land at the moment the attack finishes.
|
||||
var attack_remaining : float = 0.0
|
||||
# ── Find any currently-playing attack animation ──────────────────────────
|
||||
var attacker_iid : String = ""
|
||||
var attack_remaining : float = 0.0
|
||||
for iid: String in _anim_players:
|
||||
var ap2 : AnimationPlayer = _anim_players[iid] as AnimationPlayer
|
||||
if ap2 == null or not is_instance_valid(ap2) or not ap2.is_playing(): continue
|
||||
if _is_attack_track(ap2.current_animation):
|
||||
attacker_iid = iid
|
||||
attack_remaining = maxf(0.0,
|
||||
ap2.current_animation_length - ap2.current_animation_position)
|
||||
break
|
||||
|
||||
var played := false
|
||||
var seen_iids : Dictionary = {}
|
||||
# ── Collect who took damage / died ───────────────────────────────────────
|
||||
var damaged_iids : Dictionary = {}
|
||||
var seen_iids : Dictionary = {}
|
||||
for player: Dictionary in state.get("players", []):
|
||||
for unit: Dictionary in ([player.get("archSpirit", {})] + player.get("field", [])):
|
||||
if unit.is_empty(): continue
|
||||
@@ -945,37 +998,73 @@ func _play_damage_animations(state: Dictionary) -> bool:
|
||||
var old_hp : int = _prev_unit_hp.get(iid, new_hp)
|
||||
seen_iids[iid] = true
|
||||
if new_hp < old_hp:
|
||||
var ap : AnimationPlayer = _anim_players.get(iid, null)
|
||||
if ap != null and is_instance_valid(ap) and ap.is_playing() \
|
||||
and _is_attack_track(ap.current_animation):
|
||||
# Attacker took counter-damage while mid-attack.
|
||||
# Override the idle-return callback to play "damaged" after attack finishes.
|
||||
if _oneshot_callbacks.has(iid):
|
||||
var old_cb := _oneshot_callbacks[iid] as Callable
|
||||
if ap.animation_finished.is_connected(old_cb):
|
||||
ap.animation_finished.disconnect(old_cb)
|
||||
_oneshot_callbacks.erase(iid)
|
||||
var dmg_cb := func(_n: String) -> void: _play_unit_anim(iid, "damaged", false)
|
||||
ap.animation_finished.connect(dmg_cb, CONNECT_ONE_SHOT)
|
||||
_oneshot_callbacks[iid] = dmg_cb
|
||||
elif attack_remaining > 0.0:
|
||||
# Defender (or bystander) — delay "damaged" until the attack lands.
|
||||
var hit_iid := iid # capture per-iteration
|
||||
var t := create_tween()
|
||||
t.tween_callback(func() -> void:
|
||||
if _unit_bodies.has(hit_iid) and \
|
||||
is_instance_valid(_unit_bodies[hit_iid] as StaticBody3D):
|
||||
_play_unit_anim(hit_iid, "damaged")
|
||||
).set_delay(attack_remaining)
|
||||
else:
|
||||
_play_unit_anim(iid, "damaged")
|
||||
played = true
|
||||
# Units that vanished this update → play dying.
|
||||
damaged_iids[iid] = true
|
||||
|
||||
var dying_iids : Dictionary = {}
|
||||
for iid: String in _prev_unit_hp:
|
||||
if not seen_iids.has(iid):
|
||||
_play_unit_anim(iid, "dying")
|
||||
played = true
|
||||
return played
|
||||
dying_iids[iid] = true
|
||||
|
||||
if damaged_iids.is_empty() and dying_iids.is_empty():
|
||||
return false
|
||||
|
||||
# ── Detect counterattack: attacker AND another unit both took damage ──────
|
||||
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
|
||||
is_counterattack = defender_iid != ""
|
||||
|
||||
# ── Schedule animations ───────────────────────────────────────────────────
|
||||
if is_counterattack:
|
||||
# Full chain handles the attacker and primary defender.
|
||||
_play_counter_chain(defender_iid, attacker_iid, attack_remaining)
|
||||
# Any other simultaneously-damaged units get a simple delay (uncommon).
|
||||
for iid: String in damaged_iids:
|
||||
if iid == attacker_iid or iid == defender_iid:
|
||||
continue
|
||||
var hit_iid := iid
|
||||
var t2 := create_tween()
|
||||
t2.tween_callback(func() -> void:
|
||||
if _unit_bodies.has(hit_iid) and \
|
||||
is_instance_valid(_unit_bodies.get(hit_iid) as Node):
|
||||
_play_unit_anim(hit_iid, "damaged")
|
||||
).set_delay(attack_remaining)
|
||||
else:
|
||||
for iid: String in damaged_iids:
|
||||
var ap : AnimationPlayer = _anim_players.get(iid, null)
|
||||
if ap != null and is_instance_valid(ap) and ap.is_playing() \
|
||||
and _is_attack_track(ap.current_animation):
|
||||
# This unit is mid-attack and took damage (self-damage or similar).
|
||||
# Queue "damaged" to play right after the attack finishes.
|
||||
if _oneshot_callbacks.has(iid):
|
||||
var old_cb := _oneshot_callbacks[iid] as Callable
|
||||
if ap.animation_finished.is_connected(old_cb):
|
||||
ap.animation_finished.disconnect(old_cb)
|
||||
_oneshot_callbacks.erase(iid)
|
||||
var cap_iid := iid
|
||||
var dmg_cb := func(_n: String) -> void: _play_unit_anim(cap_iid, "damaged", false)
|
||||
ap.animation_finished.connect(dmg_cb, CONNECT_ONE_SHOT)
|
||||
_oneshot_callbacks[iid] = dmg_cb
|
||||
elif attack_remaining > 0.0:
|
||||
# Bystander — delay "damaged" until after the attack lands.
|
||||
var hit_iid := iid
|
||||
var t := create_tween()
|
||||
t.tween_callback(func() -> void:
|
||||
if _unit_bodies.has(hit_iid) and \
|
||||
is_instance_valid(_unit_bodies.get(hit_iid) as Node):
|
||||
_play_unit_anim(hit_iid, "damaged")
|
||||
).set_delay(attack_remaining)
|
||||
else:
|
||||
_play_unit_anim(iid, "damaged")
|
||||
|
||||
for iid: String in dying_iids:
|
||||
_play_unit_anim(iid, "dying")
|
||||
|
||||
return true
|
||||
|
||||
|
||||
## Update _prev_unit_hp and _prev_unit_pos from the given state snapshot.
|
||||
|
||||
Reference in New Issue
Block a user