fuzzyworld

godot-shooter / commit

For enemy damage, only create as many smokes as there are damage points.

13440f93cc161173c7f8ca29b17d249de74a8123 · Pavel Safronov · 2023-11-27 15:30:56 -0800 · parents 8adb41dc7efa

For enemy damage, only create as many smokes as there are damage points.
 actors/Enemy.gd | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/actors/Enemy.gd b/actors/Enemy.gd
index c0794c5..b48ad66 100644
--- a/actors/Enemy.gd
+++ b/actors/Enemy.gd
@@ -73,15 +73,23 @@ func turn_towards(pos: Vector2):
 	var change = dir_diff * turn_rate * move_speed
 	velocity = (velocity + change).normalized() * move_speed
 
+func _ensure_smoke_exists() -> void:
+	var max_smoke_count = $DamagePoints.get_child_count()
+	var existing_smoke_count = $DamageObjects.get_child_count()
+	var expected_smoke_count = ceil(health_ratio * max_smoke_count)
+	var smoke_to_add_count = expected_smoke_count - existing_smoke_count
+	for i in smoke_to_add_count:
+		var smoke = Smoke.instance()
+		var smoke_pos = get_damage_point_on_body()
+		smoke.position = smoke_pos
+		$DamageObjects.add_child(smoke)
+
 func hurt(damage: int = 1):
 	if dead || is_invincible:
 		return
 	health -= damage
-	var smoke = Smoke.instance()
-	var smoke_pos = get_damage_point_on_body()
-	smoke.position = smoke_pos
-	$DamageObjects.add_child(smoke)
 #	add_child(smoke)
+	_ensure_smoke_exists()
 	if health <= 0:
 		kill()