From a281d40bf5b301137144419f48b360f6be5e8d18 Mon Sep 17 00:00:00 2001 From: Moe Poi ~ Date: Sun, 19 Nov 2023 17:49:32 +0700 Subject: [PATCH] Add animation when the enemy die --- scenes/enemies/cyclops.tscn | 5 +++++ scenes/enemies/demon.tscn | 5 +++++ scenes/enemies/demon_boar.tscn | 5 +++++ scenes/enemies/demon_fungus.tscn | 5 +++++ scenes/enemies/demon_shark.tscn | 5 +++++ scenes/enemies/orc.tscn | 5 +++++ scripts/enemies/enemy.gd | 27 ++++++++++++++++++++------- scripts/towers/tower.gd | 13 ++++++++----- 8 files changed, 58 insertions(+), 12 deletions(-) diff --git a/scenes/enemies/cyclops.tscn b/scenes/enemies/cyclops.tscn index e5891b1..ec7bd5a 100644 --- a/scenes/enemies/cyclops.tscn +++ b/scenes/enemies/cyclops.tscn @@ -305,4 +305,9 @@ show_percentage = false [node name="SlowTimer" type="Timer" parent="EnemyBody"] one_shot = true +[node name="DeadTimer" type="Timer" parent="EnemyBody"] +wait_time = 3.0 +one_shot = true + [connection signal="timeout" from="EnemyBody/SlowTimer" to="." method="_on_slow_timer_timeout"] +[connection signal="timeout" from="EnemyBody/DeadTimer" to="." method="_on_dead_timer_timeout"] diff --git a/scenes/enemies/demon.tscn b/scenes/enemies/demon.tscn index a77611b..8a6326e 100644 --- a/scenes/enemies/demon.tscn +++ b/scenes/enemies/demon.tscn @@ -305,4 +305,9 @@ show_percentage = false [node name="SlowTimer" type="Timer" parent="EnemyBody"] one_shot = true +[node name="DeadTimer" type="Timer" parent="EnemyBody"] +wait_time = 3.0 +one_shot = true + [connection signal="timeout" from="EnemyBody/SlowTimer" to="." method="_on_slow_timer_timeout"] +[connection signal="timeout" from="EnemyBody/DeadTimer" to="." method="_on_dead_timer_timeout"] diff --git a/scenes/enemies/demon_boar.tscn b/scenes/enemies/demon_boar.tscn index 8d2cf97..52345a8 100644 --- a/scenes/enemies/demon_boar.tscn +++ b/scenes/enemies/demon_boar.tscn @@ -305,4 +305,9 @@ show_percentage = false [node name="SlowTimer" type="Timer" parent="EnemyBody"] one_shot = true +[node name="DeadTimer" type="Timer" parent="EnemyBody"] +wait_time = 3.0 +one_shot = true + [connection signal="timeout" from="EnemyBody/SlowTimer" to="." method="_on_slow_timer_timeout"] +[connection signal="timeout" from="EnemyBody/DeadTimer" to="." method="_on_dead_timer_timeout"] diff --git a/scenes/enemies/demon_fungus.tscn b/scenes/enemies/demon_fungus.tscn index a7a6488..89f8062 100644 --- a/scenes/enemies/demon_fungus.tscn +++ b/scenes/enemies/demon_fungus.tscn @@ -305,4 +305,9 @@ show_percentage = false [node name="SlowTimer" type="Timer" parent="EnemyBody"] one_shot = true +[node name="DeadTimer" type="Timer" parent="EnemyBody"] +wait_time = 3.0 +one_shot = true + [connection signal="timeout" from="EnemyBody/SlowTimer" to="." method="_on_slow_timer_timeout"] +[connection signal="timeout" from="EnemyBody/DeadTimer" to="." method="_on_dead_timer_timeout"] diff --git a/scenes/enemies/demon_shark.tscn b/scenes/enemies/demon_shark.tscn index ab1b2eb..678144a 100644 --- a/scenes/enemies/demon_shark.tscn +++ b/scenes/enemies/demon_shark.tscn @@ -305,4 +305,9 @@ show_percentage = false [node name="SlowTimer" type="Timer" parent="EnemyBody"] one_shot = true +[node name="DeadTimer" type="Timer" parent="EnemyBody"] +wait_time = 3.0 +one_shot = true + [connection signal="timeout" from="EnemyBody/SlowTimer" to="." method="_on_slow_timer_timeout"] +[connection signal="timeout" from="EnemyBody/DeadTimer" to="." method="_on_dead_timer_timeout"] diff --git a/scenes/enemies/orc.tscn b/scenes/enemies/orc.tscn index 8b45f73..ef6e205 100644 --- a/scenes/enemies/orc.tscn +++ b/scenes/enemies/orc.tscn @@ -305,4 +305,9 @@ show_percentage = false [node name="SlowTimer" type="Timer" parent="EnemyBody"] one_shot = true +[node name="DeadTimer" type="Timer" parent="EnemyBody"] +wait_time = 3.0 +one_shot = true + [connection signal="timeout" from="EnemyBody/SlowTimer" to="." method="_on_slow_timer_timeout"] +[connection signal="timeout" from="EnemyBody/DeadTimer" to="." method="_on_dead_timer_timeout"] diff --git a/scripts/enemies/enemy.gd b/scripts/enemies/enemy.gd index 824ab62..20837af 100644 --- a/scripts/enemies/enemy.gd +++ b/scripts/enemies/enemy.gd @@ -8,11 +8,13 @@ var speed: float = 0 var reward: int = 0 var direction_progress = [] var direction = [] +var current_direction = '' var enemy_vol = Vector2(1,1) var old_enemy_pos var max_health: int = 0 var is_slow: bool = false +var is_dead: bool = false func set_config(config_path): config = load(config_path).new() @@ -40,17 +42,25 @@ func _ready(): func _process(delta): update_healthbar() change_direction(get_progress()) - if health < 1: - get_tree().current_scene.on_enemy_reward(reward) - queue_free() - set_progress(get_progress() + speed * delta) - enemy_vol = global_position - old_enemy_pos - old_enemy_pos = global_position + if !is_dead: + if health < 1: + is_dead = true + $EnemyBody.remove_from_group("Enemy") + $EnemyBody/AnimatedSprite2D.play("{direction}_dead".format({"direction": current_direction})) + get_tree().current_scene.on_enemy_reward(reward) + $EnemyBody/DeadTimer.start() + + set_progress(get_progress() + speed * delta) + enemy_vol = global_position - old_enemy_pos + old_enemy_pos = global_position + else: + $EnemyBody/HealthBar.visible = false func change_direction(current_progress): if !direction_progress.is_empty(): if current_progress > direction_progress[0]: $EnemyBody/AnimatedSprite2D.play(direction[0]) + current_direction = direction[0] direction_progress.erase(direction_progress[0]) direction.erase(direction[0]) @@ -78,5 +88,8 @@ func _on_slow_timer_timeout(): speed = config.enemy[enemy_id]['speed'] is_slow = false +func _on_dead_timer_timeout(): + destroy() + func destroy(): - queue_free() + queue_free() \ No newline at end of file diff --git a/scripts/towers/tower.gd b/scripts/towers/tower.gd index 1ff0fbd..b51deb5 100644 --- a/scripts/towers/tower.gd +++ b/scripts/towers/tower.gd @@ -51,11 +51,14 @@ func _physics_process(_delta): if enemies.is_empty(): $AnimatedSprite2D.stop() else: - var distance = global_position.distance_to(enemies[0].global_position) - _update_look_at(enemies[0].global_position + enemies[0].enemy_vol * (distance / speed)) - if timer.is_stopped(): - $AnimatedSprite2D.play("default") - _shoot() + if !enemies[0].is_dead: + var distance = global_position.distance_to(enemies[0].global_position) + _update_look_at(enemies[0].global_position + enemies[0].enemy_vol * (distance / speed)) + if timer.is_stopped(): + $AnimatedSprite2D.play("default") + _shoot() + else: + enemies.erase(enemies[0]) func _shoot(): if bullet: