From e0580c25e4aea55090bcb44da32dfd104cadd153 Mon Sep 17 00:00:00 2001 From: rassv453 Date: Thu, 24 Oct 2024 10:00:38 +0200 Subject: [PATCH] Added invincible when taking damage --- scenes/characters/mouse/player.tscn | 5 +++++ scripts/characters/mouse/player.gd | 34 +++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/scenes/characters/mouse/player.tscn b/scenes/characters/mouse/player.tscn index d41a32a..894ce76 100644 --- a/scenes/characters/mouse/player.tscn +++ b/scenes/characters/mouse/player.tscn @@ -658,3 +658,8 @@ debug_color = Color(1, 0, 0.0999999, 0.682353) [node name="hit_box_collision" type="CollisionShape2D" parent="hit_box"] shape = SubResource("CircleShape2D_vktk8") debug_color = Color(0, 0.64181, 0.328266, 0.42) + +[node name="invincibility_timer" type="Timer" parent="."] +unique_name_in_owner = true + +[connection signal="timeout" from="invincibility_timer" to="." method="_on_invincibility_timer_timeout"] diff --git a/scripts/characters/mouse/player.gd b/scripts/characters/mouse/player.gd index 0393b64..40c34d7 100644 --- a/scripts/characters/mouse/player.gd +++ b/scripts/characters/mouse/player.gd @@ -11,6 +11,8 @@ var attack_duration = 0.5 var is_falling = false var is_flying = false var attack_switch = false +var is_invincible = false + # Cooldown variables var attack_cooldown = 0.7 @@ -37,6 +39,7 @@ var knockback_strength = 220 @onready var animated_sprite_2d: AnimatedSprite2D = $animated_sprite_mouse @onready var hit_box: HitBox = $hit_box @onready var hurt_box: HurtBox = $hurt_box +@onready var invincibility_timer: Timer = %invincibility_timer func _ready(): spawn_point = global_position @@ -170,23 +173,36 @@ func is_colliding() -> bool: # Function to handle taking damage func take_damage(amount: int, attacker_position: Vector2): - if is_dead: + if is_dead or is_invincible: return + # Update health current_health -= amount hud.update_health(current_health) + + start_invincibility() # Emit signal when damage is taken player_take_damage.emit() + # Calculate knockback direction here + var direction = (position - attacker_position).normalized() + knockback_velocity = direction * knockback_strength + knockback_timer = knockback_duration + animated_sprite_2d.play("damaged") + if current_health <= 0: die() - else: - # Calculate knockback direction here - var direction = (position - attacker_position).normalized() - knockback_velocity = direction * knockback_strength - knockback_timer = knockback_duration - animated_sprite_2d.play("damaged") + +func start_invincibility() -> void: + is_invincible = true + + var tween = create_tween() + tween.set_loops(4) # Flash 4 times + tween.tween_property(animated_sprite_2d, "modulate:a", 0.2, 0.1) + tween.tween_property(animated_sprite_2d, "modulate:a", 1.0, 0.1) + + invincibility_timer.start(1.0) # Function to handle player death func die(): @@ -234,3 +250,7 @@ func fall_in_pit(): await tween.finished is_falling = false + + +func _on_invincibility_timer_timeout() -> void: + is_invincible = false