It's been a few weeks since I contributed to ShooterCarnival, and it was fun to see where they had taken it in that time. Slowly, but surely, they were updating the project and adding features, such as the shooting and enemies spawning.
I decided to go in and try to add some new features as well. I found this issue open that was asking for someone to implement the bullet and player collisions with the enemy sprites, which felt like a step up from my last contribution and a good opportunity to learn more GDScript.
I started by learning how to add the sprite node and rig it as an animation, which I actually did through the Godot editor and not through code. It was simply easier to make the new node and select the animation frames this way.
The rest of the work was through the code. I implemented the die() function that swapped the enemy sprite to the explosion sprite, disabled collisions and played the animation:
func die() -> void:
if is_dying:
return
# Mark as dying
is_dying = true
speed = 0
# Disable collision
collision_shape.set_deferred("disabled", true)
# Play explosion animation
sprite.visible = false
explosion_sprite.visible = true
explosion_sprite.play("explode")
explosion_sprite.connect("animation_finished", _on_explosion_sprite_animation_finished)
Next was triggering the die function when the bullet or player touched an enemy. When two Area2D nodes overlap, they emit signals. We use area_entered(area) to look for when another Area2D node enters the area to trigger the dying animation, as well as remove the bullet (or player).
func _on_area_entered(target: Node2D) -> void:
# Bullet collision handling
if target.is_in_group("bullets"):
# Remove the bullet
target.queue_free()
# Explode the enemy
die()
# Player collision handling
if target.is_in_group("player"):
# Explode the enemy
die()
# Hide player
target.hide()
# TODO: Game over, or notify player of hit
For now, the issue just asks to hide the player, so that is how I implemented it.
After setting up the groups and writing the area entered logic for bullets as well, we get this, which looks ready for a pull request.

Top comments (0)