fuzzyworld

godot-shooter / commit

Add explosion when enemy dies, using particle generator and the star image in assets.

c6faad15ea2ad241cacdbc68c2d22dccfb9bf5c4 · Pavel Safronov · 2023-10-31 11:08:31 -0700 · parents 169b896b6f15

Add explosion when enemy dies, using particle generator and the star image in assets.
 Enemy.gd       |  7 +++++++
 Explosion.gd   | 10 ++++++++++
 Explosion.tscn | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 project.godot  |  1 -
 4 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/Enemy.gd b/Enemy.gd
index 0ab478e..84b54d7 100644
--- a/Enemy.gd
+++ b/Enemy.gd
@@ -3,6 +3,7 @@ extends KinematicBody2D
 onready var ray_cast_2d: RayCast2D = $RayCast2D
 onready var scene_tree: SceneTree = get_tree()
 const utils = preload("res://Utils.gd")
+var Explosion = preload("res://Explosion.tscn")
 
 export var move_speed = 100
 export var health: int = randi() % 8 + 2
@@ -50,6 +51,12 @@ func kill():
 	utils.log("zombie is dead")
 	dead = true
 	$Graphics/Alive.hide()
+	var explosion = Explosion.instance()
+	explosion.connect("explosion_done", self, "_on_death_explosion_finished")
+	$Graphics.add_child(explosion)
 	$CollisionPolygon2D.disabled = true
 	z_index = -1
+
+func _on_death_explosion_finished():
+	utils.log("removing zombie after explosion finished")
 	queue_free()
diff --git a/Explosion.gd b/Explosion.gd
new file mode 100644
index 0000000..64136f5
--- /dev/null
+++ b/Explosion.gd
@@ -0,0 +1,10 @@
+extends Particles2D
+
+signal explosion_done
+
+func _ready() -> void:
+	one_shot = true
+
+func _on_Timer_timeout() -> void:
+	emit_signal("explosion_done")
+	queue_free()
diff --git a/Explosion.tscn b/Explosion.tscn
new file mode 100644
index 0000000..2404580
--- /dev/null
+++ b/Explosion.tscn
@@ -0,0 +1,50 @@
+[gd_scene load_steps=8 format=2]
+
+[ext_resource path="res://kenney_simple-space/PNG/Default/star_small.png" type="Texture" id=1]
+[ext_resource path="res://Explosion.gd" type="Script" id=2]
+
+[sub_resource type="Gradient" id=4]
+offsets = PoolRealArray( 0, 0.941935, 1 )
+colors = PoolColorArray( 1, 1, 1, 1, 1, 0.0580645, 0.0580645, 1, 1, 0, 0, 1 )
+
+[sub_resource type="GradientTexture" id=5]
+gradient = SubResource( 4 )
+
+[sub_resource type="Curve" id=1]
+_data = [ Vector2( 0, 1 ), 0.0, 0.0, 0, 0, Vector2( 1, 0 ), 0.0, 0.0, 0, 0 ]
+
+[sub_resource type="CurveTexture" id=2]
+curve = SubResource( 1 )
+
+[sub_resource type="ParticlesMaterial" id=3]
+emission_shape = 1
+emission_sphere_radius = 0.01
+flag_disable_z = true
+spread = 180.0
+gravity = Vector3( 0, 0, 0 )
+initial_velocity = 470.3
+initial_velocity_random = 0.52
+orbit_velocity = 0.0
+orbit_velocity_random = 0.0
+damping = 1000.0
+angle = 306.5
+angle_random = 0.4
+scale = 0.5
+scale_random = 1.0
+scale_curve = SubResource( 2 )
+color_ramp = SubResource( 5 )
+
+[node name="Explosion" type="Particles2D"]
+amount = 100
+lifetime = 0.5
+explosiveness = 1.0
+process_material = SubResource( 3 )
+texture = ExtResource( 1 )
+script = ExtResource( 2 )
+
+[node name="Timer" type="Timer" parent="."]
+wait_time = 0.5
+one_shot = true
+autostart = true
+
+[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"]
diff --git a/project.godot b/project.godot
index 91d187f..ef1ba3f 100644
--- a/project.godot
+++ b/project.godot
@@ -63,6 +63,5 @@ common/enable_pause_aware_picking=true
 
 [rendering]
 
-quality/driver/driver_name="GLES2"
 vram_compression/import_etc=true
 environment/default_environment="res://default_env.tres"