From 0cbb15507782f39e56039b7ab638c7574052a283 Mon Sep 17 00:00:00 2001 From: Dabble Date: Wed, 21 Feb 2018 22:25:04 +0100 Subject: [PATCH] Enemies don't spam hurt sounds from Fireball --- .../Scripts/Spells/Fireball/FireballRange.cs | 2 +- Assets/Scripts/Enemy/Enemy.cs | 4 ++-- Assets/Scripts/Enemy/Enemy_Sound.cs | 21 +++++++++++++++---- Assets/Scripts/Interfaces/IDamagable.cs | 7 +++++-- .../Interfaces/IGameHealthIndicationItem.cs | 5 +++-- Assets/Scripts/Interfaces/IGameManager.cs | 13 ++++++------ 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Assets/MagicBook/Scripts/Spells/Fireball/FireballRange.cs b/Assets/MagicBook/Scripts/Spells/Fireball/FireballRange.cs index be0a1e51..97c8b6c0 100644 --- a/Assets/MagicBook/Scripts/Spells/Fireball/FireballRange.cs +++ b/Assets/MagicBook/Scripts/Spells/Fireball/FireballRange.cs @@ -90,6 +90,6 @@ private IEnumerator FadeAudio(float fadeTime) void InflictDamage(Enemy enemy) { - enemy.InflictDamage(attackDamage * Time.deltaTime); + enemy.InflictDamage(attackDamage * Time.deltaTime, this); } } diff --git a/Assets/Scripts/Enemy/Enemy.cs b/Assets/Scripts/Enemy/Enemy.cs index 5231b7a7..d2ca3288 100644 --- a/Assets/Scripts/Enemy/Enemy.cs +++ b/Assets/Scripts/Enemy/Enemy.cs @@ -89,11 +89,11 @@ void Update() SoundUpdate(); } - public void InflictDamage(float damage) + public void InflictDamage(float damage, Component source = null) { health -= damage; if (health > 0f) - PlayHurtSound(); + PlayHurtSound(source); else OnDeath(); } diff --git a/Assets/Scripts/Enemy/Enemy_Sound.cs b/Assets/Scripts/Enemy/Enemy_Sound.cs index cc5ccdfd..8df72a3b 100644 --- a/Assets/Scripts/Enemy/Enemy_Sound.cs +++ b/Assets/Scripts/Enemy/Enemy_Sound.cs @@ -18,6 +18,8 @@ abstract partial class Enemy private float nextIdleSoundTime; private float lastSoundPlayTime; + private AudioClip lastPlayedSound; + void SoundStart() { nextIdleSoundTime = Time.time; @@ -49,18 +51,28 @@ private void HandleIdleSound() private void PlayIdleSound() { float currentTime = Time.time; - SoundManager.PlayRandomSound(this, idleSounds, randomPitchRange, idleVolume); + AudioClip clip = SoundManager.PlayRandomSound(this, idleSounds, randomPitchRange, idleVolume); enemyManager.OnEnemyPlayedSound(this); lastSoundPlayTime = currentTime; + lastPlayedSound = clip; // Longer timeout when enemy has just played a sound nextIdleSoundTime = currentTime + idleSoundFreq_sec * 1.5f + Random.value * idleSoundFreq_sec; } - private void PlayHurtSound() + private void PlayHurtSound(Component source) { - SoundManager.PlayRandomSound(this, hurtSounds, randomPitchRange, hurtVolume); + float currentTime = Time.time; + if (source is FireballRange) + { + if (lastPlayedSound != null + && currentTime <= lastSoundPlayTime + lastPlayedSound.length * 1.5f) // 50% extra silence time + return; + } + + AudioClip clip = SoundManager.PlayRandomSound(this, hurtSounds, randomPitchRange, hurtVolume); enemyManager.OnEnemyPlayedSound(this); - lastSoundPlayTime = Time.time; + lastSoundPlayTime = currentTime; + lastPlayedSound = clip; } private AudioClip PlayDeathSound() @@ -68,6 +80,7 @@ private AudioClip PlayDeathSound() AudioClip clip = SoundManager.PlayRandomSound(this, deathSounds, randomPitchRange, deathVolume); enemyManager.OnEnemyPlayedSound(this); lastSoundPlayTime = Time.time; + lastPlayedSound = clip; return clip; } } diff --git a/Assets/Scripts/Interfaces/IDamagable.cs b/Assets/Scripts/Interfaces/IDamagable.cs index 53326413..eda67471 100644 --- a/Assets/Scripts/Interfaces/IDamagable.cs +++ b/Assets/Scripts/Interfaces/IDamagable.cs @@ -1,3 +1,6 @@ -public interface IDamagable { - void InflictDamage (float damage); +using UnityEngine; + +public interface IDamagable +{ + void InflictDamage(float damage, Component source = null); } diff --git a/Assets/Scripts/Interfaces/IGameHealthIndicationItem.cs b/Assets/Scripts/Interfaces/IGameHealthIndicationItem.cs index 68ddaf7e..6079f8ae 100644 --- a/Assets/Scripts/Interfaces/IGameHealthIndicationItem.cs +++ b/Assets/Scripts/Interfaces/IGameHealthIndicationItem.cs @@ -1,7 +1,8 @@ using UnityEngine; -public interface IGameHealthIndicationItem{ +public interface IGameHealthIndicationItem +{ int GetHealthAmount(); - GameObject GetGameObject (); + GameObject GetGameObject(); void Kill(); } diff --git a/Assets/Scripts/Interfaces/IGameManager.cs b/Assets/Scripts/Interfaces/IGameManager.cs index 3c18f685..2f270562 100644 --- a/Assets/Scripts/Interfaces/IGameManager.cs +++ b/Assets/Scripts/Interfaces/IGameManager.cs @@ -1,7 +1,8 @@ -public interface IGameManager{ - void GameLost(); - void GameStart(); - void GamePause(); - void GameRestart(); - void GameWin(); +public interface IGameManager +{ + void GameLost(); + void GameStart(); + void GamePause(); + void GameRestart(); + void GameWin(); }