From c07d26f9c925e862722b4ae6048b71c94c0f5e56 Mon Sep 17 00:00:00 2001 From: Tom Rijnbeek Date: Sun, 1 Oct 2023 22:50:17 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=B5=20Play=20hit=20sound?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entities/Character.cs | 12 ++++++++++++ entities/hazard/hazard.tscn | 6 +++++- entities/player/player.tscn | 6 +++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/entities/Character.cs b/entities/Character.cs index 8d76474..7264acd 100644 --- a/entities/Character.cs +++ b/entities/Character.cs @@ -80,6 +80,7 @@ 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) { @@ -87,6 +88,17 @@ public void TakeDamage(int amountOfDamage) } } + private void playHitSound() + { + if (GetNode("HitSound") is not { } sound) + { + return; + } + + sound.Seek(0.1f); + sound.Play(); + } + protected virtual void OnDeath() {} public void Heal(int amountToHeal) diff --git a/entities/hazard/hazard.tscn b/entities/hazard/hazard.tscn index 35a2bce..c956c28 100644 --- a/entities/hazard/hazard.tscn +++ b/entities/hazard/hazard.tscn @@ -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 ) @@ -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 ) diff --git a/entities/player/player.tscn b/entities/player/player.tscn index 3b10a22..9134f0a 100644 --- a/entities/player/player.tscn +++ b/entities/player/player.tscn @@ -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 ) @@ -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 )