Skip to content

Commit

Permalink
Enemies don't spam hurt sounds from Fireball
Browse files Browse the repository at this point in the history
  • Loading branch information
ddabble committed Feb 21, 2018
1 parent d93a014 commit 0cbb155
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Assets/MagicBook/Scripts/Spells/Fireball/FireballRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ private IEnumerator FadeAudio(float fadeTime)

void InflictDamage(Enemy enemy)
{
enemy.InflictDamage(attackDamage * Time.deltaTime);
enemy.InflictDamage(attackDamage * Time.deltaTime, this);
}
}
4 changes: 2 additions & 2 deletions Assets/Scripts/Enemy/Enemy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
21 changes: 17 additions & 4 deletions Assets/Scripts/Enemy/Enemy_Sound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ abstract partial class Enemy
private float nextIdleSoundTime;
private float lastSoundPlayTime;

private AudioClip lastPlayedSound;

void SoundStart()
{
nextIdleSoundTime = Time.time;
Expand Down Expand Up @@ -49,25 +51,36 @@ 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()
{
AudioClip clip = SoundManager.PlayRandomSound(this, deathSounds, randomPitchRange, deathVolume);
enemyManager.OnEnemyPlayedSound(this);
lastSoundPlayTime = Time.time;
lastPlayedSound = clip;
return clip;
}
}
7 changes: 5 additions & 2 deletions Assets/Scripts/Interfaces/IDamagable.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
public interface IDamagable {
void InflictDamage (float damage);
using UnityEngine;

public interface IDamagable
{
void InflictDamage(float damage, Component source = null);
}
5 changes: 3 additions & 2 deletions Assets/Scripts/Interfaces/IGameHealthIndicationItem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using UnityEngine;

public interface IGameHealthIndicationItem{
public interface IGameHealthIndicationItem
{
int GetHealthAmount();
GameObject GetGameObject ();
GameObject GetGameObject();
void Kill();
}
13 changes: 7 additions & 6 deletions Assets/Scripts/Interfaces/IGameManager.cs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 0cbb155

Please sign in to comment.