Skip to content

Commit

Permalink
🎵 Play hit sound
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrijnbeek committed Oct 1, 2023
1 parent f1950cf commit c07d26f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 12 additions & 0 deletions entities/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,25 @@ public void TakeDamage(int amountOfDamage)
var oldHealth = CurrentHealth;
CurrentHealth = Math.Max(CurrentHealth - amountOfDamage, 0);
redness = 1;
playHitSound();
EmitSignal(nameof(HealthChanged), CurrentHealth, MaxHealth, CurrentHealth - oldHealth);
if (CurrentHealth <= 0)
{
OnDeath();
}
}

private void playHitSound()
{
if (GetNode<AudioStreamPlayer>("HitSound") is not { } sound)
{
return;
}

sound.Seek(0.1f);
sound.Play();
}

protected virtual void OnDeath() {}

public void Heal(int amountToHeal)
Expand Down
6 changes: 5 additions & 1 deletion entities/hazard/hazard.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=6 format=2]

[ext_resource path="res://entities/hazard/Hazard.cs" type="Script" id=1]
[ext_resource path="res://assets/images/slime.png" type="Texture" id=2]
[ext_resource path="res://assets/images/health_fill.png" type="Texture" id=3]
[ext_resource path="res://assets/images/hazard_health.png" type="Texture" id=4]
[ext_resource path="res://assets/audio/hit.wav" type="AudioStream" id=5]

[node name="Hazard" type="Node2D"]
script = ExtResource( 1 )
Expand All @@ -23,3 +24,6 @@ margin_top = -1.0
margin_right = 63.0
margin_bottom = 2.0
texture = ExtResource( 3 )

[node name="HitSound" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 5 )
6 changes: 5 additions & 1 deletion entities/player/player.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=4 format=2]

[ext_resource path="res://entities/player/Player.cs" type="Script" id=1]
[ext_resource path="res://assets/images/hero.png" type="Texture" id=2]
[ext_resource path="res://assets/audio/hit.wav" type="AudioStream" id=3]

[node name="Player" type="Node2D"]
script = ExtResource( 1 )
Expand All @@ -10,3 +11,6 @@ script = ExtResource( 1 )

[node name="Sprite" type="Sprite" parent="Offset"]
texture = ExtResource( 2 )

[node name="HitSound" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 3 )

0 comments on commit c07d26f

Please sign in to comment.