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):

View File

@@ -6,7 +6,7 @@
[sub_resource type="Curve2D" id="Curve2D_frsqi"]
bake_interval = 2.0
_data = {
"points": PackedVector2Array(0, 0, 0, 0, 0, 0, 0, 0, 25, 10, 150, 0)
"points": PackedVector2Array(0, 0, 0, 0, 0, 0, 0, 0, 25, 10, 200, 0)
}
point_count = 2
@@ -28,7 +28,6 @@ loop = false
[node name="Sprite2D" type="Sprite2D" parent="AttackPath/PathFollow2D"]
position = Vector2(0.322462, -0.946582)
rotation = 0.328329
scale = Vector2(1, 1)
texture = ExtResource("1_3fwwl")
[node name="AttackArea" type="Area2D" parent="AttackPath/PathFollow2D/Sprite2D"]
@@ -49,7 +48,6 @@ shape = SubResource("CircleShape2D_3fwwl")
[node name="Timer" type="Timer" parent="."]
one_shot = true
autostart = true
[connection signal="body_entered" from="AttackPath/PathFollow2D/Sprite2D/AttackArea" to="." method="_on_attack_area_body_entered"]
[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"]

View File

@@ -6,6 +6,21 @@
[sub_resource type="CircleShape2D" id="CircleShape2D_6xk8f"]
radius = 6.7082
[sub_resource type="Animation" id="Animation_38ino"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:position")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(0, 0)]
}
[sub_resource type="Animation" id="Animation_8e3ao"]
resource_name = "jump"
length = 0.7
@@ -23,21 +38,6 @@ tracks/0/keys = {
"values": [Vector2(0, 0), Vector2(0, -7)]
}
[sub_resource type="Animation" id="Animation_38ino"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:position")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(0, 0)]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_38ino"]
_data = {
&"RESET": SubResource("Animation_38ino"),