game: improve attack animation

This commit is contained in:
2025-08-19 03:34:07 +02:00
parent ae413deedd
commit 4682171a2f
3 changed files with 39 additions and 28 deletions

View File

@@ -14,44 +14,51 @@ var damage: float
var current_target: Node2D = null
var current_progress: float = 0.0
var damaged_this_attack: Array = []
var is_attacking: bool = false
func _ready() -> void:
attack_path.visible = false
damage = default_damage
func _physics_process(delta: float) -> void:
if timer.is_stopped():
func _process(delta: float) -> void:
if timer.is_stopped() and current_progress == 0.0:
print_debug("starting timer")
timer.start()
if current_progress == 1.0:
else:
print_debug("timer status: %s\nprogress: %s" % [timer.is_stopped(), current_progress])
if current_progress > 0.95:
print_debug("reset")
reset_attack()
if current_target and is_instance_valid(current_target) and not current_target.is_queued_for_deletion():
track_target(current_target)
is_attacking = true
attack_path.visible = true
# Do attack animation
current_progress += delta / default_attack_time
current_progress = clampf(current_progress, 0.0, 1.0)
path_follow_2d.progress_ratio = current_progress
if is_attacking:
# Do attack animation
current_progress += delta / default_attack_time
current_progress = clampf(current_progress, 0.0, 1.0)
path_follow_2d.progress_ratio = current_progress
func reset_attack() -> void:
current_target = null
attack_path.visible = false
current_progress = 0.0
damaged_this_attack = []
is_attacking = false
position = Vector2.ZERO
rotation = 0.0
func set_target(body: Node2D):
current_target = body
is_attacking = true
func track_target(body: Node2D):
if not current_target or not is_instance_valid(body) or body.is_queued_for_deletion():
print_debug("target deleted or queued for deletion")
return
var mid_distance = attack_path.curve.get_baked_length() / 2
var mid_point: Vector2 = attack_path.curve.sample_baked(mid_distance)
var offset = body.global_position - to_global(mid_point)
var desired_dir = (body.global_position - to_global(mid_point)).normalized()
var start_point_global = attack_path.to_global(attack_path.curve.sample_baked(0))
var end_point_global = attack_path.to_global(attack_path.curve.sample_baked(attack_path.curve.get_baked_length()))
@@ -63,6 +70,12 @@ func track_target(body: Node2D):
position += offset
func _on_timer_timeout() -> void:
if current_target:
if trigger_area.has_overlapping_areas():
if current_target not in trigger_area.get_overlapping_bodies():
print_debug("target out of range")
current_target = null
return
if trigger_area.has_overlapping_bodies():
for body in trigger_area.get_overlapping_bodies():
if body.is_in_group(GlobalConst.GROUP_ENEMY):