Fix: sequence attack→damaged animations (no mid-attack interruption)
Attack animations were being interrupted halfway through by "damaged"
because _play_damage_animations fired immediately on state arrival —
even for the attacker who also took counter-damage.
Fix:
- Track each unit's oneshot animation_finished callback in _oneshot_callbacks
so it can be replaced later without a stale lambda leak.
- _is_attack_track() helper matches current animation against ANIM_ALIASES.
- _play_damage_animations now sequences:
Attacker mid-attack → override idle-return callback with "damaged",
so attack plays fully, then damaged plays, then idle.
Defender → delay "damaged" tween by the attack's remaining duration
so the hit lands at the moment the swing connects.
No attack in progress → play "damaged" immediately (existing behaviour).
Result: Attack → (attack lands) defender damaged + attacker damaged → idle.
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:
@@ -174,8 +174,9 @@ const POST_ANIM_SECS : float = 1.2
|
||||
const ANIM_TRANSITION_SECS : float = 5.0 / 30.0 # 5 frames at 30 fps
|
||||
|
||||
# Per-unit animation loop tracking.
|
||||
var _idle_entry_done : Dictionary = {} # iid → true once the first idle entry has played
|
||||
var _idle_callbacks : Dictionary = {} # iid → Callable (active loop reconnect, to cancel on interrupt)
|
||||
var _idle_entry_done : Dictionary = {} # iid → true once the first idle entry has played
|
||||
var _idle_callbacks : Dictionary = {} # iid → Callable (active loop reconnect, to cancel on interrupt)
|
||||
var _oneshot_callbacks : Dictionary = {} # iid → Callable (animation_finished → next action, replaceable)
|
||||
# Per-unit model config.
|
||||
var _model_base_angles : Dictionary = {} # iid → float (base facing angle, radians)
|
||||
var _needs_face_rot : Dictionary = {} # iid → bool (team-based 180° flip flag)
|
||||
@@ -592,7 +593,8 @@ func _rebuild_units(state: Dictionary) -> void:
|
||||
_anim_players.erase(iid)
|
||||
_unit_def_ids.erase(iid)
|
||||
_idle_entry_done.erase(iid)
|
||||
_idle_callbacks.erase(iid) # callback already fired or animation stopping
|
||||
_idle_callbacks.erase(iid)
|
||||
_oneshot_callbacks.erase(iid)
|
||||
_model_base_angles.erase(iid)
|
||||
_needs_face_rot.erase(iid)
|
||||
|
||||
@@ -887,32 +889,86 @@ func _play_unit_anim(iid: String, action: String, loop: bool = false) -> void:
|
||||
ap.animation_finished.connect(cb, CONNECT_ONE_SHOT)
|
||||
_idle_callbacks[iid] = cb
|
||||
else:
|
||||
# For one-shot non-death animations: return to idle via animation_finished
|
||||
# (fires synchronously inside AnimationPlayer._process, so no gap frame).
|
||||
# For one-shot non-death animations: return to idle via animation_finished.
|
||||
var action_l := action.to_lower()
|
||||
var is_death := action_l == "dying" or action_l == "death" or action_l == "die"
|
||||
if not is_death:
|
||||
ap.animation_finished.connect(
|
||||
func(_n: String) -> void: _play_unit_anim(iid, "idle", true),
|
||||
CONNECT_ONE_SHOT)
|
||||
# Disconnect any prior oneshot callback — allows _play_damage_animations to
|
||||
# replace "return-to-idle" with "play-damaged-then-idle" on an attacking unit.
|
||||
if _oneshot_callbacks.has(iid):
|
||||
var old_os := _oneshot_callbacks[iid] as Callable
|
||||
if ap.animation_finished.is_connected(old_os):
|
||||
ap.animation_finished.disconnect(old_os)
|
||||
_oneshot_callbacks.erase(iid)
|
||||
var os_cb := func(_n: String) -> void: _play_unit_anim(iid, "idle", true)
|
||||
ap.animation_finished.connect(os_cb, CONNECT_ONE_SHOT)
|
||||
_oneshot_callbacks[iid] = os_cb
|
||||
|
||||
|
||||
## Returns true if the AnimationPlayer's current track is an "attack" animation.
|
||||
func _is_attack_track(full_name: String) -> bool:
|
||||
var s := full_name.get_slice("/", full_name.get_slice_count("/") - 1)
|
||||
for alias: String in ANIM_ALIASES.get("attack", []):
|
||||
if s.to_lower() == alias: return true
|
||||
if "." in s and s.get_slice(".", s.get_slice_count(".") - 1).to_lower() == alias: return true
|
||||
if "_" in s and s.substr(s.find("_") + 1).to_lower() == alias: return true
|
||||
return false
|
||||
|
||||
|
||||
## 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
|
||||
## 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
|
||||
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):
|
||||
attack_remaining = maxf(0.0,
|
||||
ap2.current_animation_length - ap2.current_animation_position)
|
||||
break
|
||||
|
||||
var played := false
|
||||
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
|
||||
var iid := str(unit.get("instanceId", ""))
|
||||
var new_hp := int(unit.get("currentHealth", 0))
|
||||
var old_hp : int = _prev_unit_hp.get(iid, new_hp)
|
||||
var iid := str(unit.get("instanceId", ""))
|
||||
var new_hp := int(unit.get("currentHealth", 0))
|
||||
var old_hp : int = _prev_unit_hp.get(iid, new_hp)
|
||||
seen_iids[iid] = true
|
||||
if new_hp < old_hp:
|
||||
_play_unit_anim(iid, "damaged")
|
||||
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.
|
||||
for iid: String in _prev_unit_hp:
|
||||
|
||||
Reference in New Issue
Block a user