diff --git a/enemies/walker/walker.gd b/enemies/walker/walker.gd index 010ae4d..5c35d43 100644 --- a/enemies/walker/walker.gd +++ b/enemies/walker/walker.gd @@ -70,6 +70,19 @@ enum AttackState { ## during its windup useful instead of forcing an unavoidable damage trade. @export var hit_stun_duration: float = 0.18 +## Strength of the knockback applied per point of damage. The final push is +## divided by the enemy's max health, so tougher enemies move less. +@export var knockback_strength: float = 500.0 + +## How long the knockback impulse lasts before friction brings the enemy to a halt. +@export var knockback_duration: float = 0.4 + +## Damage threshold to trigger a longer stun (e.g. a fully charged special attack). +@export_range(0, 100, 1) var stun_damage_threshold: int = 5 + +## Stun duration when the received damage exceeds the threshold. +@export var strong_hit_stun_duration: float = 2.0 + # ============================================================================ # RESPAWN @@ -131,6 +144,9 @@ var target: Node2D ## Prevents one sweep from damaging the same target once per physics frame. var attack_hit_targets: Dictionary = {} +## Timer for the knockback impulse applied when the enemy is hit. +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 = [] @@ -176,11 +192,18 @@ func _physics_process(delta: float) -> void: queue_redraw() return - # Being hit pauses both movement and attack for only a few frames. The + # Being hit pauses both movement and attack. While the knockback impulse + # lasts the enemy is pushed back, then friction brings it to a halt. The # cooldown keeps running, so interrupting an attack does not make the enemy # immediately start another one. if hit_stun_timer > 0.0: - velocity.x = 0.0 + if knockback_timer > 0.0: + knockback_timer = max(knockback_timer - delta, 0.0) + var fade: float = knockback_timer / knockback_duration if knockback_duration > 0.0 else 0.0 + velocity.x = move_toward(velocity.x, 0.0, knockback_strength * delta * (1.0 - fade + 0.1)) + else: + velocity.x = move_toward(velocity.x, 0.0, knockback_strength * delta) + move_and_slide() queue_redraw() return @@ -421,8 +444,25 @@ func take_damage(amount: int, source_position: Vector2) -> void: # Light enemies are interruptible. This makes a well-timed player attack a # defensive option and avoids mandatory damage trades at close range. _interrupt_attack() - hit_stun_timer = hit_stun_duration - velocity.x = 0.0 + + # Knockback: stronger hits push harder, tougher enemies (more max health) + # resist the push. Direction is away from the damage source. + var knockback_dir: float = signf(global_position.x - source_position.x) + if knockback_dir == 0.0: + knockback_dir = 1.0 + + var knockback_force: float = float(amount) * knockback_strength / float(max_health) + velocity.x = knockback_dir * knockback_force + velocity.y = -180.0 + + knockback_timer = knockback_duration + + # Stun: normal hits use the brief hit stun; very strong hits (e.g. a fully + # charged special attack) trigger a longer stun. + if amount >= stun_damage_threshold: + hit_stun_timer = max(hit_stun_timer, strong_hit_stun_duration) + else: + hit_stun_timer = max(hit_stun_timer, hit_stun_duration) var reaction_direction: float = signf(source_position.x - global_position.x) if reaction_direction != 0.0: @@ -456,6 +496,7 @@ func _respawn() -> void: attack_phase_timer = 0.0 attack_cooldown_timer = 0.0 hit_stun_timer = 0.0 + knockback_timer = 0.0 attack_hit_targets.clear() attack_direction = facing_direction _reset_attack_hitbox_position()