Animaciones

This commit is contained in:
Diego De la Viuda Llorente committed 2026-10-27 10:58:57 +01:00
1 parent b365c79251
commit 8a5290c5ef
10 files changed
+1581 -288

No files matched your search

+79 -11
View File
@@ -10,6 +10,7 @@ signal died
const PLAYER_HURTBOX_MASK: int = 1 << 3
const DROP_MANAGER := preload("res://pickups/drop_manager.gd")
const COMBAT_FX = preload("res://effects/combat_fx.gd")
enum AttackState {
@@ -108,6 +109,9 @@ var drops_handled_by_level: bool = false
## A value of 0 disables respawn (one enemy = one test item).
@export var respawn_delay: float = 5.0
## Visible launch, collapse and fade before the body disappears.
@export_range(0.3, 2.5, 0.05) var death_animation_duration: float = 0.9
## Limits how far the enemy may chase from its spawn point; 0 = unrestricted.
## Small limits keep independent testing encounters from piling up.
@export var chase_radius: float = 0.0
@@ -125,7 +129,7 @@ var drops_handled_by_level: bool = false
## Enabled while the enemy has no final sprite/animation. The grey rectangle is
## the temporary body. Red overlays are the hurtbox and active attack hitbox.
## The orange area during windup previews the complete attack sweep.
@export var show_debug_hitboxes: bool = true
@export var show_debug_hitboxes: bool = false
## Shows the enemy health bar above its head and floating damage numbers when
## it is hit. Purely debug: can be disabled once final art and UI exist.
@@ -173,9 +177,20 @@ var knockback_timer: float = 0.0
## Floating damage numbers shown above the enemy when it takes damage.
## Each entry: { text: String, local_pos: Vector2, timer: float }
var _damage_texts: Array = []
var _hit_flash_timer: float = 0.0
var _death_elapsed: float = 0.0
var _last_hit_direction: float = 1.0
var _visual_position: Vector2
var _visual_scale: Vector2
var _visual_rotation: float
var _visual_color: Color
func _ready() -> void:
_visual_position = visual.position
_visual_scale = visual.scale
_visual_rotation = visual.rotation
_visual_color = visual.self_modulate
spawn_position = global_position
current_health = max_health
target = get_tree().get_first_node_in_group(&"player") as Node2D
@@ -198,8 +213,11 @@ func _ready() -> void:
func _physics_process(delta: float) -> void:
if is_dead:
_update_death_animation(delta)
return
_hit_flash_timer = maxf(0.0, _hit_flash_timer - delta)
visual.self_modulate = Color(1.8, 1.8, 1.8) if _hit_flash_timer > 0.0 else _visual_color
_update_timers(delta)
_update_damage_texts(delta)
_apply_gravity(delta)
@@ -465,6 +483,15 @@ func take_damage(amount: int, source_position: Vector2) -> void:
return
current_health = max(current_health - amount, 0)
_hit_flash_timer = 0.09
is_awake = true
_last_hit_direction = signf(global_position.x - source_position.x)
if _last_hit_direction == 0.0:
_last_hit_direction = -facing_direction
COMBAT_FX.spawn(get_parent(), global_position, &"hit", _last_hit_direction)
var camera: Camera2D = get_viewport().get_camera_2d()
if camera != null and camera.has_method("impact"):
camera.call("impact", 1.0, 0.12)
# Floating damage number (debug only).
if show_debug_enemy_info:
@@ -508,7 +535,14 @@ func take_damage(amount: int, source_position: Vector2) -> void:
func _die() -> void:
if is_dead:
return
_interrupt_attack()
is_dead = true
_death_elapsed = 0.0
velocity = Vector2(_last_hit_direction * 150.0, -210.0)
COMBAT_FX.spawn(get_parent(), global_position, &"death", _last_hit_direction)
# Logical death happens now, exactly once. Animation must not delay rewards.
died.emit()
# Test encounters always drop their assigned item, including the two items
@@ -525,21 +559,51 @@ func _die() -> void:
parent = get_tree().root
DROP_MANAGER.spawn_drops(drops, parent, global_position)
velocity = Vector2.ZERO
visible = false
# take_damage() can run while Godot is flushing physics queries, therefore
# collision shapes are disabled deferred instead of changing them directly.
body_collision.set_deferred("disabled", true)
# Disable combat immediately; keep the body colliding with the WORLD while it
# falls. Disabling the body now would let the death pose fall through streets.
hurtbox_collision.set_deferred("disabled", true)
attack_collision.set_deferred("disabled", true)
queue_redraw()
if respawn_delay > 0.0:
await get_tree().create_timer(respawn_delay).timeout
func _update_death_animation(delta: float) -> void:
_death_elapsed += delta
_update_damage_texts(delta)
var duration: float = maxf(death_animation_duration, 0.3)
if _death_elapsed < duration:
_apply_gravity(delta)
velocity.x = move_toward(velocity.x, 0.0, 180.0 * delta)
move_and_slide()
var collapse: float = smoothstep(0.06, duration * 0.55, _death_elapsed)
visual.rotation = _visual_rotation + _last_hit_direction * PI * 0.5 * collapse
visual.position = _visual_position + Vector2(0, 10.0 * collapse)
visual.scale = _visual_scale * Vector2(1.0, lerpf(1.0, 0.85, collapse))
var fade: float = 1.0 - smoothstep(duration * 0.65, duration, _death_elapsed)
var color: Color = Color(1.8, 1.8, 1.8) if _death_elapsed < 0.09 else _visual_color
color.a = _visual_color.a * fade
visual.self_modulate = color
queue_redraw()
return
visible = false
velocity = Vector2.ZERO
body_collision.set_deferred("disabled", true)
if respawn_delay <= 0.0:
# Keep the dead node for the lab's KO polling and Level02's total count.
set_physics_process(false)
elif _death_elapsed >= maxf(respawn_delay, duration):
_respawn()
func _respawn() -> void:
set_physics_process(true)
_death_elapsed = 0.0
_hit_flash_timer = 0.0
visual.position = _visual_position
visual.scale = _visual_scale
visual.rotation = _visual_rotation
visual.self_modulate = _visual_color
_damage_texts.clear()
global_position = spawn_position
velocity = Vector2.ZERO
current_health = max_health
@@ -571,6 +635,10 @@ func _respawn() -> void:
# ============================================================================
func _draw() -> void:
if is_dead:
if show_debug_enemy_info:
_draw_damage_texts()
return
# Retain a fallback body if an imported sprite is missing.
if visual.texture == null:
_draw_rectangle_collision(
@@ -650,7 +718,7 @@ func _draw_enemy_health_bar() -> void:
# Background.
draw_rect(Rect2(bar_pos, Vector2(bar_width, bar_height)), Color(0.2, 0.2, 0.2, 0.8), true)
# Fill.
var fill_ratio: float = current_health / max_health if max_health > 0 else 0.0
var fill_ratio: float = float(current_health) / float(max_health) if max_health > 0 else 0.0
draw_rect(
Rect2(bar_pos, Vector2(bar_width * fill_ratio, bar_height)),
Color(0.85, 0.2, 0.2, 0.95),
@@ -667,4 +735,4 @@ func _draw_damage_texts() -> void:
for entry in _damage_texts:
var alpha: float = clamp(entry.timer, 0.0, 1.0)
draw_string(font, entry.local_pos, entry.text, 0, -1, 16, Color(1, 1, 1, alpha))
draw_string(font, entry.local_pos, entry.text, HORIZONTAL_ALIGNMENT_LEFT, -1, 16, Color(1, 1, 1, alpha))